"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 row_resizing_exports = {};
__export(row_resizing_exports, {
  getStartRowResizeDrag: () => getStartRowResizeDrag,
  rowResizingPlugin: () => rowResizingPlugin
});
module.exports = __toCommonJS(row_resizing_exports);
var import_get_element_scale = require("./../../../_shared/get-element-scale.js");
var import_resize_drag_controller = require("./resize-drag-controller.js");
var import_resizing_plugin_keys = require("./resizing-plugin-keys.js");
const startRowResizeDragByEditor = /* @__PURE__ */ new WeakMap();
const getStartRowResizeDrag = (editor) => startRowResizeDragByEditor.get(editor);
const rowResizingPlugin = ({ editor }) => {
  let dragRowElement = null;
  let dragNextRowElement = null;
  let latestRowNewHeight = 0;
  let latestNextRowNewHeight = void 0;
  const controller = (0, import_resize_drag_controller.createResizeDragController)(editor, {
    pluginKey: import_resizing_plugin_keys.rowResizingPluginKey,
    setActive: import_resizing_plugin_keys.setRowResizeDragActive,
    isOtherActive: import_resizing_plugin_keys.isColumnResizingActive,
    begin(view, { tablePosition, rowInfo, nextRowInfo, event }) {
      const dragRowInfo = getDragRowInfo(view, rowInfo);
      const dragNextRowInfo = nextRowInfo ? getDragRowInfo(view, nextRowInfo) : void 0;
      const rowDom = view.nodeDOM(dragRowInfo.position);
      if (!rowDom) {
        return null;
      }
      const scale = (0, import_get_element_scale.getElementScale)(rowDom);
      dragRowElement = rowDom;
      dragNextRowElement = dragNextRowInfo ? view.nodeDOM(dragNextRowInfo.position) : null;
      latestRowNewHeight = dragRowInfo.newHeight;
      latestNextRowNewHeight = dragNextRowInfo?.newHeight;
      return {
        rowInfo: dragRowInfo,
        initialTotalHeight: dragRowInfo.newHeight + (dragNextRowInfo?.newHeight ?? 0),
        initialRowHeight: dragRowInfo.newHeight,
        nextRowInfo: dragNextRowInfo,
        tablePosition,
        startY: event.pageY / scale.scaleY,
        scale
      };
    },
    applyFrame(view, event, dragState) {
      const scaledPageY = event.pageY / dragState.scale.scaleY;
      const deltaY = scaledPageY - dragState.startY;
      const newRowHeight = Math.max(
        dragState.initialRowHeight + deltaY,
        dragState.rowInfo.minHeight
      );
      const nextRowPotentialHeight = dragState.initialTotalHeight - newRowHeight;
      const newNextRowHeight = dragState.nextRowInfo ? Math.max(nextRowPotentialHeight, dragState.nextRowInfo.minHeight) : void 0;
      latestRowNewHeight = newRowHeight;
      latestNextRowNewHeight = newNextRowHeight;
      if (dragRowElement) {
        dragRowElement.style.height = `${newRowHeight}px`;
      }
      if (dragNextRowElement && newNextRowHeight !== void 0) {
        dragNextRowElement.style.height = `${newNextRowHeight}px`;
      }
      (0, import_resize_drag_controller.triggerResizeTick)(view);
      event.preventDefault();
      event.stopPropagation();
    },
    commit(view, dragState) {
      const finalRowHeight = latestRowNewHeight;
      const finalNextRowHeight = latestNextRowNewHeight;
      dragRowElement = null;
      dragNextRowElement = null;
      const tr = view.state.tr.setMeta("pointer", true);
      tr.setNodeAttribute(dragState.rowInfo.position, "height", finalRowHeight);
      if (dragState.nextRowInfo && finalNextRowHeight !== void 0) {
        tr.setNodeAttribute(dragState.nextRowInfo.position, "height", finalNextRowHeight);
      }
      view.dispatch(tr);
    }
  });
  startRowResizeDragByEditor.set(editor, controller.start);
  return controller.plugin;
};
const getDragRowInfo = (view, { position, index }) => {
  const rowElement = view.nodeDOM(position);
  const minHeight = rowElement ? calculateMinRowHeight(rowElement) : 0;
  return {
    position,
    index,
    newHeight: Math.max(rowElement?.offsetHeight ?? 0, minHeight),
    minHeight
  };
};
function calculateMinRowHeight(rowElement) {
  const originalHeight = rowElement.style.height;
  rowElement.style.height = "0px";
  const minHeight = rowElement.offsetHeight;
  rowElement.style.height = originalHeight;
  return Math.max(minHeight, 0);
}
//# sourceMappingURL=row-resizing.js.map
