"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 floating_container_exports = {};
__export(floating_container_exports, {
  FloatingContainer: () => FloatingContainer
});
module.exports = __toCommonJS(floating_container_exports);
var import_react_dom = require("@floating-ui/react-dom");
var import_react = __toESM(require("react"));
var import_ricos_context = require("./../../ricos-context/index.js");
var import_toolbar_boundary = require("./../../toolbars-v3/toolbar-boundary.js");
var import_ui_components = require("./../../ui-components/index.js");
var import_data_hooks = require("../data-hooks.js");
const FloatingContainer = ({
  children,
  modalConfig,
  onClickOutside
}) => {
  const { jsdomMode } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  const [modalElement, setModalElement] = (0, import_react.useState)(null);
  const referenceElement = modalConfig?.positioning?.referenceElement;
  const boundary = (0, import_toolbar_boundary.useToolbarBoundary)();
  const {
    x,
    y,
    refs: { setReference, setFloating },
    strategy,
    middlewareData,
    placement
  } = (0, import_react_dom.useFloating)({
    placement: modalConfig?.positioning?.placement,
    strategy: "fixed",
    middleware: [
      (0, import_react_dom.offset)({ mainAxis: 2 }),
      (0, import_react_dom.flip)({
        boundary: boundary || void 0
      }),
      (0, import_react_dom.shift)({
        crossAxis: true,
        mainAxis: false,
        limiter: (0, import_react_dom.limitShift)(),
        boundary: boundary || void 0
      })
    ],
    whileElementsMounted: (referenceEl, floatingEl, update) => (0, import_react_dom.autoUpdate)(referenceEl, floatingEl, update, {
      animationFrame: true
    })
  });
  (0, import_react.useEffect)(() => {
    setReference(referenceElement);
  }, [setReference, referenceElement]);
  const isVisible = jsdomMode || Boolean(x || y);
  const escToClickOutside = (e) => {
    if (e.key === "Escape") {
      onClickOutside(e);
      return;
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_ui_components.ClickOutside,
    {
      onClickOutside,
      triggerOn: typeof modalConfig?.onClickOutside !== "function" ? modalConfig?.onClickOutside?.on : void 0
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: (ref) => {
          setFloating(ref);
          setModalElement(ref);
        },
        onKeyDown: escToClickOutside,
        style: {
          width: referenceElement?.offsetWidth ? referenceElement.offsetWidth : "auto",
          position: strategy,
          top: y ?? "",
          left: x ?? "",
          zIndex: zIndexService.getZIndex("DIALOG"),
          display: "block",
          visibility: isVisible ? "visible" : "hidden"
        },
        "data-hook": import_data_hooks.FLOATING_CONTAINER_AI_WIDGET
      },
      modalElement && /* @__PURE__ */ import_react.default.createElement(import_ui_components.UiComponentsProvider, { containerEl: modalElement }, children({ middlewareData, placement }))
    )
  );
};
//# sourceMappingURL=floating-container.js.map
