"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactUtils = require("@contember/react-utils");
const ui = require("@contember/ui");
const react = require("react");
const reactI18n = require("@contember/react-i18n");
const outdatedApplicationDictionary = require("./outdatedApplicationDictionary.cjs");
const postponeTimeoutMs = 6e4 * 5;
const checkIntervalMs = 3e4;
const OutdatedApplicationChecker = () => {
  const modal = ui.useDialog();
  const isOutdated = useIsOutdated();
  const isOpen = react.useRef(false);
  const isMounted = reactUtils.useIsMounted();
  const openDialog = react.useCallback(async () => {
    if (isOpen.current || !isMounted.current) {
      return;
    }
    isOpen.current = true;
    const result = await modal.openDialog({
      content: ({ resolve }) => /* @__PURE__ */ jsxRuntime.jsx(OutdatedApplicationDialog, { onReload: () => resolve(true), onPostpone: () => resolve(false) })
    });
    isOpen.current = false;
    if (!isMounted.current) {
      return;
    }
    if (result) {
      window.location.reload();
    } else {
      setTimeout(openDialog, postponeTimeoutMs);
    }
  }, [isMounted, modal]);
  react.useEffect(() => {
    if (!isOutdated) {
      return;
    }
    openDialog();
  }, [isOutdated, openDialog]);
  return null;
};
const OutdatedApplicationDialog = ({ onReload, onPostpone }) => {
  const formatMessage = reactI18n.useMessageFormatter(outdatedApplicationDictionary.outdatedApplicationDictionary);
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { children: [
    /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: formatMessage("outdatedApplication.heading") }),
    /* @__PURE__ */ jsxRuntime.jsx("p", { children: formatMessage("outdatedApplication.text") }),
    /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { horizontal: true, gap: "gap", justify: "end", children: [
      /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: onReload, intent: "success", children: formatMessage("outdatedApplication.reloadButton") }),
      /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { onClick: onPostpone, intent: "warn", children: formatMessage("outdatedApplication.postponeButton") })
    ] })
  ] });
};
const getVersionFromDocument = (node) => {
  const meta = node.querySelector("meta[name=contember-build-version]");
  return meta?.content;
};
const useFetchVersion = () => {
  return react.useCallback(async () => {
    try {
      const result = await (await fetch(window.location.href)).text();
      const html = document.createElement("html");
      html.innerHTML = result;
      return getVersionFromDocument(html);
    } catch (e) {
      console.error(e);
      return void 0;
    }
  }, []);
};
const useIsOutdated = () => {
  const [initialVersion] = react.useState(() => {
    return getVersionFromDocument(document.head);
  });
  const [isOutdated, setIsOutdated] = react.useState(false);
  const fetchVersion = useFetchVersion();
  const checkVersion = react.useCallback(async () => {
    if (document.hidden) {
      return;
    }
    const version = await fetchVersion();
    if (version !== initialVersion) {
      setIsOutdated(true);
    }
  }, [fetchVersion, initialVersion]);
  const shouldCheck = !!initialVersion && !isOutdated;
  react.useEffect(() => {
    if (!shouldCheck) {
      return;
    }
    const handle = setInterval(checkVersion, checkIntervalMs);
    return () => clearInterval(handle);
  }, [checkVersion, shouldCheck]);
  react.useEffect(() => {
    if (!shouldCheck) {
      return;
    }
    document.addEventListener("visibilitychange", checkVersion);
    return () => document.removeEventListener("visibilitychange", checkVersion);
  }, [checkVersion, shouldCheck]);
  return isOutdated;
};
exports.OutdatedApplicationChecker = OutdatedApplicationChecker;
//# sourceMappingURL=OutdatedApplicationChecker.cjs.map
