import { useState as E, useCallback as c, useEffect as f } from "react";
const h = 10080 * 60 * 1e3, l = "healthspanClosedModalList", M = () => {
  try {
    return JSON.parse(localStorage.getItem(l) || "{}");
  } catch {
    return {};
  }
}, O = (e) => {
  localStorage.setItem(l, JSON.stringify(e));
}, i = (e) => {
  const o = M()[e];
  return !o || Date.now() > o;
}, T = ({
  id: e,
  initiallyOpen: a = !1,
  trigger: o,
  onCloseCallback: d,
  onClickCallback: r
}) => {
  const [s, m] = E(a), u = c(() => {
    i(e) && m(!0);
  }, [e]), n = c(() => {
    m(!1);
    const t = M();
    t[e] = Date.now() + h, O(t), d == null || d();
  }, [e, d]), p = c(() => {
    r == null || r();
  }, [r]);
  f(() => {
    if ((o == null ? void 0 : o.type) === "timer" && !s) {
      const t = setTimeout(() => {
        u();
      }, o.delay * 1e3);
      return () => clearTimeout(t);
    }
  }, [o, s, u]), f(() => {
    if (!s) return;
    const t = window.getComputedStyle(document.body).overflow;
    document.body.style.overflow = "hidden";
    const y = (w) => {
      w.key === "Escape" && n();
    };
    return document.addEventListener("keydown", y), () => {
      document.body.style.overflow = t, document.removeEventListener("keydown", y);
    };
  }, [s, n, e]);
  const S = c(
    (t) => {
      t.target === t.currentTarget && n();
    },
    [n]
  );
  return {
    isOpen: s,
    openModal: u,
    closeModal: n,
    handleClick: p,
    handleOverlayClick: S
  };
};
export {
  l as CLOSED_MODALS_KEY,
  h as REOPEN_TIMEOUT_MS,
  T as useHSModal
};
