"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const index = require("../../../ContemberEditor/index.cjs");
const TableCellElement = require("./TableCellElement.cjs");
const TableElement = require("./TableElement.cjs");
const TableElementSelection = require("./TableElementSelection.cjs");
const TableModifications = require("./TableModifications.cjs");
const TableRowElement = require("./TableRowElement.cjs");
const withTables = (editor) => {
  const {
    deleteForward,
    deleteBackward,
    deleteFragment,
    insertBreak,
    onKeyDown
  } = editor;
  editor.registerElement(TableElement.tableElementPlugin);
  editor.registerElement(TableCellElement.tableCellElementPlugin);
  editor.registerElement(TableRowElement.tableRowElementPlugin);
  Object.assign(editor, {
    insertBreak: () => {
      const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor);
      if (closestBlockEntry && TableCellElement.isTableCellElement(closestBlockEntry[0])) {
        const selection = editor.selection;
        const [, closestBlockPath] = closestBlockEntry;
        const [cellStart, cellEnd] = Slate.Editor.edges(editor, closestBlockPath);
        if (!selection) {
          return insertBreak();
        }
        return Slate.Editor.withoutNormalizing(editor, () => {
          Slate.Transforms.wrapNodes(editor, editor.createDefaultElement([]), {
            at: {
              anchor: cellStart,
              focus: cellEnd
            },
            match: (node) => Slate.Text.isText(node) || Slate.Element.isElement(node) && editor.isInline(node)
          });
          const relative = Slate.Path.relative(selection.focus.path, closestBlockPath);
          Slate.Transforms.splitNodes(editor, {
            // The zero should be the newly created default element.
            at: {
              path: [...closestBlockPath, 0, ...relative],
              offset: selection.focus.offset
            },
            always: true
          });
        });
      }
      insertBreak();
    },
    deleteBackward: (unit) => {
      const selection = editor.selection;
      if (selection && Slate.Range.isCollapsed(selection)) {
        const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor);
        if (closestBlockEntry && TableCellElement.isTableCellElement(closestBlockEntry[0]) && Slate.Point.equals(selection.focus, Slate.Editor.start(editor, closestBlockEntry[1]))) {
          return;
        }
      }
      deleteBackward(unit);
    },
    deleteForward: (unit) => {
      const selection = editor.selection;
      if (selection && Slate.Range.isCollapsed(selection)) {
        const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor);
        if (closestBlockEntry && TableCellElement.isTableCellElement(closestBlockEntry[0]) && Slate.Point.equals(selection.focus, Slate.Editor.end(editor, closestBlockEntry[1]))) {
          return;
        }
      }
      deleteForward(unit);
    },
    deleteFragment: () => {
      const selection = editor.selection;
      if (!selection || Slate.Range.isCollapsed(selection)) {
        return deleteFragment();
      }
      const lowestCommonAncestorPath = Slate.Path.common(selection.anchor.path, selection.focus.path);
      const [lowestCommonAncestor] = Slate.Editor.node(editor, lowestCommonAncestorPath);
      if (TableElement.isTableElement(lowestCommonAncestor) || TableRowElement.isTableRowElement(lowestCommonAncestor)) {
        return;
      }
      deleteFragment();
    },
    onKeyDown: (event) => {
      const selection = editor.selection;
      if (!selection || !(event.key === "Tab" || event.key === "ArrowUp" || event.key === "ArrowRight" || event.key === "ArrowDown" || event.key === "ArrowLeft" || event.key === "Enter" || event.key === "Backspace")) {
        return onKeyDown(event);
      }
      const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor, { at: selection });
      if (!closestBlockEntry) {
        return onKeyDown(event);
      }
      let cellElement;
      let cellPath;
      if (!TableCellElement.isTableCellElement(closestBlockEntry[0])) {
        const parent = Slate.Node.parent(editor, closestBlockEntry[1]);
        if (Slate.Element.isElement(closestBlockEntry[0]) && editor.isDefaultElement(closestBlockEntry[0]) && TableCellElement.isTableCellElement(parent)) {
          cellElement = parent;
          cellPath = Slate.Path.parent(closestBlockEntry[1]);
        } else {
          return onKeyDown(event);
        }
      } else {
        [cellElement, cellPath] = closestBlockEntry;
      }
      if (cellPath.length < 3) {
        return onKeyDown(event);
      }
      const [rowIndex, columnIndex] = TableElementSelection.getTableCellCoordinates(editor, cellElement);
      const tablePath = cellPath.slice(0, -2);
      const tableElement = Slate.Node.get(editor, tablePath);
      const rowCount = TableElement.getTableElementRowCount(tableElement);
      const columnCount = TableElement.getTableElementColumnCount(tableElement);
      if (rowCount === 0 || columnCount === 0) {
        return;
      }
      const moveSelectionBeforeTable = () => {
        const pointBeforeTable = Slate.Editor.before(editor, tablePath);
        if (!pointBeforeTable) {
          return onKeyDown(event);
        }
        event.preventDefault();
        return Slate.Transforms.select(editor, pointBeforeTable);
      };
      const moveSelectionAfterTable = () => {
        const pointAfterTable = Slate.Editor.after(editor, tablePath);
        if (!pointAfterTable) {
          return onKeyDown(event);
        }
        event.preventDefault();
        return Slate.Transforms.select(editor, pointAfterTable);
      };
      if (event.key === "Tab") {
        if (event.shiftKey) {
          if (columnIndex > 0) {
            event.preventDefault();
            return TableElementSelection.selectTableCellContents(editor, tablePath, rowIndex, columnIndex - 1);
          } else if (columnIndex === 0) {
            if (rowIndex > 0) {
              event.preventDefault();
              return TableElementSelection.selectTableCellContents(editor, tablePath, rowIndex - 1, columnCount - 1);
            }
            return moveSelectionBeforeTable();
          }
        } else {
          if (columnIndex < columnCount - 1) {
            event.preventDefault();
            return TableElementSelection.selectTableCellContents(editor, tablePath, rowIndex, columnIndex + 1);
          } else if (columnIndex === columnCount - 1) {
            if (rowIndex < rowCount - 1) {
              event.preventDefault();
              return TableElementSelection.selectTableCellContents(editor, tablePath, rowIndex + 1, 0);
            } else if (rowIndex === rowCount - 1) {
              return moveSelectionAfterTable();
            }
          }
        }
      }
      if (event.key === "ArrowLeft") {
        if (!Slate.Range.isCollapsed(selection)) {
          return onKeyDown(event);
        }
        if (Slate.Point.equals(selection.focus, Slate.Editor.start(editor, cellPath))) {
          if (columnIndex > 0) {
            event.preventDefault();
            return Slate.Transforms.select(editor, Slate.Editor.end(editor, [...tablePath, rowIndex, columnIndex - 1]));
          } else if (columnIndex === 0) {
            if (rowIndex > 0) {
              event.preventDefault();
              return Slate.Transforms.select(editor, Slate.Editor.end(editor, [...tablePath, rowIndex - 1, columnCount - 1]));
            }
            return moveSelectionBeforeTable();
          }
        }
      } else if (event.key === "ArrowRight") {
        if (!Slate.Range.isCollapsed(selection)) {
          return onKeyDown(event);
        }
        if (Slate.Point.equals(selection.focus, Slate.Editor.end(editor, cellPath))) {
          if (columnIndex < columnCount - 1) {
            event.preventDefault();
            return Slate.Transforms.select(editor, Slate.Editor.start(editor, [...tablePath, rowIndex, columnIndex + 1]));
          } else if (columnIndex === columnCount - 1) {
            if (rowIndex < rowCount - 1) {
              event.preventDefault();
              return Slate.Transforms.select(editor, Slate.Editor.start(editor, [...tablePath, rowIndex + 1, 0]));
            } else if (rowIndex === rowCount - 1) {
              return moveSelectionAfterTable();
            }
          }
        }
      } else if (event.key === "ArrowUp" || event.key === "ArrowDown") {
        const withinDefault = editor.isDefaultElement(closestBlockEntry[0]);
        if (withinDefault) {
          const relative = Slate.Path.relative(closestBlockEntry[1], cellPath);
          const defaultIndex = relative[0];
          if (event.key === "ArrowUp" && defaultIndex !== 0 || event.key === "ArrowDown" && defaultIndex !== cellElement.children.length - 1) {
            return onKeyDown(event);
          }
        }
        if (event.key === "ArrowUp") {
          if (rowIndex > 0) {
            event.preventDefault();
            const rowAboveEndPoint = Slate.Editor.end(editor, [...tablePath, rowIndex - 1, columnIndex]);
            if (!Slate.Range.isCollapsed(selection)) {
              return Slate.Transforms.select(editor, rowAboveEndPoint);
            }
            const minOffset = Math.min(selection.focus.offset, rowAboveEndPoint.offset);
            return Slate.Transforms.select(editor, {
              path: rowAboveEndPoint.path,
              offset: minOffset
            });
          }
          return moveSelectionBeforeTable();
        } else if (event.key === "ArrowDown") {
          if (rowIndex < rowCount - 1) {
            event.preventDefault();
            const rowBelowStatPoint = Slate.Editor.start(editor, [...tablePath, rowIndex + 1, columnIndex]);
            if (!Slate.Range.isCollapsed(selection)) {
              return Slate.Transforms.select(editor, rowBelowStatPoint);
            }
            const [rowBelowStatPointNode] = Slate.Editor.node(editor, rowBelowStatPoint);
            return Slate.Transforms.select(editor, {
              path: rowBelowStatPoint.path,
              offset: Math.min(selection.focus.offset, rowBelowStatPointNode.text.length)
            });
          }
          return moveSelectionAfterTable();
        }
      } else if (event.ctrlKey || event.metaKey) {
        if (event.key === "Backspace") {
          event.preventDefault();
          if (event.altKey) {
            TableModifications.TableModifications.deleteTableColumn(editor, tableElement, columnIndex);
            if (columnCount > 1) {
              const columnBefore = Slate.Editor.start(editor, [...tablePath, rowIndex, Math.max(0, columnIndex - 1)]);
              return Slate.Transforms.select(editor, columnBefore);
            } else {
              return moveSelectionBeforeTable();
            }
          } else {
            TableModifications.TableModifications.deleteTableRow(editor, tableElement, rowIndex);
            if (rowCount > 1) {
              const rowAbove = Slate.Editor.start(editor, [...tablePath, Math.max(0, rowIndex - 1), columnIndex]);
              return Slate.Transforms.select(editor, rowAbove);
            } else {
              return moveSelectionBeforeTable();
            }
          }
        } else if (event.key === "Enter") {
          event.preventDefault();
          if (event.altKey) {
            if (event.shiftKey) {
              TableModifications.TableModifications.addTableColumn(editor, tableElement, columnIndex);
              const columnBefore = Slate.Editor.start(editor, [...tablePath, rowIndex, Math.max(0, columnIndex)]);
              return Slate.Transforms.select(editor, columnBefore);
            } else {
              TableModifications.TableModifications.addTableColumn(editor, tableElement, columnIndex + 1);
              const columnAfter = Slate.Editor.start(editor, [...tablePath, rowIndex, columnIndex + 1]);
              return Slate.Transforms.select(editor, columnAfter);
            }
          } else {
            if (event.shiftKey) {
              TableModifications.TableModifications.addTableRow(editor, tableElement, rowIndex);
              const rowAbove = Slate.Editor.start(editor, [...tablePath, Math.max(0, rowIndex), columnIndex]);
              return Slate.Transforms.select(editor, rowAbove);
            } else {
              TableModifications.TableModifications.addTableRow(editor, tableElement, rowIndex + 1);
              const rowBelowStat = Slate.Editor.start(editor, [...tablePath, rowIndex + 1, columnIndex]);
              return Slate.Transforms.select(editor, rowBelowStat);
            }
          }
        }
      }
      onKeyDown(event);
    }
  });
  return editor;
};
exports.withTables = withTables;
//# sourceMappingURL=withTables.cjs.map
