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