import { jsxDEV } from "react/jsx-dev-runtime";
import { useIsMounted } from "@contember/react-utils";
import { useDialog, Stack, Heading, Button } from "@contember/ui";
import { useRef, useCallback, useEffect, useState } from "react";
import { useMessageFormatter } from "@contember/react-i18n";
import { outdatedApplicationDictionary } from "./outdatedApplicationDictionary.js";
const postponeTimeoutMs = 6e4 * 5;
const checkIntervalMs = 3e4;
const OutdatedApplicationChecker = () => {
  const modal = useDialog();
  const isOutdated = useIsOutdated();
  const isOpen = useRef(false);
  const isMounted = useIsMounted();
  const openDialog = useCallback(async () => {
    if (isOpen.current || !isMounted.current) {
      return;
    }
    isOpen.current = true;
    const result = await modal.openDialog({
      content: ({ resolve }) => /* @__PURE__ */ jsxDEV(OutdatedApplicationDialog, { onReload: () => resolve(true), onPostpone: () => resolve(false) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
        lineNumber: 27,
        columnNumber: 5
      }, void 0)
    });
    isOpen.current = false;
    if (!isMounted.current) {
      return;
    }
    if (result) {
      window.location.reload();
    } else {
      setTimeout(openDialog, postponeTimeoutMs);
    }
  }, [isMounted, modal]);
  useEffect(() => {
    if (!isOutdated) {
      return;
    }
    openDialog();
  }, [isOutdated, openDialog]);
  return null;
};
const OutdatedApplicationDialog = ({ onReload, onPostpone }) => {
  const formatMessage = useMessageFormatter(outdatedApplicationDictionary);
  return /* @__PURE__ */ jsxDEV(Stack, { children: [
    /* @__PURE__ */ jsxDEV(Heading, { children: formatMessage("outdatedApplication.heading") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
      lineNumber: 56,
      columnNumber: 4
    }, void 0),
    /* @__PURE__ */ jsxDEV("p", { children: formatMessage("outdatedApplication.text") }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
      lineNumber: 57,
      columnNumber: 4
    }, void 0),
    /* @__PURE__ */ jsxDEV(Stack, { horizontal: true, gap: "gap", justify: "end", children: [
      /* @__PURE__ */ jsxDEV(Button, { onClick: onReload, intent: "success", children: formatMessage("outdatedApplication.reloadButton") }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
        lineNumber: 59,
        columnNumber: 5
      }, void 0),
      /* @__PURE__ */ jsxDEV(Button, { onClick: onPostpone, intent: "warn", children: formatMessage("outdatedApplication.postponeButton") }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
        lineNumber: 62,
        columnNumber: 5
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
      lineNumber: 58,
      columnNumber: 4
    }, void 0)
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/Application/OutdatedApplicationChecker.tsx",
    lineNumber: 55,
    columnNumber: 3
  }, void 0);
};
const getVersionFromDocument = (node) => {
  const meta = node.querySelector("meta[name=contember-build-version]");
  return meta?.content;
};
const useFetchVersion = () => {
  return 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] = useState(() => {
    return getVersionFromDocument(document.head);
  });
  const [isOutdated, setIsOutdated] = useState(false);
  const fetchVersion = useFetchVersion();
  const checkVersion = useCallback(async () => {
    if (document.hidden) {
      return;
    }
    const version = await fetchVersion();
    if (version !== initialVersion) {
      setIsOutdated(true);
    }
  }, [fetchVersion, initialVersion]);
  const shouldCheck = !!initialVersion && !isOutdated;
  useEffect(() => {
    if (!shouldCheck) {
      return;
    }
    const handle = setInterval(checkVersion, checkIntervalMs);
    return () => clearInterval(handle);
  }, [checkVersion, shouldCheck]);
  useEffect(() => {
    if (!shouldCheck) {
      return;
    }
    document.addEventListener("visibilitychange", checkVersion);
    return () => document.removeEventListener("visibilitychange", checkVersion);
  }, [checkVersion, shouldCheck]);
  return isOutdated;
};
export {
  OutdatedApplicationChecker
};
//# sourceMappingURL=OutdatedApplicationChecker.js.map
