"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_toolbar_exports = {};
__export(floating_toolbar_exports, {
  FloatingToolbar: () => FloatingToolbar
});
module.exports = __toCommonJS(floating_toolbar_exports);
var import_react_dom = require("@floating-ui/react-dom");
var import_react = __toESM(require("react"));
var import_react_dom2 = __toESM(require("react-dom"));
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_use_floating_toolbar_offset = require("./use-floating-toolbar-offset.js");
const FloatingToolbar = ({
  children,
  hideStrategy,
  referenceElement,
  placement,
  boundary
}) => {
  const zIndexService = (0, import_react.useContext)(import_ricos_context.ZIndexContext);
  const { jsdomMode, portal } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const toolbarOffset = (0, import_use_floating_toolbar_offset.useFloatingToolbarOffset)({ referenceElement });
  const {
    x,
    y,
    refs: { setReference, setFloating },
    strategy,
    middlewareData
  } = (0, import_react_dom.useFloating)({
    placement,
    middleware: [
      (0, import_react_dom.shift)({
        boundary
      }),
      (0, import_react_dom.hide)({
        strategy: hideStrategy,
        ...hideStrategy !== "referenceHidden" && { boundary }
      }),
      (0, import_react_dom.offset)(toolbarOffset),
      (0, import_react_dom.flip)({
        boundary
      })
    ],
    whileElementsMounted: (referenceEl, floatingEl, update) => (0, import_react_dom.autoUpdate)(referenceEl, floatingEl, update, {
      animationFrame: true
      // this fix issue of reposition when scrolling
    })
  });
  (0, import_react.useEffect)(() => {
    setReference(referenceElement);
  }, [setReference, referenceElement]);
  if (!portal) {
    return null;
  }
  return import_react_dom2.default.createPortal(
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        ref: setFloating,
        style: {
          position: strategy,
          top: y ?? "",
          left: x ?? "",
          zIndex: zIndexService.getZIndex("TOOLBAR"),
          display: jsdomMode ? "block" : middlewareData.hide?.escaped || middlewareData.hide?.referenceHidden || !x && !y ? "none" : "block"
        }
      },
      /* @__PURE__ */ import_react.default.createElement("div", { "data-hook": "ricos-floating-toolbar", tabIndex: -1 }, children)
    ),
    portal
  );
};
//# sourceMappingURL=floating-toolbar.js.map
