"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 table_editing_plugin_exports = {};
__export(table_editing_plugin_exports, {
  isSelectingCells: () => isSelectingCells,
  selectingCellsKey: () => selectingCellsKey,
  tableEditingPlugin: () => tableEditingPlugin
});
module.exports = __toCommonJS(table_editing_plugin_exports);
var import_state = require("./../../../tiptap/pm/state.js");
var import_fix = require("../commands/fix.js");
var import_tablemap = require("../tablemap.js");
var import_find_table = require("../utilities/find-table.js");
var import_is_selection_type = require("../utilities/is-selection-type.js");
var import_cellselection = require("./cellselection.js");
var import_input = require("./input.js");
const selectingCellsKey = new import_state.PluginKey("selectingCells");
const isSelectingCells = (state) => {
  return !!(selectingCellsKey.getState(state) !== null && selectingCellsKey.getState(state) !== void 0);
};
const tableEditingPlugin = ({
  editor,
  isMobile
}) => {
  const key = selectingCellsKey;
  return new import_state.Plugin({
    key,
    // This piece of state is used to remember when a mouse-drag
    // cell-selection is happening, so that it can continue even as
    // transactions (which might move its anchor cell) come in.
    state: {
      init() {
        return null;
      },
      apply(tr, cur) {
        const set = tr.getMeta(key);
        if (set !== null && set !== void 0) {
          return set === -1 ? null : set;
        }
        if (cur === null || !tr.docChanged) {
          return cur;
        }
        const { deleted, pos } = tr.mapping.mapResult(cur);
        return deleted ? null : pos;
      }
    },
    props: {
      decorations: import_cellselection.drawCellSelection,
      handleDOMEvents: {
        mousedown: (view, event) => {
          (0, import_input.handleMouseDown)(view, event, key, isMobile);
          return false;
        }
      },
      createSelectionBetween(view, $anchor, $head) {
        if (key.getState(view.state) !== null) {
          return view.state.selection;
        }
        const selection = view.state.selection;
        if ((0, import_is_selection_type.isRicosCellSelection)(selection) && !selection.isSingleCell()) {
          if ($anchor && $head && $anchor.pos === $head.pos) {
            return null;
          }
          return selection;
        }
        return null;
      },
      handleKeyDown: (view, event) => {
        if ((0, import_is_selection_type.isRicosCellSelection)(view.state.selection) && !event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey && event.key !== "Escape") {
          editor.commands.setEditCell();
        }
        return false;
      },
      handlePaste: import_input.handlePaste
    },
    appendTransaction(transactions, oldState, state) {
      let tr = (0, import_fix.fixTables)(state, oldState);
      const newSel = state.selection;
      const tableContext = (0, import_find_table.findTable)(newSel);
      if (tableContext) {
        const tableEnd = tableContext.pos + tableContext.node.nodeSize;
        const isSelectionBeyondTable = newSel.from < tableContext.pos || newSel.to > tableEnd;
        if (isSelectionBeyondTable) {
          return tr;
        }
      }
      const isKeyboardNavigation = transactions.some((t) => {
        const meta = t.getMeta("pointer");
        return meta === void 0;
      });
      if (isKeyboardNavigation && tableContext) {
        const oldSel = oldState.selection;
        const wasInTable = (0, import_find_table.findTable)(oldSel);
        if (!wasInTable && newSel instanceof import_state.TextSelection && !(0, import_is_selection_type.isRicosCellSelection)(newSel)) {
          const table = tableContext.node;
          const tableStart = tableContext.start;
          const map = import_tablemap.TableMap.get(table);
          const rect = { left: 0, right: map.width, top: 0, bottom: map.height };
          const cellPositions = map.cellsInRect(rect);
          const cells = cellPositions.map((pos) => state.doc.resolve(tableStart + pos));
          const tableSelection = new import_cellselection.RicosCellSelection(cells, state.doc);
          if (!tr) {
            tr = state.tr;
          }
          tr?.setSelection(tableSelection);
        }
      }
      return (0, import_cellselection.normalizeSelection)(state, tr);
    }
  });
};
//# sourceMappingURL=table-editing-plugin.js.map
