"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const slateReact = require("slate-react");
const TableElement = require("./TableElement.cjs");
const TableRowElement = require("./TableRowElement.cjs");
const index = require("../../../ContemberEditor/index.cjs");
const TableCellElement = require("./TableCellElement.cjs");
class TableModifications {
  static deleteTableColumn(editor, element, index2) {
    const tablePath = slateReact.ReactEditor.findPath(editor, element);
    const rowCount = element.children.length;
    Slate.Editor.withoutNormalizing(editor, () => {
      for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        Slate.Transforms.removeNodes(editor, {
          at: [...tablePath, rowIndex, index2]
        });
      }
    });
  }
  static addTableRow(editor, element, index$1) {
    const tablePath = slateReact.ReactEditor.findPath(editor, element);
    const columnCount = TableElement.getTableElementColumnCount(element);
    const rowIndex = index$1 ?? element.children.length;
    let tableRow;
    if (element.children.length === 0) {
      tableRow = TableRowElement.createEmptyTableRowElement(columnCount);
    } else {
      const blueprintRow = element.children[rowIndex === 0 ? 0 : rowIndex - 1];
      tableRow = {
        ...index.ContemberEditor.elementToSpecifics(blueprintRow),
        type: TableRowElement.tableRowElementType,
        children: Array.from(
          { length: columnCount },
          (_, columnIndex) => copyTableCell(blueprintRow.children[columnIndex])
        )
      };
      if (rowIndex === 0) {
        const firstRow = element.children[0];
        if (firstRow.headerScope) {
          Slate.Transforms.setNodes(editor, { headerScope: null }, { at: [...tablePath, 0] });
        }
      }
    }
    Slate.Transforms.insertNodes(editor, tableRow, {
      at: [...tablePath, rowIndex]
    });
  }
  static addTableColumn(editor, element, index2) {
    const tablePath = slateReact.ReactEditor.findPath(editor, element);
    const columnCount = TableElement.getTableElementColumnCount(element);
    const rowCount = TableElement.getTableElementRowCount(element);
    const columnIndex = index2 ?? columnCount ?? 0;
    Slate.Editor.withoutNormalizing(editor, () => {
      for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        const tableRow = element.children[rowIndex];
        let tableCell;
        if (tableRow.children.length === 0) {
          tableCell = TableCellElement.createEmptyTableCellElement();
        } else {
          tableCell = copyTableCell(tableRow.children[columnIndex === 0 ? 0 : columnIndex - 1]);
          if (columnIndex === 0) {
            const firstCell = tableRow.children[0];
            if (firstCell.headerScope) {
              Slate.Transforms.setNodes(editor, { headerScope: null }, { at: [...tablePath, rowIndex, 0] });
            }
          }
        }
        Slate.Transforms.insertNodes(editor, tableCell, {
          at: [...tablePath, rowIndex, columnIndex]
        });
      }
    });
  }
  static justifyTableColumn(editor, element, columnIndex, direction) {
    forEachCellInColumn(editor, element, columnIndex, ([, cellPath]) => {
      Slate.Transforms.setNodes(
        editor,
        { justify: direction ?? null },
        { match: (node) => TableCellElement.isTableCellElement(node), at: cellPath }
      );
    });
  }
  static toggleTableRowHeaderScope(editor, element, rowIndex, scope) {
    const rowCount = element.children.length;
    if (rowIndex !== 0 || rowCount < 1) {
      return;
    }
    const tablePath = slateReact.ReactEditor.findPath(editor, element);
    const firstRow = element.children[0];
    Slate.Transforms.setNodes(
      editor,
      { headerScope: firstRow.headerScope === scope ? null : scope },
      { match: (node) => TableRowElement.isTableRowElement(node), at: [...tablePath, 0] }
    );
  }
  static toggleTableColumnHeaderScope(editor, element, columnIndex, scope) {
    let currentStatusScore = 0;
    const rowCount = element.children.length;
    forEachCellInColumn(editor, element, columnIndex, ([cell]) => {
      currentStatusScore += cell.headerScope === scope ? 1 : -1;
    });
    const shouldSetScope = -currentStatusScore === rowCount || currentStatusScore > 0 && currentStatusScore !== rowCount;
    forEachCellInColumn(editor, element, columnIndex, ([, cellPath]) => {
      Slate.Transforms.setNodes(
        editor,
        { headerScope: shouldSetScope ? scope : null },
        { match: (node) => TableCellElement.isTableCellElement(node), at: cellPath }
      );
    });
  }
  static deleteTableRow(editor, element, index2) {
    const tablePath = slateReact.ReactEditor.findPath(editor, element);
    Slate.Transforms.removeNodes(editor, {
      at: [...tablePath, index2]
    });
  }
}
const forEachCellInColumn = (editor, element, columnIndex, callback) => {
  const tablePath = slateReact.ReactEditor.findPath(editor, element);
  const rowCount = TableElement.getTableElementRowCount(element);
  Slate.Editor.withoutNormalizing(editor, () => {
    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
      callback(Slate.Editor.node(editor, [...tablePath, rowIndex, columnIndex]));
    }
  });
};
const copyTableCell = (tableCellElement) => ({
  ...index.ContemberEditor.elementToSpecifics(tableCellElement),
  type: TableCellElement.tableCellElementType,
  children: [{ text: "" }]
});
exports.TableModifications = TableModifications;
//# sourceMappingURL=TableModifications.cjs.map
