import { PmNodeType } from "./../../_shared/pm-node-types.js";
import { findParentNodeClosestToPos } from "./../../tiptap/core/index.js";
import { NodeSelection } from "./../../tiptap/pm/state.js";
const resolveSmartBlockReferenceElement = (tiptapEditor) => {
  const { selection } = tiptapEditor.state;
  if (selection instanceof NodeSelection && selection.node?.type.name === PmNodeType.SMART_BLOCK_CELL) {
    const cellId = selection.node.attrs.id;
    const cellElement = tiptapEditor.view.dom.querySelector(
      `[data-smart-block-cell="${cellId}"]`
    );
    if (!cellElement) {
      return null;
    }
    return {
      getBoundingClientRect: () => cellElement.getBoundingClientRect(),
      contextElement: cellElement,
      toolbarOffset: 16
    };
  }
  const HANDLE_SELECTOR = "[data-smart-block-handle]";
  const CONTAINER_SELECTOR = "[data-smart-block-container]";
  const closest = findParentNodeClosestToPos(
    tiptapEditor.state.selection.$from,
    (node) => node.type.name === PmNodeType.SMART_BLOCK
  );
  const startNodePos = closest ? tiptapEditor.$pos(closest.pos) : tiptapEditor.state.selection.$from;
  const nodeElement = tiptapEditor.view.nodeDOM(startNodePos.pos);
  const handleElement = nodeElement?.querySelector?.(HANDLE_SELECTOR);
  const containerElement = CONTAINER_SELECTOR ? nodeElement?.querySelector(CONTAINER_SELECTOR) ?? nodeElement?.closest(CONTAINER_SELECTOR) ?? nodeElement : nodeElement;
  if (containerElement && handleElement) {
    return {
      getBoundingClientRect: () => {
        const nodeElementRect = containerElement.getBoundingClientRect();
        const borderTopWidth = parseInt(
          getComputedStyle(containerElement).getPropertyValue("border-top-width")
        );
        const handleElementRect = handleElement.getBoundingClientRect();
        return new DOMRect(
          nodeElementRect.x,
          Math.min(handleElementRect.y, nodeElementRect.y) + +(isNaN(borderTopWidth) ? 0 : borderTopWidth),
          nodeElementRect.width,
          nodeElementRect.height + handleElementRect.height
        );
      },
      contextElement: containerElement,
      toolbarOffset: 16
    };
  }
  return tiptapEditor.view.nodeDOM(startNodePos.pos - 1);
};
export {
  resolveSmartBlockReferenceElement
};
//# sourceMappingURL=resolve-smart-block-reference-element.js.map
