import { jsx, jsxs } from "react/jsx-runtime";
import { memo, useEffect, useState, useRef } from "react";
import { DSButtonV2 } from "@elliemae/ds-button";
import { WarningCircle } from "@elliemae/ds-icons";
import { useFocusTrap } from "@elliemae/ds-utilities";
import {
  DSDialog,
  DSDialogDefaultLayout,
  DSDialogPrimaryMessage,
  DSDialogSecondaryMessage,
  DSDialogBody,
  DSDialogFooter,
  DSDialogSeparator
} from "@elliemae/ds-dialog";
import { useAppDispatch } from "../../../data/react.js";
import { logout } from "../../../data/logout/actions.js";
import { addEventListeners, renewSession } from "../../../utils/session.js";
import { useTrackSessionExpiry } from "./customHooks.js";
const SessionExpiry = memo(
  ({ open, warningNotifiedAt = 0 }) => {
    const [isOpen, setIsOpen] = useState(open);
    const timeLeft = useTrackSessionExpiry(warningNotifiedAt);
    const dispatch = useAppDispatch();
    const firstElementRef = useRef(null);
    const lastElementRef = useRef(null);
    const handleOnKeyDown = useFocusTrap({ firstElementRef, lastElementRef });
    useEffect(() => {
      setIsOpen(open);
    }, [open]);
    const resetSession = async () => {
      setIsOpen(false);
      await renewSession();
      addEventListeners();
      dispatch(logout.cancel());
    };
    const logoutSession = () => {
      setIsOpen(false);
      dispatch(logout.confirm());
    };
    return timeLeft ? /* @__PURE__ */ jsxs(DSDialog, { onKeyDown: handleOnKeyDown, isOpen, size: "small", children: [
      /* @__PURE__ */ jsx(DSDialogBody, { p: "s", children: /* @__PURE__ */ jsxs(DSDialogDefaultLayout, { children: [
        /* @__PURE__ */ jsx(WarningCircle, { size: "xl", color: ["warning", "900"] }),
        /* @__PURE__ */ jsx(DSDialogPrimaryMessage, { children: `Your session will expire in ${timeLeft}` }),
        /* @__PURE__ */ jsx(DSDialogSecondaryMessage, { children: "Please logout or reset your session" })
      ] }) }),
      /* @__PURE__ */ jsx(DSDialogSeparator, {}),
      /* @__PURE__ */ jsxs(DSDialogFooter, { children: [
        /* @__PURE__ */ jsx(
          DSButtonV2,
          {
            innerRef: firstElementRef,
            buttonType: "outline",
            onClick: logoutSession,
            children: "Logout"
          }
        ),
        /* @__PURE__ */ jsx(DSButtonV2, { innerRef: lastElementRef, onClick: resetSession, children: "Reset" })
      ] })
    ] }) : null;
  }
);
export {
  SessionExpiry
};
