import { Editor, Transforms } from "slate";
import { ReactEditor } from "slate-react";
import { getTableElementColumnCount, getTableElementRowCount } from "./TableElement.js";
import { createEmptyTableRowElement, tableRowElementType, isTableRowElement } from "./TableRowElement.js";
import { ContemberEditor } from "../../../ContemberEditor/index.js";
import { isTableCellElement, tableCellElementType, createEmptyTableCellElement } from "./TableCellElement.js";
class TableModifications {
  static deleteTableColumn(editor, element, index) {
    const tablePath = ReactEditor.findPath(editor, element);
    const rowCount = element.children.length;
    Editor.withoutNormalizing(editor, () => {
      for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        Transforms.removeNodes(editor, {
          at: [...tablePath, rowIndex, index]
        });
      }
    });
  }
  static addTableRow(editor, element, index) {
    const tablePath = ReactEditor.findPath(editor, element);
    const columnCount = getTableElementColumnCount(element);
    const rowIndex = index ?? element.children.length;
    let tableRow;
    if (element.children.length === 0) {
      tableRow = createEmptyTableRowElement(columnCount);
    } else {
      const blueprintRow = element.children[rowIndex === 0 ? 0 : rowIndex - 1];
      tableRow = {
        ...ContemberEditor.elementToSpecifics(blueprintRow),
        type: tableRowElementType,
        children: Array.from({ length: columnCount }, (_, columnIndex) => copyTableCell(blueprintRow.children[columnIndex]))
      };
      if (rowIndex === 0) {
        const firstRow = element.children[0];
        if (firstRow.headerScope) {
          Transforms.setNodes(editor, { headerScope: null }, { at: [...tablePath, 0] });
        }
      }
    }
    Transforms.insertNodes(editor, tableRow, {
      at: [...tablePath, rowIndex]
    });
  }
  static addTableColumn(editor, element, index) {
    const tablePath = ReactEditor.findPath(editor, element);
    const columnCount = getTableElementColumnCount(element);
    const rowCount = getTableElementRowCount(element);
    const columnIndex = index ?? columnCount ?? 0;
    Editor.withoutNormalizing(editor, () => {
      for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        const tableRow = element.children[rowIndex];
        let tableCell;
        if (tableRow.children.length === 0) {
          tableCell = createEmptyTableCellElement();
        } else {
          tableCell = copyTableCell(tableRow.children[columnIndex === 0 ? 0 : columnIndex - 1]);
          if (columnIndex === 0) {
            const firstCell = tableRow.children[0];
            if (firstCell.headerScope) {
              Transforms.setNodes(editor, { headerScope: null }, { at: [...tablePath, rowIndex, 0] });
            }
          }
        }
        Transforms.insertNodes(editor, tableCell, {
          at: [...tablePath, rowIndex, columnIndex]
        });
      }
    });
  }
  static justifyTableColumn(editor, element, columnIndex, direction) {
    forEachCellInColumn(editor, element, columnIndex, ([, cellPath]) => {
      Transforms.setNodes(editor, { justify: direction ?? null }, { match: (node) => isTableCellElement(node), at: cellPath });
    });
  }
  static toggleTableRowHeaderScope(editor, element, rowIndex, scope) {
    const rowCount = element.children.length;
    if (rowIndex !== 0 || rowCount < 1) {
      return;
    }
    const tablePath = ReactEditor.findPath(editor, element);
    const firstRow = element.children[0];
    Transforms.setNodes(editor, { headerScope: firstRow.headerScope === scope ? null : scope }, { match: (node) => 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]) => {
      Transforms.setNodes(editor, { headerScope: shouldSetScope ? scope : null }, { match: (node) => isTableCellElement(node), at: cellPath });
    });
  }
  static deleteTableRow(editor, element, index) {
    const tablePath = ReactEditor.findPath(editor, element);
    Transforms.removeNodes(editor, {
      at: [...tablePath, index]
    });
  }
}
const forEachCellInColumn = (editor, element, columnIndex, callback) => {
  const tablePath = ReactEditor.findPath(editor, element);
  const rowCount = getTableElementRowCount(element);
  Editor.withoutNormalizing(editor, () => {
    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
      callback(Editor.node(editor, [...tablePath, rowIndex, columnIndex]));
    }
  });
};
const copyTableCell = (tableCellElement) => ({
  ...ContemberEditor.elementToSpecifics(tableCellElement),
  type: tableCellElementType,
  children: [{ text: "" }]
});
export { TableModifications };
//# sourceMappingURL=TableModifications.js.map
