import { Plugin, PluginKey, TextSelection } from "./../../../tiptap/pm/state.js";
import { fixTables } from "../commands/fix.js";
import { TableMap } from "../tablemap.js";
import { findTable } from "../utilities/find-table.js";
import { isRicosCellSelection } from "../utilities/is-selection-type.js";
import { RicosCellSelection, drawCellSelection, normalizeSelection } from "./cellselection.js";
import { handleMouseDown, handlePaste } from "./input.js";
const selectingCellsKey = new PluginKey("selectingCells");
const isSelectingCells = (state) => {
  return !!(selectingCellsKey.getState(state) !== null && selectingCellsKey.getState(state) !== void 0);
};
const tableEditingPlugin = ({
  editor,
  isMobile
}) => {
  const key = selectingCellsKey;
  return new 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: drawCellSelection,
      handleDOMEvents: {
        mousedown: (view, event) => {
          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 (isRicosCellSelection(selection) && !selection.isSingleCell()) {
          if ($anchor && $head && $anchor.pos === $head.pos) {
            return null;
          }
          return selection;
        }
        return null;
      },
      handleKeyDown: (view, event) => {
        if (isRicosCellSelection(view.state.selection) && !event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey && event.key !== "Escape") {
          editor.commands.setEditCell();
        }
        return false;
      },
      handlePaste
    },
    appendTransaction(transactions, oldState, state) {
      let tr = fixTables(state, oldState);
      const newSel = state.selection;
      const tableContext = 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 = findTable(oldSel);
        if (!wasInTable && newSel instanceof TextSelection && !isRicosCellSelection(newSel)) {
          const table = tableContext.node;
          const tableStart = tableContext.start;
          const map = 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 RicosCellSelection(cells, state.doc);
          if (!tr) {
            tr = state.tr;
          }
          tr?.setSelection(tableSelection);
        }
      }
      return normalizeSelection(state, tr);
    }
  });
};
export {
  isSelectingCells,
  selectingCellsKey,
  tableEditingPlugin
};
//# sourceMappingURL=table-editing-plugin.js.map
