"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);
var modal_popper_exports = {};
__export(modal_popper_exports, {
  ModalPopper: () => ModalPopper
});
module.exports = __toCommonJS(modal_popper_exports);
var import_classnames = __toESM(require("classnames"));
var import_emulate_tab = require("emulate-tab");
var import_react = __toESM(require("react"));
var import_react_dom = __toESM(require("react-dom"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_uncreachable = require("./../../_shared/uncreachable.js");
var import_dialog = require("../components/dialog.js");
var import_drawer = require("../components/drawer.js");
var import_floating = require("../components/floating.js");
var import_fullscreen = require("../components/fullscreen.js");
var import_popper_scss = __toESM(require("../statics/styles/popper-scss.js"));
const layoutMapper = {
  drawer: import_drawer.Drawer,
  fullscreen: import_fullscreen.Fullscreen,
  dialog: (props) => /* @__PURE__ */ import_react.default.createElement(import_dialog.Dialog, { ...props }),
  floating: import_floating.Floating,
  manual: ({ children }) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, children)
};
const useForceUpdate = () => {
  const [_, setValue] = (0, import_react.useState)(0);
  return () => setValue((value) => value + 1);
};
const ModalPopper = ({ modalConfig, setModalRef }) => {
  const [isVisible, setIsVisible] = (0, import_react.useState)(false);
  const forceUpdate = useForceUpdate();
  const modalService = (0, import_react.useContext)(import_ricos_context.ModalServiceContext) || {};
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  const {
    adapter: { tiptapEditor }
  } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  const { languageDir, portal, isMobile } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const stoppedKeyDownPropagationRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    tiptapEditor.on("update", forceUpdate);
    return () => {
      tiptapEditor.off("update", forceUpdate);
    };
  }, []);
  (0, import_react.useEffect)(() => {
    setIsVisible(true);
  }, []);
  const closeModal = () => modalService?.closeModal?.(modalConfig.id);
  const onKeyDown = (e) => {
    if (modalConfig.disableCloseOnEscape !== true && e.key === "Escape") {
      e.preventDefault();
      if (modalConfig.onClickOutside && typeof modalConfig.onClickOutside === "function") {
        modalConfig.onClickOutside(e);
        return;
      }
      e.stopPropagation();
      stoppedKeyDownPropagationRef.current = e.key;
      closeModal();
      return;
    }
    if (modalConfig.disableArrowNavigation) {
      return;
    }
    if (e.key === "ArrowDown") {
      (0, import_emulate_tab.emulateTab)();
      e.preventDefault();
    } else if (e.key === "ArrowUp") {
      import_emulate_tab.emulateTab.backwards();
      e.preventDefault();
    }
  };
  const onKeyUp = (e) => {
    if (stoppedKeyDownPropagationRef.current === e.key) {
      e.stopPropagation();
      stoppedKeyDownPropagationRef.current = null;
    }
  };
  const resolveLayout = (layout2) => {
    if (typeof layout2 === "object") {
      return isMobile ? layout2.mobile : layout2.desktop;
    }
    return layout2;
  };
  const layout = resolveLayout(modalConfig.layout);
  const layoutToZIndex = () => {
    switch (layout) {
      case "drawer":
        return "DRAWER";
      case "dialog":
      case "fullscreen":
      case "floating":
      case "manual":
        return "DIALOG";
      default:
        (0, import_uncreachable.unreachable)(layout);
        return "DIALOG";
    }
  };
  const getZIndex = () => zIndexService.getZIndex(layoutToZIndex());
  const ModalLayout = layoutMapper[layout];
  const ModalComponent = modalConfig.Component;
  if (!portal) {
    return null;
  }
  return import_react_dom.default.createPortal(
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        "data-hook": "modal-popper",
        dir: languageDir,
        onKeyDown,
        onKeyUp,
        className: (0, import_classnames.default)(import_popper_scss.default.container, {
          [import_popper_scss.default.transparent]: !isVisible && modalConfig.withTransition
        })
      },
      /* @__PURE__ */ import_react.default.createElement(ModalLayout, { closeModal, modalConfig, zIndex: getZIndex() }, /* @__PURE__ */ import_react.default.createElement(
        ModalComponent,
        {
          ref: setModalRef,
          ...{ closeModal, modalConfig, zIndex: getZIndex() },
          ...modalConfig.componentProps || {}
        }
      ))
    ),
    portal
  );
};
//# sourceMappingURL=modal-popper.js.map
