"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var toolbar_reference_element_exports = {};
__export(toolbar_reference_element_exports, {
  getToolbarReferenceElement: () => getToolbarReferenceElement
});
module.exports = __toCommonJS(toolbar_reference_element_exports);
var import_tiptap = require("./../tiptap/index.js");
const getToolbarReferenceElement = (tiptapEditor, options) => {
  const closest = (0, import_tiptap.findParentNodeClosestToPos)(
    tiptapEditor.state.selection.$from,
    (node) => node.type.name === options.nodeType
  );
  const startNodePos = closest ? tiptapEditor.$pos(closest.pos) : tiptapEditor.state.selection.$from;
  const nodeElement = tiptapEditor.view.nodeDOM(startNodePos.pos);
  const handleElement = nodeElement?.querySelector?.(options.handleSelector);
  if (nodeElement && handleElement) {
    return {
      getBoundingClientRect: () => {
        const containerElement = options.containerSelector ? nodeElement.querySelector(options.containerSelector) ?? nodeElement.closest(options.containerSelector) ?? nodeElement : nodeElement;
        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) + (options.topOffset ?? 0) * -1 + (isNaN(borderTopWidth) ? 0 : borderTopWidth),
          nodeElementRect.width,
          nodeElementRect.height + handleElementRect.height + (options.topOffset ?? 0) + (options.bottomOffset ?? 0)
        );
      },
      contextElement: nodeElement
    };
  }
  return tiptapEditor.view.nodeDOM(startNodePos.pos - 1);
};
//# sourceMappingURL=toolbar-reference-element.js.map
