"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const TableRowElement = require("./TableRowElement.cjs");
const TableElementRenderer = require("./TableElementRenderer.cjs");
const index = require("../../../ContemberEditor/index.cjs");
const gaugeTableColumnCount = require("./gaugeTableColumnCount.cjs");
const TableCellElement = require("./TableCellElement.cjs");
const tableElementType = "table";
const isTableElement = (element) => Slate.Element.isElement(element) && element.type === tableElementType;
const createEmptyTableElement = (rowCount = 3, columnCount = 2) => ({
  type: tableElementType,
  children: Array.from({ length: rowCount }, () => TableRowElement.createEmptyTableRowElement(columnCount))
});
const getTableElementRowCount = (element) => {
  return element.children.length;
};
const getTableElementColumnCount = (element) => {
  const firstRow = element.children[0];
  return firstRow?.children.length ?? 0;
};
const tableElementPlugin = {
  type: tableElementType,
  render: TableElementRenderer.TableElementRenderer,
  normalizeNode: ({ editor, element, path, preventDefault }) => {
    if (element.children.length === 0) {
      Slate.Transforms.removeNodes(editor, { at: path });
      return preventDefault();
    }
    let didTransform = false;
    for (const [child, childPath] of Slate.Node.children(editor, path)) {
      if (Slate.Element.isElement(child)) {
        if (!TableRowElement.isTableRowElement(child)) {
          index.ContemberEditor.ejectElement(editor, childPath);
          Slate.Transforms.setNodes(editor, { type: TableRowElement.tableRowElementType }, { at: childPath });
          didTransform = true;
        }
      } else {
        Slate.Transforms.removeNodes(editor, { at: path });
        return preventDefault();
      }
    }
    if (didTransform) {
      return preventDefault();
    }
    const columnCount = gaugeTableColumnCount.gaugeTableColumnCount(element);
    for (const [row, childPath] of Slate.Node.children(editor, path)) {
      const currentColumnCount = row.children.length;
      if (currentColumnCount === columnCount) {
        continue;
      }
      if (currentColumnCount > columnCount) {
        const shouldBiasTowardsStart = childPath[childPath.length - 1] === 0;
        for (let i = columnCount; i < currentColumnCount; i++) {
          Slate.Transforms.removeNodes(editor, { at: [...childPath, shouldBiasTowardsStart ? 0 : i] });
        }
      } else if (currentColumnCount < columnCount) {
        const shouldBiasTowardsStart = childPath[childPath.length - 1] === 0;
        for (let i = currentColumnCount; i < columnCount; i++) {
          Slate.Transforms.insertNodes(editor, TableCellElement.createEmptyTableCellElement(), {
            at: [...childPath, shouldBiasTowardsStart ? 0 : i]
          });
        }
      }
    }
  },
  canContainAnyBlocks: false,
  toggleElement: ({ editor }) => {
    const closestTableEntry = Slate.Editor.above(editor, {
      mode: "lowest",
      match: (matchedNode) => Slate.Element.isElement(matchedNode) && isTableElement(matchedNode)
    });
    if (closestTableEntry) {
      return;
    }
    const { selection } = editor;
    if (!selection || Slate.Range.isExpanded(selection)) {
      return;
    }
    const closestDefaultEntry = Slate.Editor.above(editor, {
      at: selection.focus,
      mode: "lowest",
      match: (matchedNode) => Slate.Element.isElement(matchedNode) && editor.isDefaultElement(matchedNode)
    });
    if (!closestDefaultEntry) {
      return;
    }
    const [closestDefault, closestDefaultPath] = closestDefaultEntry;
    if (closestDefaultPath.length !== 1) {
      return;
    }
    Slate.Editor.withoutNormalizing(editor, () => {
      let targetPath;
      if (Slate.Node.string(closestDefault) === "") {
        Slate.Transforms.removeNodes(editor, {
          at: closestDefaultPath
        });
        targetPath = closestDefaultPath;
      } else {
        targetPath = [closestDefaultPath[0] + 1];
      }
      const table = createEmptyTableElement();
      Slate.Transforms.insertNodes(editor, table, {
        at: targetPath
      });
    });
  }
};
exports.createEmptyTableElement = createEmptyTableElement;
exports.getTableElementColumnCount = getTableElementColumnCount;
exports.getTableElementRowCount = getTableElementRowCount;
exports.isTableElement = isTableElement;
exports.tableElementPlugin = tableElementPlugin;
exports.tableElementType = tableElementType;
//# sourceMappingURL=TableElement.cjs.map
