"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/hooks/useDialog/index.ts
var useDialog_exports = {};
__export(useDialog_exports, {
  useDialog: () => useDialog
});
module.exports = __toCommonJS(useDialog_exports);

// src/hooks/useDialog/useDialog.ts
var import_react2 = require("react");

// src/utils/polyfill-tests.ts
var supportsDialog = () => typeof HTMLDialogElement === "function";

// src/hooks/useDialog/useDialog.ts
var import_mobius_hooks = require("@simplybusiness/mobius-hooks");

// src/hooks/useDialogPolyfill/useDialogPolyfill.ts
var import_react = require("react");
var useDialogPolyfill = () => {
  const [hasDialogSupport, setHasDialogSupport] = (0, import_react.useState)(false);
  const hasAlreadyLoaded = (0, import_react.useRef)(false);
  (0, import_react.useEffect)(() => {
    setHasDialogSupport(supportsDialog());
  }, []);
  const polyfillDialog = async (ref) => {
    if (!hasAlreadyLoaded.current && !hasDialogSupport && typeof window !== "undefined" && ref.current !== null) {
      const { default: dialogPolyfill } = await import("dialog-polyfill");
      hasAlreadyLoaded.current = true;
      try {
        if (ref.current) {
          dialogPolyfill.registerDialog(ref.current);
        }
      } catch (error) {
        console.error("Failed to load dialog-polyfill", error);
      }
    }
  };
  return {
    polyfillDialog
  };
};

// src/hooks/useDialog/useDialog.ts
var TRANSITION_CLASS_NAME = "--transition";
var FALLBACK_TRANSITION_DURATION = 0;
var useDialog = (props) => {
  const { ref, isOpen, transition, onOpen, onClose } = props;
  const [hasDialogSupport, setHasDialogSupport] = (0, import_react2.useState)(false);
  const shouldTransition = hasDialogSupport && transition.isEnabled;
  const { polyfillDialog } = useDialogPolyfill();
  (0, import_react2.useEffect)(() => {
    setHasDialogSupport(supportsDialog());
  }, []);
  const TRANSITION_DURATION_IN_MS = ref.current && Number(
    getComputedStyle(ref.current).getPropertyValue(transition.CSSVariable).replace("ms", "")
  ) || FALLBACK_TRANSITION_DURATION;
  (0, import_mobius_hooks.useBodyScrollLock)({ enabled: isOpen });
  const open = (0, import_react2.useCallback)(() => {
    ref.current?.showModal();
    onOpen?.();
  }, [onOpen, ref]);
  const close = (0, import_react2.useCallback)(() => {
    ref.current?.close();
    onClose?.();
  }, [onClose, ref]);
  const handleClose = (0, import_react2.useCallback)(
    (event) => {
      if (event) {
        event.preventDefault();
        event.stopPropagation();
      }
      if (shouldTransition) {
        ref.current?.classList.remove(TRANSITION_CLASS_NAME);
        setTimeout(() => close(), TRANSITION_DURATION_IN_MS);
      } else {
        close();
      }
    },
    [TRANSITION_DURATION_IN_MS, close, ref, shouldTransition]
  );
  (0, import_react2.useEffect)(() => {
    async function toggleDialog() {
      if (isOpen && !ref.current?.open) {
        await polyfillDialog(ref);
        open();
        setTimeout(() => {
          ref.current?.classList.add(TRANSITION_CLASS_NAME);
        }, 0);
      } else if (!isOpen && ref.current?.open) {
        handleClose();
      }
    }
    void toggleDialog();
  }, [handleClose, isOpen, open, ref, polyfillDialog]);
  return {
    open,
    close: handleClose
  };
};
//# sourceMappingURL=index.js.map
