"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 use_floating_toolbar_offset_exports = {};
__export(use_floating_toolbar_offset_exports, {
  useFloatingToolbarOffset: () => useFloatingToolbarOffset
});
module.exports = __toCommonJS(use_floating_toolbar_offset_exports);
var import_react = __toESM(require("react"));
var import_gap_spacer_nodes = require("./../../../ricos-common-plugins/gap-spacer/gap-spacer-nodes.js");
var import_ricos_context = require("./../../../ricos-context/index.js");
const DEFAULT_OFFSET = 8;
const PLUGIN_NODE_OFFSET = 16;
const useFloatingToolbarOffset = ({
  referenceElement
}) => {
  const editorContext = import_react.default.useContext(import_ricos_context.EditorContext);
  const offset = import_react.default.useMemo(() => {
    const explicitOffset = referenceElement?.toolbarOffset;
    if (typeof explicitOffset === "number") {
      return explicitOffset;
    }
    if (!referenceElement?.getBoundingClientRect) {
      return DEFAULT_OFFSET;
    }
    if (!editorContext?.adapter?.tiptapEditor) {
      return DEFAULT_OFFSET;
    }
    const editor = editorContext.adapter.tiptapEditor;
    if (!editor.view || !editor.view.state?.doc) {
      return DEFAULT_OFFSET;
    }
    const view = editor.view;
    try {
      const rect = referenceElement.getBoundingClientRect();
      const centerX = rect.left + rect.width / 2;
      const centerY = rect.top + rect.height / 2;
      const coords = { left: centerX, top: centerY };
      const posInfo = view.posAtCoords(coords);
      if (!posInfo) {
        return DEFAULT_OFFSET;
      }
      const targetNode = view.state.doc.nodeAt(posInfo.pos - 1);
      if (targetNode?.type?.name) {
        const spacedNode = (0, import_gap_spacer_nodes.isNodeSpaced)(targetNode.type.name);
        return spacedNode ? PLUGIN_NODE_OFFSET : DEFAULT_OFFSET;
      }
      return DEFAULT_OFFSET;
    } catch (error) {
      console.warn("Error calculating floating toolbar offset:", error);
      return DEFAULT_OFFSET;
    }
  }, [referenceElement, editorContext]);
  return offset;
};
//# sourceMappingURL=use-floating-toolbar-offset.js.map
