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 _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
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 _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 classNames from "clsx";
import React, { useEffect, useMemo } from "react";
import { MOBILE_BREAKPOINT } from "../../../../Constants/mobile";
import { TABLE_ROW_INDEX_COL_WIDTH, TableColgroup } from "./TableColgroup";
import { TableRowIndex } from "./TableRowIndex";
import useScrollShadow from "./useScrollShadow";
import { getEditableTableMinWidth } from "./utils/editableTableWidth";
import { useEditableTableColWidths } from "./utils/useEditableTableColWidths";
import { useEditableTableContentWidth } from "./utils/useEditableTableContentWidth";
var TABLE_MIN_CONTAINER_WIDTH = 200;
export var EditableTable = function EditableTable(param) {
    var baseCls = param.baseCls, children = param.children, tablePath = param.tablePath, tableNode = param.tableNode, markdownContainerRef = param.markdownContainerRef, tableCssVariables = param.tableCssVariables;
    var _tableNode_children__children, _tableNode_children_, _tableNode_children;
    var columnCount = (tableNode === null || tableNode === void 0 ? void 0 : (_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;
    var mobileBreakpointValue = parseInt(MOBILE_BREAKPOINT, 10) || 768;
    var _useScrollShadow = _sliced_to_array(useScrollShadow(), 2), tableRef = _useScrollShadow[0], scrollState = _useScrollShadow[1];
    var _useEditableTableContentWidth = useEditableTableContentWidth({
        readonly: false,
        markdownContainerRef: markdownContainerRef,
        minContainerWidth: TABLE_MIN_CONTAINER_WIDTH
    }), resolvedContentWidth = _useEditableTableContentWidth.resolvedContentWidth, availableTableWidth = _useEditableTableContentWidth.availableTableWidth;
    var colWidths = useEditableTableColWidths({
        readonly: false,
        columnCount: columnCount,
        availableTableWidth: availableTableWidth,
        mobileBreakpointValue: mobileBreakpointValue,
        element: tableNode
    });
    useEffect(function() {
        var resize = function resize() {
            if (process.env.NODE_ENV === 'test') return;
            var dom = tableRef.current;
            if (!dom) return;
            var requiredMinWidth = getEditableTableMinWidth({
                columnCount: columnCount,
                colWidths: colWidths,
                availableTableWidth: availableTableWidth,
                mobileBreakpointValue: mobileBreakpointValue,
                resolvedContentWidth: resolvedContentWidth,
                minContainerWidth: TABLE_MIN_CONTAINER_WIDTH,
                rowIndexColWidth: TABLE_ROW_INDEX_COL_WIDTH
            });
            dom.style.minWidth = "".concat(requiredMinWidth, "px");
        };
        document.addEventListener('md-resize', resize);
        window.addEventListener('resize', resize);
        resize();
        return function() {
            document.removeEventListener('md-resize', resize);
            window.removeEventListener('resize', resize);
        };
    }, [
        tableRef,
        columnCount,
        colWidths,
        availableTableWidth,
        mobileBreakpointValue,
        resolvedContentWidth
    ]);
    useEffect(function() {
        document.dispatchEvent(new CustomEvent('md-resize', {
            detail: {}
        }));
    }, []);
    var tableDom = useMemo(function() {
        return /*#__PURE__*/ React.createElement("table", {
            className: classNames("".concat(baseCls, "-editor-table")),
            onDragStart: function onDragStart(e) {
                e.preventDefault();
                return false;
            }
        }, /*#__PURE__*/ React.createElement(TableColgroup, {
            colWidths: colWidths,
            prefixColWidth: TABLE_ROW_INDEX_COL_WIDTH
        }), /*#__PURE__*/ React.createElement("tbody", null, /*#__PURE__*/ React.createElement(TableRowIndex, {
            colWidths: colWidths,
            columnCount: columnCount,
            tablePath: tablePath
        }), children));
    }, [
        baseCls,
        colWidths,
        columnCount,
        tablePath,
        children
    ]);
    var boxShadowStyle = useMemo(function() {
        return {
            flex: 1,
            minWidth: 0,
            boxShadow: "\n      ".concat(scrollState.vertical.hasScroll && !scrollState.vertical.isAtStart ? 'inset 0 8px 8px -8px rgba(0,0,0,0.1)' : '', "\n      ").concat(scrollState.vertical.hasScroll && !scrollState.vertical.isAtEnd ? 'inset 0 -8px 8px -8px rgba(0,0,0,0.1)' : '', "\n      ").concat(scrollState.horizontal.hasScroll && !scrollState.horizontal.isAtStart ? 'inset 8px 0 8px -8px rgba(0,0,0,0.1)' : '', "\n      ").concat(scrollState.horizontal.hasScroll && !scrollState.horizontal.isAtEnd ? 'inset -8px 0 8px -8px rgba(0,0,0,0.1)' : '', "\n    ")
        };
    }, [
        scrollState
    ]);
    return /*#__PURE__*/ React.createElement("div", {
        className: classNames(baseCls),
        ref: tableRef,
        style: _object_spread(_object_spread_props(_object_spread({}, boxShadowStyle), {
            position: 'relative'
        }), tableCssVariables),
        onDragStart: function onDragStart(e) {
            e.preventDefault();
        },
        onDoubleClick: function onDoubleClick(e) {
            e.preventDefault();
        }
    }, tableDom);
};
