"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableContent = void 0;
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var TableRow_1 = require("./TableRow");
var tpl_1 = require("../../utils/tpl");
var mobx_1 = require("mobx");
var helper_1 = require("../../utils/helper");
var TableContent = /** @class */ (function (_super) {
    tslib_1.__extends(TableContent, _super);
    function TableContent(props) {
        var _this = _super.call(this, props) || this;
        var rows = props.rows;
        _this.reaction = mobx_1.reaction(function () {
            return "" + helper_1.flattenTree(rows)
                .map(function (item) { return "" + item.id; })
                .join(',') + rows
                .filter(function (item) { return item.checked; })
                .map(function (item) { return item.id; })
                .join(',');
        }, function () { return _this.forceUpdate(); }, {
            onError: function () { return _this.reaction(); }
        });
        return _this;
    }
    TableContent.prototype.shouldComponentUpdate = function (nextProps) {
        var props = this.props;
        if (props.columns !== nextProps.columns) {
            return true;
        }
        return false;
    };
    TableContent.prototype.componentwillUnmount = function () {
        var _a;
        (_a = this.reaction) === null || _a === void 0 ? void 0 : _a.call(this);
    };
    TableContent.prototype.renderRows = function (rows, columns, rowProps) {
        var _this = this;
        if (columns === void 0) { columns = this.props.columns; }
        if (rowProps === void 0) { rowProps = {}; }
        var _a = this.props, rowClassName = _a.rowClassName, rowClassNameExpr = _a.rowClassNameExpr, onAction = _a.onAction, buildItemProps = _a.buildItemProps, checkOnItemClick = _a.checkOnItemClick, cx = _a.classnames, render = _a.render, renderCell = _a.renderCell, onCheck = _a.onCheck, onQuickChange = _a.onQuickChange, footable = _a.footable, footableColumns = _a.footableColumns;
        return rows.map(function (item, rowIndex) {
            var itemProps = buildItemProps ? buildItemProps(item, rowIndex) : null;
            var doms = [
                react_1.default.createElement(TableRow_1.TableRow, tslib_1.__assign({}, itemProps, { classnames: cx, checkOnItemClick: checkOnItemClick, key: item.id, itemIndex: rowIndex, item: item, itemClassName: cx(rowClassNameExpr
                        ? tpl_1.filter(rowClassNameExpr, item.data)
                        : rowClassName, {
                        'is-last': item.depth > 1 && rowIndex === rows.length - 1
                    }), columns: columns, renderCell: renderCell, render: render, onAction: onAction, onCheck: onCheck, 
                    // todo 先注释 quickEditEnabled={item.depth === 1}
                    onQuickChange: onQuickChange }, rowProps))
            ];
            if (footable && footableColumns.length) {
                if (item.depth === 1) {
                    doms.push(react_1.default.createElement(TableRow_1.TableRow, tslib_1.__assign({}, itemProps, { classnames: cx, checkOnItemClick: checkOnItemClick, key: "foot-" + item.id, itemIndex: rowIndex, item: item, itemClassName: cx(rowClassNameExpr
                            ? tpl_1.filter(rowClassNameExpr, item.data)
                            : rowClassName), columns: footableColumns, renderCell: renderCell, render: render, onAction: onAction, onCheck: onCheck, footableMode: true, footableColSpan: columns.length, onQuickChange: onQuickChange }, rowProps)));
                }
            }
            else if (item.children.length) {
                // 嵌套表格
                doms.push.apply(doms, _this.renderRows(item.children, columns, tslib_1.__assign(tslib_1.__assign({}, rowProps), { parent: item })));
            }
            return doms;
        });
    };
    TableContent.prototype.render = function () {
        var _a = this.props, placeholder = _a.placeholder, cx = _a.classnames, render = _a.render, className = _a.className, columns = _a.columns, columnsGroup = _a.columnsGroup, onMouseMove = _a.onMouseMove, onScroll = _a.onScroll, tableRef = _a.tableRef, rows = _a.rows, renderHeadCell = _a.renderHeadCell;
        var tableClassName = cx('Table-table', this.props.tableClassName);
        var hideHeader = columns.every(function (column) { return !column.label; });
        return (react_1.default.createElement("div", { onMouseMove: onMouseMove, className: cx('Table-content', className), onScroll: onScroll },
            react_1.default.createElement("table", { ref: tableRef, className: tableClassName },
                react_1.default.createElement("thead", null,
                    columnsGroup.length ? (react_1.default.createElement("tr", null, columnsGroup.map(function (item, index) { return (react_1.default.createElement("th", { key: index, "data-index": item.index, colSpan: item.colSpan }, item.label ? render('tpl', item.label) : null)); }))) : null,
                    react_1.default.createElement("tr", { className: hideHeader ? 'fake-hide' : '' }, columns.map(function (column) {
                        return renderHeadCell(column, {
                            'data-index': column.index,
                            'key': column.index
                        });
                    }))),
                react_1.default.createElement("tbody", null, rows.length ? (this.renderRows(rows, columns)) : (react_1.default.createElement("tr", { className: cx('Table-placeholder') },
                    react_1.default.createElement("td", { colSpan: columns.length }, render('placeholder', placeholder || '暂无数据'))))))));
    };
    return TableContent;
}(react_1.default.Component));
exports.TableContent = TableContent;
//# sourceMappingURL=./renderers/Table/TableContent.js.map
