function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { Editor, Transforms } from "slate";
import { ReactEditor } from "slate-react";
import { NativeTableEditor } from "../../../../utils/native-table";
var EMPTY_TABLE_CELL = {
    type: 'table-cell',
    children: [
        {
            type: 'paragraph',
            children: [
                {
                    text: ''
                }
            ]
        }
    ]
};
function getTableNode(editor, tablePath) {
    var _Editor_node = _sliced_to_array(Editor.node(editor, tablePath), 1), tableNode = _Editor_node[0];
    if (!tableNode || tableNode.type !== 'table') {
        return null;
    }
    return tableNode;
}
function getTableRowCount(tableNode) {
    var _tableNode_children;
    return ((_tableNode_children = tableNode.children) === null || _tableNode_children === void 0 ? void 0 : _tableNode_children.length) || 0;
}
function getTableColumnCount(tableNode) {
    var _tableNode_children__children, _tableNode_children_, _tableNode_children;
    return ((_tableNode_children = tableNode.children) === null || _tableNode_children === void 0 ? void 0 : (_tableNode_children_ = _tableNode_children[0]) === null || _tableNode_children_ === void 0 ? void 0 : (_tableNode_children__children = _tableNode_children_.children) === null || _tableNode_children__children === void 0 ? void 0 : _tableNode_children__children.length) || 0;
}
function forEachCellPath(editor, tablePath, callback) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    var rowCount = getTableRowCount(tableNode);
    for(var rowIndex = 0; rowIndex < rowCount; rowIndex += 1){
        var _tableNode_children, _rowNode_children;
        var rowNode = (_tableNode_children = tableNode.children) === null || _tableNode_children === void 0 ? void 0 : _tableNode_children[rowIndex];
        var columnCount = (rowNode === null || rowNode === void 0 ? void 0 : (_rowNode_children = rowNode.children) === null || _rowNode_children === void 0 ? void 0 : _rowNode_children.length) || 0;
        for(var columnIndex = 0; columnIndex < columnCount; columnIndex += 1){
            var cellPath = _to_consumable_array(tablePath).concat([
                rowIndex,
                columnIndex
            ]);
            if (!Editor.hasPath(editor, cellPath)) {
                continue;
            }
            var _Editor_node = _sliced_to_array(Editor.node(editor, cellPath), 1), cellNode = _Editor_node[0];
            var tableCellNode = cellNode;
            if (tableCellNode.type !== 'table-cell') {
                continue;
            }
            callback(cellPath, tableCellNode);
        }
    }
}
export function clearTableSelection(editor, tablePath) {
    forEachCellPath(editor, tablePath, function(_, cellNode) {
        try {
            var domNode = ReactEditor.toDOMNode(editor, cellNode);
            domNode === null || domNode === void 0 ? void 0 : domNode.removeAttribute('data-select');
        } catch (unused) {
        // cell may not be mounted yet
        }
    });
}
export function selectTableRow(editor, tablePath, rowIndex) {
    var _tableNode_children, _rowNode_children;
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    var rowNode = (_tableNode_children = tableNode.children) === null || _tableNode_children === void 0 ? void 0 : _tableNode_children[rowIndex];
    if (!(rowNode === null || rowNode === void 0 ? void 0 : (_rowNode_children = rowNode.children) === null || _rowNode_children === void 0 ? void 0 : _rowNode_children.length)) return;
    for(var columnIndex = 0; columnIndex < rowNode.children.length; columnIndex += 1){
        var cellPath = _to_consumable_array(tablePath).concat([
            rowIndex,
            columnIndex
        ]);
        if (!Editor.hasPath(editor, cellPath)) {
            continue;
        }
        var _Editor_node = _sliced_to_array(Editor.node(editor, cellPath), 1), cellNode = _Editor_node[0];
        var tableCellNode = cellNode;
        if (tableCellNode.type !== 'table-cell') {
            continue;
        }
        try {
            var domNode = ReactEditor.toDOMNode(editor, tableCellNode);
            domNode === null || domNode === void 0 ? void 0 : domNode.setAttribute('data-select', 'true');
        } catch (unused) {
        // cell may not be mounted yet
        }
    }
}
export function selectTableColumn(editor, tablePath, columnIndex) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    var rowCount = getTableRowCount(tableNode);
    for(var rowIndex = 0; rowIndex < rowCount; rowIndex += 1){
        var cellPath = _to_consumable_array(tablePath).concat([
            rowIndex,
            columnIndex
        ]);
        if (!Editor.hasPath(editor, cellPath)) {
            continue;
        }
        var _Editor_node = _sliced_to_array(Editor.node(editor, cellPath), 1), cellNode = _Editor_node[0];
        var tableCellNode = cellNode;
        if (tableCellNode.type !== 'table-cell') {
            continue;
        }
        try {
            var domNode = ReactEditor.toDOMNode(editor, tableCellNode);
            domNode === null || domNode === void 0 ? void 0 : domNode.setAttribute('data-select', 'true');
        } catch (unused) {
        // cell may not be mounted yet
        }
    }
}
export function selectWholeTable(editor, tablePath) {
    forEachCellPath(editor, tablePath, function(_, cellNode) {
        try {
            var domNode = ReactEditor.toDOMNode(editor, cellNode);
            domNode === null || domNode === void 0 ? void 0 : domNode.setAttribute('data-select', 'true');
        } catch (unused) {
        // cell may not be mounted yet
        }
    });
}
export function removeTableRow(editor, tablePath, rowIndex) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    if (getTableRowCount(tableNode) <= 1) {
        NativeTableEditor.removeTable(editor, tablePath);
        return;
    }
    var rowPath = _to_consumable_array(tablePath).concat([
        rowIndex
    ]);
    if (Editor.hasPath(editor, rowPath)) {
        Transforms.removeNodes(editor, {
            at: rowPath
        });
    }
}
export function insertTableRow(editor, tablePath, rowIndex, position) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    var rowCount = getTableRowCount(tableNode);
    var columnCount = getTableColumnCount(tableNode);
    if (columnCount <= 0) return;
    var rowNode = {
        type: 'table-row',
        children: Array.from({
            length: columnCount
        }, function() {
            return EMPTY_TABLE_CELL;
        })
    };
    var insertIndex = position === 'before' ? rowIndex : Math.min(rowIndex + 1, rowCount);
    Transforms.insertNodes(editor, rowNode, {
        at: _to_consumable_array(tablePath).concat([
            insertIndex
        ])
    });
}
export function removeTableColumn(editor, tablePath, columnIndex) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    if (getTableColumnCount(tableNode) <= 1) {
        NativeTableEditor.removeTable(editor, tablePath);
        return;
    }
    var rowCount = getTableRowCount(tableNode);
    for(var rowIndex = 0; rowIndex < rowCount; rowIndex += 1){
        var cellPath = _to_consumable_array(tablePath).concat([
            rowIndex,
            columnIndex
        ]);
        if (Editor.hasPath(editor, cellPath)) {
            Transforms.removeNodes(editor, {
                at: cellPath
            });
        }
    }
}
export function insertTableColumn(editor, tablePath, columnIndex, position) {
    var tableNode = getTableNode(editor, tablePath);
    if (!tableNode) return;
    var rowCount = getTableRowCount(tableNode);
    var insertOffset = position === 'before' ? 0 : 1;
    for(var rowIndex = 0; rowIndex < rowCount; rowIndex += 1){
        var insertPath = _to_consumable_array(tablePath).concat([
            rowIndex,
            columnIndex + insertOffset
        ]);
        Transforms.insertNodes(editor, EMPTY_TABLE_CELL, {
            at: insertPath
        });
    }
}
