/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

import { __extends, __spreadArray, __read, __rest, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { InView } from 'react-intersection-observer';
import { isClickOnInput, isObject, autobind } from 'amis-core';
import Checkbox from '../Checkbox.js';
import { Icon } from '../icons.js';
import BodyCell from './Cell.js';
import { hasFixedColumn, updateFixedRow, getBuildColumns, levelsSplit } from './util.js';

/**
 * @file table/BodyRow
 * @author fex
 */
var BodyRow = /** @class */ (function (_super) {
    __extends(BodyRow, _super);
    function BodyRow() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domRef = React__default.createRef();
        return _this;
    }
    BodyRow.prototype.onExpand = function () {
        var _a = this.props, onExpand = _a.onExpand, data = _a.data;
        onExpand && onExpand([data]);
    };
    BodyRow.prototype.onCollapse = function () {
        var _a = this.props, onCollapse = _a.onCollapse, data = _a.data;
        onCollapse && onCollapse([data]);
    };
    BodyRow.prototype.onMouseEnter = function (event, record, rowIndex) {
        var onMouseEnter = this.props.onMouseEnter;
        onMouseEnter && onMouseEnter(event, record, rowIndex);
    };
    BodyRow.prototype.onMouseLeave = function (event, record, rowIndex) {
        var onMouseLeave = this.props.onMouseLeave;
        onMouseLeave && onMouseLeave(event, record, rowIndex);
    };
    BodyRow.prototype.onClick = function (e, record, rowIndex) {
        if (isClickOnInput(e) && !this.props.rowClickIgControl) {
            return;
        }
        var onClick = this.props.onClick;
        onClick && onClick(e, record, rowIndex);
    };
    BodyRow.prototype.onDoubleClick = function (event, record, rowIndex) {
        var onDoubleClick = this.props.onDoubleClick;
        onDoubleClick && onDoubleClick(event, record, rowIndex);
    };
    BodyRow.prototype.getExpandedIcons = function () {
        var _a = this.props, cx = _a.classnames, isExpanded = _a.isExpanded;
        return isExpanded ? (React__default.createElement("i", { className: cx('Table-expandBtn', 'is-active'), onClick: this.onCollapse },
            React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" }))) : (React__default.createElement("i", { className: cx('Table-expandBtn'), onClick: this.onExpand },
            React__default.createElement(Icon, { icon: "right-arrow-bold", className: "icon" })));
    };
    BodyRow.prototype.prependColumns = function (columns) {
        var _a = this.props, selectable = _a.selectable, expandable = _a.expandable, expandableFixed = _a.expandableFixed, draggable = _a.draggable, rowSelectionFixed = _a.rowSelectionFixed;
        if (draggable) {
            columns.unshift({});
        }
        else {
            if (expandable) {
                columns.unshift({ fixed: expandableFixed });
            }
            if (selectable) {
                columns.unshift({ fixed: rowSelectionFixed });
            }
        }
    };
    BodyRow.prototype.updateFixedRow = function () {
        var cx = this.props.classnames;
        var columns = __spreadArray([], __read(this.tdColumns), false);
        this.prependColumns(columns);
        if (hasFixedColumn(columns)) {
            updateFixedRow(this.domRef.current, columns, cx);
        }
    };
    BodyRow.prototype.componentDidMount = function () {
        this.updateFixedRow();
    };
    BodyRow.prototype.componentDidUpdate = function () {
        this.updateFixedRow();
    };
    BodyRow.prototype.onCheckChange = function (value) {
        var data = this.props.data;
        this.props.onChange && this.props.onChange(value, data);
        event && event.stopPropagation();
    };
    BodyRow.prototype.render = function () {
        var _this = this;
        var _a = this.props, cx = _a.classnames, isChecked = _a.isChecked, selectable = _a.selectable, expandable = _a.expandable, draggable = _a.draggable, indentSize = _a.indentSize, rowClassName = _a.rowClassName, lineHeight = _a.lineHeight, levels = _a.levels, columns = _a.columns, data = _a.data, isExpandable = _a.isExpandable, rowIndex = _a.rowIndex, keyField = _a.keyField, colCount = _a.colCount, isRightExpandable = _a.isRightExpandable, isLeftExpandable = _a.isLeftExpandable, classPrefix = _a.classPrefix, rowSelectionFixed = _a.rowSelectionFixed, rowSelectionType = _a.rowSelectionType, isExpandableRow = _a.isExpandableRow, hasChildrenRow = _a.hasChildrenRow, hasChildrenChecked = _a.hasChildrenChecked, expandedRowClassName = _a.expandedRowClassName, expandedRowRender = _a.expandedRowRender, isExpanded = _a.isExpanded; _a.childrenColumnName; var expandableFixed = _a.expandableFixed; _a.onExpand; _a.onCollapse; _a.onMouseEnter; _a.onMouseLeave; _a.onClick; _a.onChange; var testIdBuilder = _a.testIdBuilder, rest = __rest(_a, ["classnames", "isChecked", "selectable", "expandable", "draggable", "indentSize", "rowClassName", "lineHeight", "levels", "columns", "data", "isExpandable", "rowIndex", "keyField", "colCount", "isRightExpandable", "isLeftExpandable", "classPrefix", "rowSelectionFixed", "rowSelectionType", "isExpandableRow", "hasChildrenRow", "hasChildrenChecked", "expandedRowClassName", "expandedRowRender", "isExpanded", "childrenColumnName", "expandableFixed", "onExpand", "onCollapse", "onMouseEnter", "onMouseLeave", "onClick", "onChange", "testIdBuilder"]);
        var tdColumns = getBuildColumns(columns).tdColumns;
        this.tdColumns = tdColumns;
        var level = levelsSplit(levels).length;
        // 设置缩进效果
        var indentDom = levels.length > 0 ? (React__default.createElement("span", { className: cx('Table-row-indent', "indent-level-".concat(level)), style: (levels === null || levels === void 0 ? void 0 : levels.length) > 0 ? { width: indentSize * level + 'px' } : {} })) : null;
        var cells = tdColumns.map(function (item, i) {
            var _a, _b;
            // 为了支持灵活合并单元格，renderers层的Table2传递的render方法，返回{children: <ReactElement>, props: {rowSpan, colSpan}}
            // 但直接使用amis-ui的table，render方法一般直接返回ReactElement
            var cellIDBuilder = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("cell-".concat(i));
            var render = item.render && typeof item.render === 'function'
                ? item.render(data[item.name], data, rowIndex, i, levelsSplit(levels))
                : null;
            var props = { rowSpan: 1, colSpan: 1 };
            var children = render;
            if (render && !React__default.isValidElement(render) && isObject(render)) {
                if (render.props) {
                    props = render.props;
                }
                children = render.children;
                // 如果合并行 且有展开行，那么合并行不生效
                if (props.rowSpan > 1 && isExpandableRow && hasChildrenRow) {
                    props.rowSpan === 1;
                }
            }
            var className = typeof item.className === 'function'
                ? item.className(data, rowIndex)
                : '';
            return props.rowSpan === 0 || props.colSpan === 0 ? null : (React__default.createElement(BodyCell, __assign({ classnames: cx, classPrefix: classPrefix, key: i }, props, { fixed: item.fixed === true ? 'left' : item.fixed, column: item, className: cx((_a = {},
                    _a["".concat(className)] = !!className,
                    _a)), col: i > -1 ? i.toString() : undefined, testIdBuilder: cellIDBuilder }),
                React__default.createElement("div", { className: cx('Table-cell-wrapper', (_b = {},
                        _b[cx('Table-cell-wrapper-prefix')] = i === 0 &&
                            (!!indentDom || (levels.length === 0 && hasChildrenRow)),
                        _b[cx("Table-cell-height-".concat(lineHeight))] = !!lineHeight,
                        _b)) },
                    i === 0 && levels.length > 0 ? indentDom : null,
                    i === 0 && hasChildrenRow ? _this.getExpandedIcons() : null,
                    render ? children : data[item.name])));
        });
        var rowClassNameClass = rowClassName && typeof rowClassName === 'function'
            ? rowClassName(data, rowIndex)
            : '';
        // 可展开和嵌套不能同时支持
        // 设置了expandable 数据源里有children也就不生效了
        // 拖拽排序 可选、可展开都先不支持了，可以支持嵌套展示
        var children = !draggable && isExpandableRow && isExpanded ? (React__default.createElement("tr", { key: "expandedRow", className: cx('Table-expanded-row', expandedRowClassName) },
            React__default.createElement(BodyCell, { key: "expanded", classnames: cx, classPrefix: classPrefix, colSpan: tdColumns.length + colCount }, expandable &&
                expandedRowRender &&
                typeof expandedRowRender === 'function'
                ? expandedRowRender(data, rowIndex)
                : null))) : null;
        var isRadio = rowSelectionType === 'radio';
        var expandableCell = !draggable && isExpandable ? (React__default.createElement(BodyCell, { key: "expandable", classnames: cx, classPrefix: classPrefix, fixed: expandable && expandableFixed
                ? isRightExpandable
                    ? 'right'
                    : 'left'
                : '', className: cx('Table-cell-expand-icon-cell') }, isExpandableRow || hasChildrenRow ? this.getExpandedIcons() : null)) : null;
        return [
            React__default.createElement("tr", { ref: this.domRef, key: "".concat(data[keyField || 'key'] || rowIndex), "row-index": rowIndex, "row-levels": levels, className: cx('Table-row', "Table-row-level-".concat(level), rowClassNameClass, {
                    'Table-row-disabled': !!rest.disabled
                }), onMouseEnter: function (e) { return _this.onMouseEnter(e, data, rowIndex); }, onMouseLeave: function (e) { return _this.onMouseLeave(e, data, rowIndex); }, onClick: function (e) { return _this.onClick(e, data, rowIndex); }, onDoubleClick: function (e) { return _this.onDoubleClick(e, data, rowIndex); } },
                draggable ? (React__default.createElement(BodyCell, { key: "drag", classPrefix: classPrefix, classnames: cx, className: cx('Table-dragCell') },
                    React__default.createElement(Icon, { icon: "drag-bar", className: "icon" }))) : null,
                !draggable && selectable ? (React__default.createElement(BodyCell, { key: "select", classPrefix: classPrefix, classnames: cx, fixed: rowSelectionFixed ? 'left' : '', className: cx('Table-checkCell') },
                    React__default.createElement(Checkbox, __assign({ name: 'Table-checkbox', type: rowSelectionType || 'checkbox', partial: !isRadio && hasChildrenChecked && !isChecked, checked: isRadio ? isChecked : hasChildrenChecked || isChecked, onChange: this.onCheckChange }, rest)))) : null,
                isLeftExpandable ? expandableCell : null,
                cells,
                isRightExpandable ? expandableCell : null),
            children
        ];
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], BodyRow.prototype, "onExpand", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], BodyRow.prototype, "onCollapse", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Boolean]),
        __metadata("design:returntype", void 0)
    ], BodyRow.prototype, "onCheckChange", null);
    return BodyRow;
}(React__default.PureComponent));
var LazyRow = /** @class */ (function (_super) {
    __extends(LazyRow, _super);
    function LazyRow(props) {
        var _this = _super.call(this, props) || this;
        var lazyRenderAfter = props.lazyRenderAfter, rowIndex = props.rowIndex;
        _this.state = { visible: rowIndex + 1 < lazyRenderAfter };
        return _this;
    }
    LazyRow.prototype.handleVisibleChange = function (visible, entry) {
        this.setState({
            visible: visible
        });
    };
    LazyRow.prototype.render = function () {
        var _this = this;
        var visible = this.state.visible;
        var _a = this.props, columns = _a.columns, lazyRenderAfter = _a.lazyRenderAfter, rowIndex = _a.rowIndex, cx = _a.classnames;
        var tdColumns = getBuildColumns(columns).tdColumns;
        return (React__default.createElement(InView, { onChange: this.handleVisibleChange, 
            // 如果是嵌套层 默认从当前层来计算
            skip: rowIndex + 1 < lazyRenderAfter }, function (_a) {
            var ref = _a.ref;
            return visible ? (React__default.createElement(BodyRow, __assign({}, _this.props))) : (React__default.createElement("tr", { ref: ref }, tdColumns.map(function (column, index) {
                return (React__default.createElement("td", { key: "empty-cell-".concat(index) },
                    React__default.createElement("div", { className: cx('Table-emptyBlock') }, "\u00A0")));
            })));
        }));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Boolean, Object]),
        __metadata("design:returntype", void 0)
    ], LazyRow.prototype, "handleVisibleChange", null);
    return LazyRow;
}(React__default.PureComponent));

export { LazyRow as default };
