"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 selection_exports = {};
__export(selection_exports, {
  deleteRowAtIndex: () => deleteRowAtIndex,
  selectColumnAtIndex: () => selectColumnAtIndex,
  selectColumnCmd: () => selectColumnCmd,
  selectColumns: () => selectColumns,
  selectRowAtIndex: () => selectRowAtIndex,
  selectRowCmd: () => selectRowCmd,
  selectRows: () => selectRows,
  selectWholeTable: () => selectWholeTable,
  setCellSelection: () => setCellSelection
});
module.exports = __toCommonJS(selection_exports);
var import_prosemirror_state = require("prosemirror-state");
var import_consts = require("../consts.js");
var import_cellselection = require("../plugins/cellselection.js");
var import_table_query = require("../table-query.js");
var import_mutations = require("./mutations.js");
var import_queries = require("./queries.js");
const setCellSelection = (pos) => (state, dispatch) => {
  const tr = state.tr;
  if (dispatch) {
    const selection = import_cellselection.RicosCellSelection.create(tr.doc, pos);
    tr.setMeta(import_consts.TRANSACTION_META_KEYS.SELECTED_CELL, { pos });
    tr.setSelection(selection);
    dispatch(tr);
  }
  return true;
};
const selectColumnAtIndex = (index) => (state, dispatch) => {
  const tr = state.tr;
  tr.setMeta(import_consts.TRANSACTION_META_KEYS.SELECT_COLUMN, index);
  const rect = (0, import_queries.selectedRect)(state);
  dispatch?.((0, import_mutations.selectColumn)(tr, rect, index));
  return true;
};
const selectColumnCmd = () => (state, dispatch) => {
  const tr = state.tr;
  const rect = (0, import_queries.selectedRect)(state);
  dispatch?.((0, import_mutations.selectColumn)(tr, rect, rect.left));
  return true;
};
const selectRowAtIndex = (index) => (state, dispatch) => {
  const tr = state.tr;
  tr.setMeta(import_consts.TRANSACTION_META_KEYS.SELECT_ROW, index);
  const rect = (0, import_queries.selectedRect)(state);
  dispatch?.((0, import_mutations.selectRow)(tr, rect, index));
  return true;
};
const selectRowCmd = () => (state, dispatch) => {
  const tr = state.tr;
  const rect = (0, import_queries.selectedRect)(state);
  dispatch?.((0, import_mutations.selectRow)(tr, rect, rect.top));
  return true;
};
const selectRows = (fromRow, toRow, tablePos) => (state, dispatch) => {
  const tr = state.tr;
  const $tablePos = tr.doc.resolve(tablePos);
  const tempSelection = import_prosemirror_state.TextSelection.create(tr.doc, $tablePos.pos);
  const table = import_table_query.TableQuery.of(tempSelection);
  if (!table) {
    return false;
  }
  const initialRect = {
    left: 0,
    right: table.map.width,
    top: fromRow,
    bottom: toRow + 1
  };
  const cells = (0, import_cellselection.getCellsInExpandedRect)(tr.doc, table.getStartPos(), table.map, initialRect);
  const selection = new import_cellselection.RicosCellSelection(cells, tr.doc);
  if (dispatch) {
    dispatch(tr.setSelection(selection));
  }
  return true;
};
const selectColumns = (fromCol, toCol, tablePos) => (state, dispatch) => {
  const tr = state.tr;
  const $tablePos = tr.doc.resolve(tablePos);
  const tempSelection = import_prosemirror_state.TextSelection.create(tr.doc, $tablePos.pos);
  const table = import_table_query.TableQuery.of(tempSelection);
  if (!table) {
    return false;
  }
  const initialRect = {
    left: fromCol,
    right: toCol + 1,
    top: 0,
    bottom: table.map.height
  };
  const cells = (0, import_cellselection.getCellsInExpandedRect)(tr.doc, table.getStartPos(), table.map, initialRect);
  const selection = new import_cellselection.RicosCellSelection(cells, tr.doc);
  if (dispatch) {
    dispatch(tr.setSelection(selection));
  }
  return true;
};
const deleteRowAtIndex = (index) => (state, dispatch) => {
  const tr = state.tr;
  const rect = (0, import_queries.selectedRect)(state);
  (0, import_mutations.removeRow)(tr, rect, index);
  dispatch?.(tr);
  return true;
};
const selectWholeTable = () => (state, dispatch) => {
  return (0, import_mutations.selectTable)(state, dispatch);
};
//# sourceMappingURL=selection.js.map
