"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 mutations_exports = {};
__export(mutations_exports, {
  addColSpan: () => addColSpan,
  addColumn: () => addColumn,
  addColumnAfter: () => addColumnAfter,
  addColumnAfterWithBorders: () => addColumnAfterWithBorders,
  addColumnBefore: () => addColumnBefore,
  addColumnBeforeWithBorders: () => addColumnBeforeWithBorders,
  addRow: () => addRow,
  addRowAfter: () => addRowAfter,
  addRowAfterWithBorders: () => addRowAfterWithBorders,
  addRowBefore: () => addRowBefore,
  addRowBeforeWithBorders: () => addRowBeforeWithBorders,
  applyHeaderToggle: () => applyHeaderToggle,
  deleteColumn: () => deleteColumn,
  deleteColumnWithBorders: () => deleteColumnWithBorders,
  deleteRow: () => deleteRow,
  deleteRowWithBorders: () => deleteRowWithBorders,
  deleteTable: () => deleteTable,
  mergeCells: () => mergeCells,
  mergeCellsWithBorders: () => mergeCellsWithBorders,
  removeColSpan: () => removeColSpan,
  removeColumn: () => removeColumn,
  removeRow: () => removeRow,
  selectColumn: () => selectColumn,
  selectRow: () => selectRow,
  selectTable: () => selectTable,
  splitCell: () => splitCell,
  splitCellWithBorders: () => splitCellWithBorders,
  splitCellWithType: () => splitCellWithType,
  toggleHeader: () => toggleHeader,
  toggleHeaderColumnCmd: () => toggleHeaderColumnCmd,
  toggleHeaderRowCmd: () => toggleHeaderRowCmd
});
module.exports = __toCommonJS(mutations_exports);
var import_model = require("./../../../tiptap/pm/model.js");
var import_canonical_remap = require("../../borders/canonical-remap.js");
var import_consts = require("../../consts.js");
var import_cellselection = require("../plugins/cellselection.js");
var import_table_query = require("../table-query.js");
var import_tablemap = require("../tablemap.js");
var import_find_table = require("../utilities/find-table.js");
var import_queries = require("./queries.js");
function removeColSpan(attrs, pos, n = 1) {
  const result = { ...attrs, colspan: attrs.colspan - n };
  if (result.colwidth) {
    result.colwidth = result.colwidth.slice();
    result.colwidth.splice(pos, n);
    if (!result.colwidth.some((w) => w > 0)) {
      result.colwidth = null;
    }
  }
  return result;
}
function updateTableDimensions(tr, table, tableStart, start, deleteCount, item) {
  const dimensions = getTableDimensions(table);
  if (!dimensions?.colsWidthRatio?.length) {
    return;
  }
  const colsWidthRatio = [...dimensions.colsWidthRatio];
  const colsMinWidth = dimensions.colsMinWidth ? [...dimensions.colsMinWidth] : [];
  if (item !== void 0) {
    colsWidthRatio.splice(start, deleteCount, item);
    if (colsMinWidth.length > 0) {
      colsMinWidth.splice(start, deleteCount, item);
    }
  } else {
    colsWidthRatio.splice(start, deleteCount);
    if (colsMinWidth.length > 0) {
      colsMinWidth.splice(start, deleteCount);
    }
  }
  tr.setNodeMarkup(tableStart - 1, null, {
    ...table.attrs,
    dimensions: {
      ...dimensions,
      colsWidthRatio,
      colsMinWidth: colsMinWidth.length > 0 ? colsMinWidth : dimensions.colsMinWidth
    }
  });
}
function getSourceColWidth(table, sourceCol) {
  const dimensions = getTableDimensions(table);
  if (!dimensions?.colsWidthRatio?.length || !dimensions.colsWidthRatio[sourceCol]) {
    return import_consts.CELL_AUTO_MIN_WIDTH;
  }
  return dimensions.colsWidthRatio[sourceCol];
}
function getTableDimensions(table) {
  return table.attrs.dimensions || table.attrs.tableData?.dimensions || null;
}
function getCellStyleAttrs(cell) {
  const attrs = {};
  if (cell.attrs.cellStyle) {
    attrs.cellStyle = { ...cell.attrs.cellStyle };
  }
  if (cell.attrs.borderColors) {
    attrs.borderColors = { ...cell.attrs.borderColors };
  }
  if (cell.attrs.borderWidths) {
    attrs.borderWidths = { ...cell.attrs.borderWidths };
  }
  if (cell.attrs.colwidth) {
    attrs.colwidth = cell.attrs.colwidth.slice();
  }
  return attrs;
}
function removeColumn(tr, { map, table, tableStart }, col) {
  const mapStart = tr.mapping.maps.length;
  for (let row = 0; row < map.height; ) {
    const index = row * map.width + col;
    const pos = map.map[index];
    const cell = table.nodeAt(pos);
    const attrs = cell.attrs;
    if (col > 0 && map.map[index - 1] === pos || col < map.width - 1 && map.map[index + 1] === pos) {
      tr.setNodeMarkup(
        tr.mapping.slice(mapStart).map(tableStart + pos),
        null,
        removeColSpan(attrs, col - map.findCell(pos).col)
      );
    } else {
      const start = tr.mapping.slice(mapStart).map(tableStart + pos);
      tr.delete(start, start + cell.nodeSize);
    }
    row += attrs.rowspan;
  }
}
function deleteColumn(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state);
    const tr = state.tr;
    if (rect.left === 0 && rect.right === rect.map.width) {
      return false;
    }
    for (let i = rect.right - 1; ; i--) {
      removeColumn(tr, rect, i);
      if (i === rect.left) {
        break;
      }
      const table = rect.tableStart ? tr.doc.nodeAt(rect.tableStart - 1) : tr.doc;
      if (!table) {
        throw RangeError("No table found");
      }
      rect.table = table;
      rect.map = import_tablemap.TableMap.get(table);
    }
    updateTableDimensions(tr, rect.table, rect.tableStart, rect.left, rect.right - rect.left);
    dispatch(tr);
  }
  return true;
}
function addColumn(tr, { map, tableStart, table }, col, styleSourceCol) {
  let refColumn = col > 0 ? -1 : 0;
  if ((0, import_queries.columnIsHeader)(map, table, col + refColumn)) {
    refColumn = col === 0 || col === map.width ? null : 0;
  }
  for (let row = 0; row < map.height; row++) {
    const index = row * map.width + col;
    if (col > 0 && col < map.width && map.map[index - 1] === map.map[index]) {
      const pos = map.map[index];
      const cell = table.nodeAt(pos);
      tr.setNodeMarkup(
        tr.mapping.map(tableStart + pos),
        null,
        addColSpan(cell.attrs, col - map.findCell(pos).col)
      );
      row += cell.attrs.rowspan - 1;
    } else {
      const refCell = refColumn !== null ? table.nodeAt(map.map[index + refColumn]) : null;
      const type = refCell?.type ?? (0, import_queries.tableNodeTypes)(table.type.schema).cell;
      const styleCell = styleSourceCol !== void 0 ? table.nodeAt(map.map[row * map.width + styleSourceCol]) : refCell;
      const styleAttrs = styleCell ? getCellStyleAttrs(styleCell) : {};
      const pos = map.positionAt(row, col, table);
      tr.insert(tr.mapping.map(tableStart + pos), type.createAndFill(styleAttrs));
    }
  }
  const widthSourceCol = styleSourceCol ?? (refColumn !== null ? col + refColumn : void 0);
  const colWidth = widthSourceCol !== void 0 ? getSourceColWidth(table, widthSourceCol) : import_consts.CELL_AUTO_MIN_WIDTH;
  updateTableDimensions(tr, table, tableStart, col, 0, colWidth);
  return tr;
}
function addColumnBefore(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state);
    dispatch(addColumn(state.tr, rect, rect.left, rect.left));
  }
  return true;
}
function addColumnAfter(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state);
    dispatch(addColumn(state.tr, rect, rect.right, rect.right - 1));
  }
  return true;
}
function addColSpan(attrs, pos, n = 1) {
  const result = { ...attrs, colspan: attrs.colspan + n };
  if (result.colwidth) {
    result.colwidth = result.colwidth.slice();
    for (let i = 0; i < n; i++) {
      result.colwidth.splice(pos, 0, 0);
    }
  }
  return result;
}
function splitCell(state, dispatch) {
  const nodeTypes = (0, import_queries.tableNodeTypes)(state.schema);
  return splitCellWithType(({ node }) => {
    return nodeTypes[node.type.spec.tableRole];
  })(state, dispatch);
}
function splitCellWithType(getCellType) {
  return (state, dispatch) => {
    const sel = state.selection;
    let cellNode;
    let cellPos;
    if (!(sel instanceof import_cellselection.RicosCellSelection)) {
      cellNode = (0, import_queries.cellWrapping)(sel.$from);
      if (!cellNode) {
        return false;
      }
      cellPos = (0, import_queries.cellAround)(sel.$from)?.pos;
    } else {
      if (sel.$anchorCell.pos !== sel.$headCell.pos) {
        return false;
      }
      cellNode = sel.$anchorCell.nodeAfter;
      cellPos = sel.$anchorCell.pos;
    }
    if (cellNode === null || cellPos === null) {
      return false;
    }
    if (cellNode.attrs.colspan === 1 && cellNode.attrs.rowspan === 1) {
      return false;
    }
    if (dispatch) {
      let baseAttrs = cellNode.attrs;
      const attrs = [];
      const colwidth = baseAttrs.colwidth;
      if (baseAttrs.rowspan > 1) {
        baseAttrs = { ...baseAttrs, rowspan: 1 };
      }
      if (baseAttrs.colspan > 1) {
        baseAttrs = { ...baseAttrs, colspan: 1 };
      }
      const rect = (0, import_queries.selectedRect)(state), tr = state.tr;
      for (let i = 0; i < rect.right - rect.left; i++) {
        attrs.push(
          colwidth ? {
            ...baseAttrs,
            colwidth: colwidth && colwidth[i] ? [colwidth[i]] : null
          } : baseAttrs
        );
      }
      const createdCells = [];
      for (let row = rect.top; row < rect.bottom; row++) {
        let pos = rect.map.positionAt(row, rect.left, rect.table);
        if (row === rect.top) {
          pos += cellNode.nodeSize;
        }
        for (let col = rect.left, i = 0; col < rect.right; col++, i++) {
          if (col === rect.left && row === rect.top) {
            continue;
          }
          const insertedPos = tr.mapping.map(pos + rect.tableStart, 1);
          tr.insert(
            insertedPos,
            getCellType({ node: cellNode, row, col }).createAndFill(attrs[i])
          );
          createdCells.push(insertedPos);
        }
      }
      tr.setNodeMarkup(
        cellPos,
        getCellType({ node: cellNode, row: rect.top, col: rect.left }),
        attrs[0]
      );
      if (sel instanceof import_cellselection.RicosCellSelection) {
        const allCellPositions = [sel.$anchorCell.pos, ...createdCells];
        tr.setSelection(new import_cellselection.RicosCellSelection(allCellPositions.map((pos) => tr.doc.resolve(pos))));
      }
      dispatch(tr);
    }
    return true;
  };
}
function addRow(tr, { map, tableStart, table }, row, styleSourceRow) {
  let rowPos = tableStart;
  for (let i = 0; i < row; i++) {
    rowPos += table.child(i).nodeSize;
  }
  const cells = [];
  let refRow = row > 0 ? -1 : 0;
  if ((0, import_queries.rowIsHeader)(map, table, row + refRow)) {
    refRow = row === 0 || row === map.height ? null : 0;
  }
  for (let col = 0, index = map.width * row; col < map.width; col++, index++) {
    if (row > 0 && row < map.height && map.map[index] === map.map[index - map.width]) {
      const pos = map.map[index];
      const attrs = table.nodeAt(pos).attrs;
      tr.setNodeMarkup(tableStart + pos, null, {
        ...attrs,
        rowspan: attrs.rowspan + 1
      });
      col += attrs.colspan - 1;
    } else {
      const refCell = refRow !== null ? table.nodeAt(map.map[index + refRow * map.width]) : null;
      const type = refCell?.type ?? (0, import_queries.tableNodeTypes)(table.type.schema).cell;
      const styleCell = styleSourceRow !== void 0 ? table.nodeAt(map.map[styleSourceRow * map.width + col]) : refCell;
      const styleAttrs = styleCell ? getCellStyleAttrs(styleCell) : {};
      const node = type.createAndFill(styleAttrs);
      if (node) {
        cells.push(node);
      }
    }
  }
  const sourceRowIdx = styleSourceRow ?? (refRow !== null ? row + refRow : void 0);
  const rowAttrs = sourceRowIdx !== void 0 ? { height: table.child(sourceRowIdx).attrs.height } : null;
  tr.insert(rowPos, (0, import_queries.tableNodeTypes)(table.type.schema).row.create(rowAttrs, cells));
  return tr;
}
function addRowBefore(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state);
    dispatch(addRow(state.tr, rect, rect.top, rect.top));
  }
  return true;
}
function addRowAfter(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state);
    dispatch(addRow(state.tr, rect, rect.bottom, rect.bottom - 1));
  }
  return true;
}
function removeRow(tr, { map, table, tableStart }, row) {
  let rowPos = 0;
  for (let i = 0; i < row; i++) {
    rowPos += table.child(i).nodeSize;
  }
  const nextRow = rowPos + table.child(row).nodeSize;
  const mapFrom = tr.mapping.maps.length;
  tr.delete(rowPos + tableStart, nextRow + tableStart);
  const seen = /* @__PURE__ */ new Set();
  for (let col = 0, index = row * map.width; col < map.width; col++, index++) {
    const pos = map.map[index];
    if (seen.has(pos)) {
      continue;
    }
    seen.add(pos);
    if (row > 0 && pos === map.map[index - map.width]) {
      const attrs = table.nodeAt(pos).attrs;
      tr.setNodeMarkup(tr.mapping.slice(mapFrom).map(pos + tableStart), null, {
        ...attrs,
        rowspan: attrs.rowspan - 1
      });
      col += attrs.colspan - 1;
    } else if (row < map.height && pos === map.map[index + map.width]) {
      const cell = table.nodeAt(pos);
      const attrs = cell.attrs;
      const copy = cell.type.create({ ...attrs, rowspan: cell.attrs.rowspan - 1 }, cell.content);
      const newPos = map.positionAt(row + 1, col, table);
      tr.insert(tr.mapping.slice(mapFrom).map(tableStart + newPos), copy);
      col += attrs.colspan - 1;
    }
  }
}
function deleteRow(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const rect = (0, import_queries.selectedRect)(state), tr = state.tr;
    if (rect.top === 0 && rect.bottom === rect.map.height) {
      return false;
    }
    for (let i = rect.bottom - 1; ; i--) {
      removeRow(tr, rect, i);
      if (i === rect.top) {
        break;
      }
      const table = rect.tableStart ? tr.doc.nodeAt(rect.tableStart - 1) : tr.doc;
      if (!table) {
        throw RangeError("No table found");
      }
      rect.table = table;
      rect.map = import_tablemap.TableMap.get(rect.table);
    }
    dispatch(tr);
  }
  return true;
}
function applyHeaderToggle(tr, rect, type, types) {
  const headerRow = (0, import_queries.isHeaderEnabledByType)("row", rect, types);
  const headerCol = (0, import_queries.isHeaderEnabledByType)("column", rect, types);
  const isHeaderEnabled = type === "column" ? headerRow.enabled : type === "row" ? headerCol.enabled : false;
  const selectionStartsAt = isHeaderEnabled ? 1 : 0;
  const cellsRect = type === "column" ? { left: 0, top: selectionStartsAt, right: 1, bottom: rect.map.height } : type === "row" ? { left: selectionStartsAt, top: 0, right: rect.map.width, bottom: 1 } : rect;
  const newType = type === "column" ? headerCol.enabled ? types.cell : types.header_cell : type === "row" ? headerRow.enabled ? types.cell : types.header_cell : types.cell;
  rect.map.cellsInRect(cellsRect).forEach((relativeCellPos) => {
    const cellPos = relativeCellPos + rect.tableStart;
    const cell = tr.doc.nodeAt(cellPos);
    if (cell) {
      tr.setNodeMarkup(cellPos, newType, cell.attrs);
    }
  });
}
function toggleHeader(type) {
  return function(state, dispatch) {
    if (!(0, import_queries.isInTable)(state)) {
      return false;
    }
    const types = (0, import_queries.tableNodeTypes)(state.schema);
    const rect = (0, import_queries.selectedRect)(state);
    const headerRow = (0, import_queries.isHeaderEnabledByType)("row", rect, types);
    const headerCol = (0, import_queries.isHeaderEnabledByType)("column", rect, types);
    if (type === "row" && !headerRow.enabled && headerRow.spansCrossBoundary) {
      return false;
    }
    if (type === "column" && !headerCol.enabled && headerCol.spansCrossBoundary) {
      return false;
    }
    if (dispatch) {
      const tr = state.tr;
      applyHeaderToggle(tr, rect, type, types);
      dispatch(tr);
    }
    return true;
  };
}
function mergeCells(state, dispatch) {
  const sel = state.selection;
  if (!(sel instanceof import_cellselection.RicosCellSelection) || sel.$anchorCell.pos === sel.$headCell.pos) {
    return false;
  }
  const rect = (0, import_queries.selectedRect)(state), { map } = rect;
  if ((0, import_queries.cellsOverlapRectangle)(map, rect)) {
    return false;
  }
  const hasHeaderRow = (0, import_queries.rowIsHeader)(map, rect.table, 0);
  const hasHeaderCol = (0, import_queries.columnIsHeader)(map, rect.table, 0);
  const crossesRowBoundary = hasHeaderRow && rect.top === 0 && rect.bottom > 1;
  const crossesColBoundary = hasHeaderCol && rect.left === 0 && rect.right > 1;
  if (crossesRowBoundary && !(hasHeaderCol && rect.right === 1)) {
    return false;
  }
  if (crossesColBoundary && !(hasHeaderRow && rect.bottom === 1)) {
    return false;
  }
  if (dispatch) {
    const tr = state.tr;
    const seen = {};
    let content = import_model.Fragment.empty;
    let mergedPos;
    let mergedCell;
    for (let row = rect.top; row < rect.bottom; row++) {
      for (let col = rect.left; col < rect.right; col++) {
        const cellPos = map.map[row * map.width + col];
        const cell = rect.table.nodeAt(cellPos);
        if (seen[cellPos] || !cell) {
          continue;
        }
        seen[cellPos] = true;
        if (typeof mergedPos !== "number") {
          mergedPos = cellPos;
          mergedCell = cell;
        } else {
          if (!(0, import_queries.isEmpty)(cell)) {
            content = content.append(cell.content);
          }
          const mapped = tr.mapping.map(cellPos + rect.tableStart);
          tr.delete(mapped, mapped + cell.nodeSize);
        }
      }
    }
    if (typeof mergedPos !== "number" || !(mergedCell instanceof import_model.Node)) {
      return true;
    }
    tr.setNodeMarkup(mergedPos + rect.tableStart, null, {
      ...addColSpan(
        mergedCell.attrs,
        mergedCell.attrs.colspan,
        rect.right - rect.left - mergedCell.attrs.colspan
      ),
      rowspan: rect.bottom - rect.top
    });
    if (content.size) {
      const end = mergedPos + 1 + mergedCell.content.size;
      const start = (0, import_queries.isEmpty)(mergedCell) ? mergedPos + 1 : end;
      tr.replaceWith(start + rect.tableStart, end + rect.tableStart, content);
    }
    tr.setSelection(
      new import_cellselection.RicosCellSelection(
        [tr.doc.resolve(mergedPos + rect.tableStart)].filter(
          (pos) => pos !== void 0
        )
      )
    );
    dispatch(tr);
  }
  return true;
}
function deleteTable(state, dispatch) {
  const $pos = state.selection.$anchor;
  for (let d = $pos.depth; d > 0; d--) {
    const node = $pos.node(d);
    if (node.type.spec.tableRole === "table") {
      if (dispatch) {
        dispatch(state.tr.delete($pos.before(d), $pos.after(d)).scrollIntoView());
      }
      return true;
    }
  }
  return false;
}
function selectTable(state, dispatch) {
  if (!(0, import_queries.isInTable)(state)) {
    return false;
  }
  if (dispatch) {
    const $cell = (0, import_queries.selectionCell)(state);
    const table = $cell.node(-1);
    const tableStart = $cell.start(-1);
    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));
    dispatch(state.tr.setSelection(new import_cellselection.RicosCellSelection(cells)).scrollIntoView());
  }
  return true;
}
function selectColumn(tr, { map, tableStart }, col) {
  const cells = map.cellsInRect({ left: col, right: col + 1, top: 0, bottom: map.height });
  const selection = new import_cellselection.RicosCellSelection(
    cells.map((pos) => tr.doc.resolve(tableStart + pos)),
    tr.doc
  );
  tr.setSelection(selection);
  return tr;
}
function selectRow(tr, { map, tableStart }, row) {
  const cells = map.cellsInRect({ left: 0, right: map.width, top: row, bottom: row + 1 });
  const selection = new import_cellselection.RicosCellSelection(
    cells.map((pos) => tr.doc.resolve(tableStart + pos)),
    tr.doc
  );
  tr.setSelection(selection);
  return tr;
}
function withBorderCanonicalization(dispatch) {
  if (!dispatch) {
    return void 0;
  }
  return (tr) => {
    const tableInfo = (0, import_find_table.findTable)(tr.selection);
    if (tableInfo) {
      (0, import_canonical_remap.canonicalizeBorders)(tr, tableInfo.node, tableInfo.start);
    }
    dispatch(tr);
  };
}
const deleteColumnWithBorders = () => (state, dispatch) => deleteColumn(state, withBorderCanonicalization(dispatch));
const deleteRowWithBorders = () => (state, dispatch) => deleteRow(state, withBorderCanonicalization(dispatch));
const mergeCellsWithBorders = () => (state, dispatch) => mergeCells(state, withBorderCanonicalization(dispatch));
const splitCellWithBorders = () => (state, dispatch) => splitCell(state, withBorderCanonicalization(dispatch));
const addColumnBeforeWithBorders = () => (state, dispatch) => {
  const table = import_table_query.TableQuery.of(state.selection);
  if (!table) {
    return false;
  }
  const colRange = table.getSelectionColRange();
  if (!dispatch) {
    return true;
  }
  const rect = (0, import_queries.selectedRect)(state);
  const tr = addColumn(state.tr, rect, rect.left, rect.left);
  const tableInfo = (0, import_find_table.findTable)(tr.selection);
  if (tableInfo) {
    (0, import_canonical_remap.canonicalizeBorders)(tr, tableInfo.node, tableInfo.start);
  }
  if (colRange) {
    const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
    selectColumn(tr, newRect, colRange.first);
  }
  dispatch(tr);
  return true;
};
const addColumnAfterWithBorders = () => (state, dispatch) => {
  const table = import_table_query.TableQuery.of(state.selection);
  if (!table) {
    return false;
  }
  const colRange = table.getSelectionColRange();
  if (!dispatch) {
    return true;
  }
  const rect = (0, import_queries.selectedRect)(state);
  const tr = addColumn(state.tr, rect, rect.right, rect.right - 1);
  const tableInfo = (0, import_find_table.findTable)(tr.selection);
  if (tableInfo) {
    (0, import_canonical_remap.canonicalizeBorders)(tr, tableInfo.node, tableInfo.start);
  }
  if (colRange) {
    const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
    selectColumn(tr, newRect, colRange.last + 1);
  }
  dispatch(tr);
  return true;
};
const addRowBeforeWithBorders = () => (state, dispatch) => {
  const table = import_table_query.TableQuery.of(state.selection);
  if (!table) {
    return false;
  }
  const rowRange = table.getSelectionRowRange();
  if (!dispatch) {
    return true;
  }
  const rect = (0, import_queries.selectedRect)(state);
  const tr = addRow(state.tr, rect, rect.top, rect.top);
  const tableInfo = (0, import_find_table.findTable)(tr.selection);
  if (tableInfo) {
    (0, import_canonical_remap.canonicalizeBorders)(tr, tableInfo.node, tableInfo.start);
  }
  if (rowRange) {
    const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
    selectRow(tr, newRect, rowRange.first);
  }
  dispatch(tr);
  return true;
};
const addRowAfterWithBorders = () => (state, dispatch) => {
  const table = import_table_query.TableQuery.of(state.selection);
  if (!table) {
    return false;
  }
  const rowRange = table.getSelectionRowRange();
  if (!dispatch) {
    return true;
  }
  const rect = (0, import_queries.selectedRect)(state);
  const tr = addRow(state.tr, rect, rect.bottom, rect.bottom - 1);
  const tableInfo = (0, import_find_table.findTable)(tr.selection);
  if (tableInfo) {
    (0, import_canonical_remap.canonicalizeBorders)(tr, tableInfo.node, tableInfo.start);
  }
  if (rowRange) {
    const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
    selectRow(tr, newRect, rowRange.last + 1);
  }
  dispatch(tr);
  return true;
};
const toggleHeaderColumnCmd = () => (state, dispatch) => {
  if (!toggleHeader("column")(state)) {
    return false;
  }
  if (!dispatch) {
    return true;
  }
  const tr = state.tr;
  const rect = (0, import_queries.selectedRect)(state);
  const types = (0, import_queries.tableNodeTypes)(state.schema);
  const parentTable = (0, import_find_table.findTable)(state.selection);
  if (parentTable) {
    const existingTableData = parentTable.node.attrs.tableData || {};
    tr.setNodeMarkup(parentTable.pos, void 0, {
      ...parentTable.node.attrs,
      tableData: { ...existingTableData, columnHeader: !existingTableData.columnHeader }
    });
  }
  applyHeaderToggle(tr, rect, "column", types);
  const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
  selectColumn(tr, newRect, 0);
  dispatch(tr);
  return true;
};
const toggleHeaderRowCmd = () => (state, dispatch) => {
  if (!toggleHeader("row")(state)) {
    return false;
  }
  if (!dispatch) {
    return true;
  }
  const tr = state.tr;
  const rect = (0, import_queries.selectedRect)(state);
  const types = (0, import_queries.tableNodeTypes)(state.schema);
  const parentTable = (0, import_find_table.findTable)(state.selection);
  if (parentTable) {
    const existingTableData = parentTable.node.attrs.tableData || {};
    tr.setNodeMarkup(parentTable.pos, void 0, {
      ...parentTable.node.attrs,
      tableData: { ...existingTableData, rowHeader: !existingTableData.rowHeader }
    });
  }
  applyHeaderToggle(tr, rect, "row", types);
  const newRect = (0, import_queries.getTableRectFromDoc)(tr.doc, rect.tableStart);
  selectRow(tr, newRect, 0);
  dispatch(tr);
  return true;
};
//# sourceMappingURL=mutations.js.map
