"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 use_selection_to_position_exports = {};
__export(use_selection_to_position_exports, {
  FLOATING_ADD_PLUGIN_MENU_BUTTON_HEIGHT: () => FLOATING_ADD_PLUGIN_MENU_BUTTON_HEIGHT,
  clampButtonTop: () => clampButtonTop,
  useSelectionToPosition: () => useSelectionToPosition
});
module.exports = __toCommonJS(use_selection_to_position_exports);
var import_react = require("react");
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_tiptap = require("./../../../tiptap/index.js");
function posToDOMRect(view, pos) {
  const minPos = 0;
  const maxPos = view.state.doc.content.size;
  const resolvedFrom = (0, import_tiptap.minMax)(pos, minPos, maxPos);
  const side = view.state.selection.anchor - view.state.selection.head;
  const start = view.coordsAtPos(resolvedFrom, side);
  const top = start.top;
  const bottom = start.bottom;
  const left = start.left;
  const right = start.right;
  const width = right - left;
  const height = bottom - top;
  const x = left;
  const y = top;
  const data = {
    top,
    bottom,
    left,
    right,
    width,
    height,
    x,
    y
  };
  return {
    ...data,
    toJSON: () => data
  };
}
const FLOATING_ADD_PLUGIN_MENU_BUTTON_HEIGHT = 20;
const clampButtonTop = (relativeTop, containerScrollHeight, selectionHeight, buttonHeight = FLOATING_ADD_PLUGIN_MENU_BUTTON_HEIGHT) => {
  const centeringOffset = selectionHeight / 2 - buttonHeight / 2;
  const minTop = -centeringOffset;
  const maxRenderedTop = Math.max(0, containerScrollHeight - buttonHeight);
  const maxTop = Math.max(minTop, maxRenderedTop - centeringOffset);
  return (0, import_tiptap.minMax)(relativeTop, minTop, maxTop);
};
function useSelectionToPosition(editor) {
  const [position, setPosition] = (0, import_react.useState)();
  const pluginsEvents = (0, import_react.useContext)(import_ricos_context.PluginsEventsContext);
  const handleButtonPosition = () => {
    const { selection } = editor.state;
    const currentPosition = posToDOMRect(editor.view, selection.head);
    const node = editor.view.domAtPos(selection.head)?.node;
    const element = node && node.nodeType === 3 ? node.parentElement : node;
    const container = element ? element.closest("[contenteditable=true]") : editor.view.dom;
    const containerRect = container?.getBoundingClientRect() || { top: 0 };
    const containerScrollHeight = container?.scrollHeight || 0;
    const relativeTop = currentPosition.top - containerRect.top;
    const clampedTop = clampButtonTop(relativeTop, containerScrollHeight, currentPosition.height);
    setPosition(
      new DOMRect(currentPosition.left, clampedTop, currentPosition.width, currentPosition.height)
    );
  };
  (0, import_react.useEffect)(() => {
    handleButtonPosition();
    editor.on("transaction", handleButtonPosition);
    const subscription = pluginsEvents.onPluginLayoutShift(() => {
      handleButtonPosition();
    });
    return () => {
      editor.off("transaction", handleButtonPosition);
      subscription.cancel();
    };
  }, []);
  return position;
}
//# sourceMappingURL=use-selection-to-position.js.map
