import React from "react";
import { isNodeSpaced } from "./../../../ricos-common-plugins/gap-spacer/gap-spacer-nodes.js";
import { EditorContext } from "./../../../ricos-context/index.js";
const DEFAULT_OFFSET = 8;
const PLUGIN_NODE_OFFSET = 16;
const useFloatingToolbarOffset = ({
  referenceElement
}) => {
  const editorContext = React.useContext(EditorContext);
  const offset = React.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 = 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;
};
export {
  useFloatingToolbarOffset
};
//# sourceMappingURL=use-floating-toolbar-offset.js.map
