"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var session_expiry_exports = {};
__export(session_expiry_exports, {
  SessionExpiry: () => SessionExpiry
});
module.exports = __toCommonJS(session_expiry_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ds_button = require("@elliemae/ds-button");
var import_ds_icons = require("@elliemae/ds-icons");
var import_ds_utilities = require("@elliemae/ds-utilities");
var import_ds_dialog = require("@elliemae/ds-dialog");
var import_react2 = require("../../../data/react.js");
var import_actions = require("../../../data/logout/actions.js");
var import_session = require("../../../utils/session.js");
var import_customHooks = require("./customHooks.js");
const SessionExpiry = (0, import_react.memo)(
  ({ open, warningNotifiedAt = 0 }) => {
    const [isOpen, setIsOpen] = (0, import_react.useState)(open);
    const timeLeft = (0, import_customHooks.useTrackSessionExpiry)(warningNotifiedAt);
    const dispatch = (0, import_react2.useAppDispatch)();
    const firstElementRef = (0, import_react.useRef)(null);
    const lastElementRef = (0, import_react.useRef)(null);
    const handleOnKeyDown = (0, import_ds_utilities.useFocusTrap)({ firstElementRef, lastElementRef });
    (0, import_react.useEffect)(() => {
      setIsOpen(open);
    }, [open]);
    const resetSession = async () => {
      setIsOpen(false);
      await (0, import_session.renewSession)();
      (0, import_session.addEventListeners)();
      dispatch(import_actions.logout.cancel());
    };
    const logoutSession = () => {
      setIsOpen(false);
      dispatch(import_actions.logout.confirm());
    };
    return timeLeft ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ds_dialog.DSDialog, { onKeyDown: handleOnKeyDown, isOpen, size: "small", children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_dialog.DSDialogBody, { p: "s", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ds_dialog.DSDialogDefaultLayout, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_icons.WarningCircle, { size: "xl", color: ["warning", "900"] }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_dialog.DSDialogPrimaryMessage, { children: `Your session will expire in ${timeLeft}` }),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_dialog.DSDialogSecondaryMessage, { children: "Please logout or reset your session" })
      ] }) }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_dialog.DSDialogSeparator, {}),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ds_dialog.DSDialogFooter, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_ds_button.DSButtonV2,
          {
            innerRef: firstElementRef,
            buttonType: "outline",
            onClick: logoutSession,
            children: "Logout"
          }
        ),
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ds_button.DSButtonV2, { innerRef: lastElementRef, onClick: resetSession, children: "Reset" })
      ] })
    ] }) : null;
  }
);
