import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { B as Button } from "./button-CdJZJLGw.js";
import { usePage, Link } from "@inertiajs/react";
import { ExternalLink, Pencil } from "lucide-react";
import { useState, useEffect } from "react";
import LiveClassForm from "./live-class-form-CM7nrk8J.js";
import "@radix-ui/react-slot";
import "class-variance-authority";
import "./utils-DLCPGU0v.js";
import "clsx";
import "tailwind-merge";
import "./datetime-picker-BRbsgYE_.js";
import "react-day-picker";
import "./popover-Cv6Hz_y0.js";
import "@radix-ui/react-popover";
import "./select-BYx0MCUK.js";
import "@radix-ui/react-select";
import "./use-lang-44ndmTOc.js";
import "date-fns";
import "./input-error-D1JIzedA.js";
import "./dialog-DGP_3dPQ.js";
import "@radix-ui/react-dialog";
import "./input-BsvJqbcd.js";
import "./label-0rIIfpX0.js";
import "@radix-ui/react-label";
import "./scroll-area-CDdrLubh.js";
import "@radix-ui/react-scroll-area";
import "richtor";
/* empty css                 */
const LiveClassStatus = ({ courseId, liveClass, zoomConfig }) => {
  var _a;
  const [currentTime, setCurrentTime] = useState(/* @__PURE__ */ new Date());
  const { props } = usePage();
  const { translate } = props;
  const { button, dashboard } = translate;
  const getClassStatus = (liveClass2) => {
    var _a2;
    const now = currentTime;
    const classStart = new Date(liveClass2.class_date_and_time);
    const classEnd = ((_a2 = liveClass2.additional_info) == null ? void 0 : _a2.end_time) ? new Date(liveClass2.additional_info.end_time) : new Date(classStart.getTime() + 60 * 60 * 1e3);
    if (now > classEnd) {
      return "ended";
    } else if (now >= classStart && now <= classEnd) {
      return "live";
    } else if (classStart > now) {
      return "upcoming";
    }
    return "scheduled";
  };
  const status = getClassStatus(liveClass);
  useEffect(() => {
    const updateTimeAndCheckStatus = () => {
      var _a2;
      const newTime = /* @__PURE__ */ new Date();
      setCurrentTime(newTime);
      const classEnd = ((_a2 = liveClass.additional_info) == null ? void 0 : _a2.end_time) ? new Date(liveClass.additional_info.end_time) : new Date(new Date(liveClass.class_date_and_time).getTime() + 60 * 60 * 1e3);
      return newTime > classEnd;
    };
    const hasEnded = updateTimeAndCheckStatus();
    if (hasEnded) {
      return;
    }
    const interval = setInterval(() => {
      const classEnded = updateTimeAndCheckStatus();
      if (classEnded) {
        clearInterval(interval);
      }
    }, 6e4);
    return () => {
      clearInterval(interval);
    };
  }, [liveClass.class_date_and_time, (_a = liveClass.additional_info) == null ? void 0 : _a.end_time]);
  const isSdkConfig = zoomConfig.zoom_web_sdk && zoomConfig.zoom_sdk_client_id && zoomConfig.zoom_sdk_client_secret ? true : false;
  const getStatusColor = (status2) => {
    switch (status2) {
      case "live":
        return "text-green-600 bg-green-100";
      case "upcoming":
        return "text-blue-600 bg-blue-100";
      case "ended":
        return "text-muted-foreground bg-gray-100";
      default:
        return "text-orange-600 bg-orange-100";
    }
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx("span", { className: `rounded-full px-2 py-1 text-center text-xs font-medium capitalize ${getStatusColor(status)}`, children: status }),
    status === "live" ? isSdkConfig ? /* @__PURE__ */ jsx(Link, { href: route("live-class.start", liveClass.id), children: /* @__PURE__ */ jsxs(Button, { size: "sm", variant: status === "live" ? "default" : "outline", className: "flex w-full items-center gap-2", children: [
      /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" }),
      button.join_class
    ] }) }) : /* @__PURE__ */ jsx("a", { href: liveClass.additional_info.join_url, target: "_blank", children: /* @__PURE__ */ jsxs(Button, { size: "sm", variant: status === "live" ? "default" : "outline", className: "flex w-full items-center gap-2", children: [
      /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" }),
      button.join_class
    ] }) }) : /* @__PURE__ */ jsxs(Button, { disabled: true, variant: "outline", size: "sm", className: "flex w-full items-center gap-2", children: [
      /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" }),
      button.join_class
    ] }),
    status === "upcoming" ? /* @__PURE__ */ jsx(
      LiveClassForm,
      {
        courseId,
        liveClass,
        title: button.edit_live_class,
        handler: /* @__PURE__ */ jsxs(
          Button,
          {
            size: "sm",
            variant: "secondary",
            className: "bg-background border-secondary-foreground/20 flex w-full items-center gap-2 border",
            children: [
              /* @__PURE__ */ jsx(Pencil, { className: "h-4 w-4" }),
              button.edit_live_class
            ]
          }
        )
      }
    ) : /* @__PURE__ */ jsxs(
      Button,
      {
        size: "sm",
        disabled: true,
        variant: "secondary",
        className: "bg-background border-secondary-foreground/20 flex w-full items-center gap-2 border",
        children: [
          /* @__PURE__ */ jsx(Pencil, { className: "h-4 w-4" }),
          button.edit_live_class
        ]
      }
    )
  ] });
};
export {
  LiveClassStatus as default
};
