/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __rest, __assign } from 'tslib';
import React__default from 'react';
import { hasFixedColumn, updateFixedRow } from './util.js';
import BodyCell from './Cell.js';

/**
 * @file table/SummaryRow
 * @author fex
 */
var SummaryRow = /** @class */ (function (_super) {
    __extends(SummaryRow, _super);
    function SummaryRow() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domRef = React__default.createRef();
        return _this;
    }
    SummaryRow.prototype.updateFixedRow = function () {
        var _a = this.props, cx = _a.classnames, row = _a.row;
        if (row && hasFixedColumn(row)) {
            var tr = this.domRef.current;
            updateFixedRow(tr, row || [], cx);
        }
    };
    SummaryRow.prototype.componentDidMount = function () {
        this.updateFixedRow();
    };
    SummaryRow.prototype.componentDidUpdate = function () {
        this.updateFixedRow();
    };
    SummaryRow.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, dataSource = _a.dataSource, classPrefix = _a.classPrefix, row = _a.row, colCount = _a.colCount, isRightExpandable = _a.isRightExpandable, onMouseEnter = _a.onMouseEnter, onMouseLeave = _a.onMouseLeave;
        var cells = [];
        var extraCount = isRightExpandable ? 1 : 0;
        row === null || row === void 0 ? void 0 : row.forEach(function (s, index) {
            cells.push(React__default.createElement(BodyCell, { classnames: cx, classPrefix: classPrefix, className: s.cellClassName, key: 'summary-cell-' + index, fixed: s.fixed, colSpan: cells.length === 0
                    ? (s.colSpan || 1) + colCount - extraCount
                    : index === row.length - 1
                        ? (s.colSpan || 1) + extraCount
                        : s.colSpan }, typeof s.render === 'function' ? s.render(dataSource) : s.render));
        });
        return (React__default.createElement("tr", { ref: this.domRef, onMouseEnter: function (e) { return onMouseEnter && onMouseEnter(e); }, onMouseLeave: function (e) { return onMouseLeave && onMouseLeave(e); }, className: cx('Table-summary-row') }, cells));
    };
    return SummaryRow;
}(React__default.PureComponent));
var SummaryRows = /** @class */ (function (_super) {
    __extends(SummaryRows, _super);
    function SummaryRows() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    SummaryRows.prototype.render = function () {
        var _a = this.props, summary = _a.summary, rest = __rest(_a, ["summary"]);
        var rows = [[]];
        if (Array.isArray(summary)) {
            summary.forEach(function (s) {
                if (Array.isArray(s)) {
                    rows.push(s);
                }
                else {
                    rows[0].push(s);
                }
            });
        }
        return rows.map(function (row, index) { return (React__default.createElement(SummaryRow, __assign({ key: "summary-".concat(index) }, rest, { row: row }))); });
    };
    return SummaryRows;
}(React__default.PureComponent));

export { SummaryRow, SummaryRows as default };
