"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 input_exports = {};
__export(input_exports, {
  handleMouseDown: () => handleMouseDown,
  handlePaste: () => handlePaste
});
module.exports = __toCommonJS(input_exports);
var import_model = require("./../../../tiptap/pm/model.js");
var import_transform = require("./../../../tiptap/pm/transform.js");
var import_queries = require("../commands/queries.js");
var import_cellselection = require("../plugins/cellselection.js");
var import_tablemap = require("../tablemap.js");
var import_copypaste = require("../utilities/copypaste.js");
var import_is_selection_type = require("../utilities/is-selection-type.js");
function handlePaste(view, _, slice) {
  if (!(0, import_queries.isInTable)(view.state)) {
    return false;
  }
  let cells = (0, import_copypaste.pastedCells)(slice);
  const sel = view.state.selection;
  if ((0, import_is_selection_type.isRicosCellSelection)(sel)) {
    if (!cells) {
      cells = {
        width: 1,
        height: 1,
        rows: [import_model.Fragment.from(fitSlice((0, import_queries.tableNodeTypes)(view.state.schema).cell, slice))]
      };
    }
    const start = sel.$anchorCell.start(-1);
    const cellsArray = [];
    sel.forEachCell((_node, pos) => {
      cellsArray.push(view.state.doc.resolve(pos));
    });
    const rect = (0, import_cellselection.getRectBetweenCells)(cellsArray);
    cells = (0, import_copypaste.clipCells)(cells, rect.right - rect.left, rect.bottom - rect.top);
    (0, import_copypaste.insertCells)(view.state, view.dispatch, start, rect, cells);
    return true;
  } else if (cells) {
    const $cell = (0, import_queries.selectionCell)(view.state);
    if ($cell) {
      const start = $cell.start(-1);
      const { col, row, colspan, rowspan } = import_tablemap.TableMap.get($cell.node(-1)).findCell(
        $cell.pos - start
      );
      (0, import_copypaste.insertCells)(
        view.state,
        view.dispatch,
        start,
        { left: col, top: row, right: col + colspan, bottom: row + rowspan },
        cells
      );
      return true;
    }
  }
  return false;
}
function handleMouseDown(view, startEvent, key, isMobile) {
  if (isMobile || startEvent.button !== 0) {
    return;
  }
  const startDOMCell = domInCell(view, startEvent.target);
  if ((startEvent.ctrlKey || startEvent.metaKey) && startDOMCell) {
    const $cell = cellUnderMouse(view, startEvent);
    if ($cell) {
      const newSelection = import_cellselection.RicosCellSelection.toggleCell(
        view.state.selection,
        $cell.pos,
        view.state.doc
      );
      view.dispatch(view.state.tr.setSelection(newSelection));
      startEvent.preventDefault();
    }
    return;
  }
  let $anchor;
  if (startEvent.shiftKey && (0, import_is_selection_type.isRicosCellSelection)(view.state.selection)) {
    setCellSelection(view.state.selection.$anchorCell, startEvent);
    startEvent.preventDefault();
  } else if (startEvent.shiftKey && startDOMCell && ($anchor = (0, import_queries.cellAround)(view.state.selection.$anchor)) !== null && cellUnderMouse(view, startEvent)?.pos !== $anchor.pos) {
    setCellSelection($anchor, startEvent);
    startEvent.preventDefault();
  } else if (!startDOMCell) {
    return;
  }
  const { selection } = view.state;
  if (!(0, import_is_selection_type.isRicosCellSelection)(selection) && !selection.empty) {
    const clickPos = view.posAtCoords({ left: startEvent.clientX, top: startEvent.clientY });
    if (clickPos && clickPos.pos >= selection.from && clickPos.pos <= selection.to) {
      return;
    }
  }
  const pointerId = startEvent.pointerId;
  const captureTarget = startEvent.target;
  if (pointerId !== void 0 && captureTarget?.setPointerCapture) {
    captureTarget.setPointerCapture(pointerId);
  }
  function isSameCellSelection($anchor2, $head) {
    const { selection: selection2 } = view.state;
    if (!(0, import_is_selection_type.isRicosCellSelection)(selection2)) {
      return false;
    }
    return selection2.$anchorCell.pos === $anchor2.pos && selection2.$headCell.pos === $head.pos;
  }
  function setCellSelection($anchor2, event) {
    const textSelection = view.root.getSelection?.();
    if (textSelection) {
      textSelection.removeAllRanges();
    }
    let $head = cellUnderMouse(view, event);
    const starting = key.getState(view.state) === null || key.getState(view.state) === void 0;
    if (!$head || !(0, import_cellselection.inSameTable)($anchor2, $head)) {
      if (starting) {
        $head = $anchor2;
      } else {
        return;
      }
    }
    if (isSameCellSelection($anchor2, $head)) {
      return;
    }
    const rect = (0, import_cellselection.getRectBetweenCells)([$anchor2, $head]);
    const table = $anchor2.node(-1);
    const tableStart = $anchor2.start(-1);
    const map = import_tablemap.TableMap.get(table);
    const cellPositions = map.cellsInRect(rect);
    const cells = cellPositions.map((pos) => view.state.doc.resolve(tableStart + pos));
    const selection2 = new import_cellselection.RicosCellSelection(cells, view.state.doc);
    if (starting || !view.state.selection.eq(selection2)) {
      const tr = view.state.tr.setSelection(selection2);
      if (starting) {
        tr.setMeta(key, $anchor2.pos);
      }
      view.dispatch(tr);
    }
  }
  let lastHeadDOMCell = null;
  function stop() {
    view.root.removeEventListener("pointerup", stop);
    view.root.removeEventListener("pointermove", move);
    if (pointerId !== void 0 && captureTarget?.releasePointerCapture) {
      try {
        captureTarget.releasePointerCapture(pointerId);
      } catch {
      }
    }
    lastHeadDOMCell = null;
    if (key.getState(view.state)) {
      view.dispatch(view.state.tr.setMeta(key, -1).setMeta("forceToolbarUpdate", true));
    }
  }
  function move(event) {
    const elementUnderPointer = document.elementFromPoint(event.clientX, event.clientY) || event.target;
    const currentDOMCell = domInCell(view, elementUnderPointer);
    if (currentDOMCell === lastHeadDOMCell) {
      return;
    }
    const anchor = key.getState(view.state);
    let $anchor2;
    if (anchor !== null) {
      $anchor2 = view.state.doc.resolve(anchor);
    } else if (currentDOMCell !== startDOMCell) {
      $anchor2 = cellUnderMouse(view, startEvent);
      if (!$anchor2) {
        return stop();
      }
    }
    if ($anchor2) {
      setCellSelection($anchor2, event);
      lastHeadDOMCell = currentDOMCell;
    }
  }
  view.root.addEventListener("pointerup", stop);
  view.root.addEventListener("pointermove", move);
}
function domInCell(view, dom) {
  for (; dom && dom !== view.dom; dom = dom.parentNode) {
    if (dom.nodeName === "TD" || dom.nodeName === "TH") {
      return dom;
    }
  }
}
function cellUnderMouse(view, event) {
  const elementUnderPointer = document.elementFromPoint(event.clientX, event.clientY) || event.target;
  const cell = domInCell(view, elementUnderPointer);
  if (!cell) {
    return null;
  }
  return view.state.doc.resolve(view.posAtDOM(cell, 0) - 1);
}
function fitSlice(nodeType, slice) {
  const node = nodeType.createAndFill();
  const tr = new import_transform.Transform(node).replace(0, node.content.size, slice);
  return tr.doc;
}
//# sourceMappingURL=input.js.map
