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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
var React = require('react');
var util = require('./util.js');
var Checkbox = require('../Checkbox.js');
var Cell = require('./Cell.js');
var HeadCellSort = require('./HeadCellSort.js');
var HeadCellFilter = require('./HeadCellFilter.js');
var HeadCellSelect = require('./HeadCellSelect.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var Head = /** @class */ (function (_super) {
    tslib.__extends(Head, _super);
    function Head() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domRef = React__default["default"].createRef();
        return _this;
    }
    Head.prototype.prependColumns = function (columns) {
        var _a = this.props, rowSelectionFixed = _a.rowSelectionFixed, expandableFixed = _a.expandableFixed, draggable = _a.draggable, selectable = _a.selectable, expandable = _a.expandable;
        if (expandable) {
            columns.unshift({});
        }
        if (draggable || selectable) {
            columns.unshift({});
        }
        else {
            if (expandableFixed) {
                columns.unshift({ fixed: expandableFixed });
            }
            if (rowSelectionFixed) {
                columns.unshift({ fixed: true });
            }
        }
    };
    Head.prototype.updateFixedRow = function () {
        var _a = this.props, cx = _a.classnames, selfSticky = _a.selfSticky;
        var thead = this.domRef.current;
        var children = thead === null || thead === void 0 ? void 0 : thead.children;
        for (var i = 0; i < ((children === null || children === void 0 ? void 0 : children.length) || 0); i++) {
            var cols = tslib.__spreadArray([], tslib.__read(this.thColumns[i]), false);
            if (i === 0) {
                this.prependColumns(cols);
            }
            if (selfSticky) {
                util.updateStickyRow(children, i);
            }
            if (util.hasFixedColumn(cols)) {
                util.updateFixedRow(children === null || children === void 0 ? void 0 : children[i], cols, cx);
            }
        }
    };
    Head.prototype.componentDidMount = function () {
        this.updateFixedRow();
    };
    Head.prototype.componentDidUpdate = function () {
        this.updateFixedRow();
    };
    Head.prototype.render = function () {
        var _this = this;
        var _a = this.props, dataSource = _a.dataSource, cx = _a.classnames, classPrefix = _a.classPrefix, expandable = _a.expandable, draggable = _a.draggable, resizable = _a.resizable, selectable = _a.selectable, rowSelectionKeyField = _a.rowSelectionKeyField, isExpandable = _a.isExpandable, childrenColumnName = _a.childrenColumnName, selectedRowKeys = _a.selectedRowKeys, maxSelectedLength = _a.maxSelectedLength, rowSelectionFixed = _a.rowSelectionFixed, rowSelectionType = _a.rowSelectionType, popOverContainer = _a.popOverContainer, isRightExpandable = _a.isRightExpandable, isLeftExpandable = _a.isLeftExpandable, orderBy = _a.orderBy, columns = _a.columns, selections = _a.selections, expandableFixed = _a.expandableFixed, onSort = _a.onSort, onSelectAll = _a.onSelectAll, onFilter = _a.onFilter, onResizeMouseDown = _a.onResizeMouseDown, testIdBuilder = _a.testIdBuilder, className = _a.className, selfSticky = _a.selfSticky;
        var _b = util.getBuildColumns(columns), thColumns = _b.thColumns, tdColumns = _b.tdColumns;
        this.thColumns = thColumns;
        this.tdColumns = tdColumns;
        // 获取一行最多th个数
        var maxCount = 0;
        this.thColumns.forEach(function (cols) {
            if (cols.length > maxCount) {
                maxCount = cols.length;
            }
        });
        var expandableCell = !draggable && isExpandable ? (_J$X_(Cell["default"], { key: "expandable", wrapperComponent: "th", rowSpan: columns.length, fixed: expandableFixed ? 'left' : '', className: cx('Table-row-expand-icon-cell'), classnames: cx, classPrefix: classPrefix })) : null;
        var _c = util.getAllSelectableRows(dataSource, rowSelectionKeyField, childrenColumnName, expandable, selectedRowKeys, maxSelectedLength), rows = _c.rows, rowKeys = _c.rowKeys, restSelectedKeys = _c.restSelectedKeys;
        // 从renderers的table传来的数据 可能不在当前页 因此需要过滤一下
        var selectedKeys = selectedRowKeys.filter(function (key) {
            return rowKeys.includes(key);
        });
        var thIndex = 0;
        return (_J$X_("thead", { ref: this.domRef, className: cx('Table-thead', className) }, this.thColumns.map(function (data, index) {
            return (_J$X_("tr", { key: 'th-cell-' + index },
                draggable && index === 0 ? (_J$X_(Cell["default"], { key: "drag-".concat(index), wrapperComponent: "th", rowSpan: _this.thColumns.length, className: cx('Table-dragCell'), selfSticky: selfSticky, col: "drag", classnames: cx, classPrefix: classPrefix, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("drag-".concat(index)) })) : null,
                !draggable && selectable && index === 0 ? (_J$X_(Cell["default"], { key: "select-".concat(index), wrapperComponent: "th", rowSpan: _this.thColumns.length, fixed: rowSelectionFixed ? 'left' : '', className: cx('Table-checkCell'), selfSticky: selfSticky, col: "select", classnames: cx, classPrefix: classPrefix, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("select-".concat(index)) }, rowSelectionType !== 'radio'
                    ? [
                        _J$X_(Checkbox["default"], { key: "checkAll", partial: selectedKeys.length > 0 &&
                                selectedKeys.length < rowKeys.length, checked: selectedKeys.length > 0, onChange: function (value) { return tslib.__awaiter(_this, void 0, void 0, function () {
                                var selectedRows, selectedRowKeys;
                                return tslib.__generator(this, function (_a) {
                                    selectedRows = value ? rows : [];
                                    selectedRowKeys = value ? rowKeys : [];
                                    onSelectAll &&
                                        onSelectAll(value, selectedRowKeys, selectedRows, restSelectedKeys);
                                    return [2 /*return*/];
                                });
                            }); } }),
                        selections && selections.length > 0 ? (_J$X_(HeadCellSelect["default"], { key: "checkSelection", keys: rowKeys, selections: selections, popOverContainer: popOverContainer })) : null
                    ]
                    : null)) : null,
                isLeftExpandable && index === 0 ? expandableCell : null,
                data.map(function (item, colIndex) {
                    var _a, _b;
                    thIndex++;
                    var sort = null;
                    if (item.sorter) {
                        sort = (_J$X_(HeadCellSort["default"], { column: item, active: !!orderBy && orderBy === (item === null || item === void 0 ? void 0 : item.name), onSort: function (payload) {
                                onSort && onSort(payload, item);
                            }, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("sort-".concat(colIndex)) }));
                    }
                    var filter = null;
                    if (item.filterDropdown) {
                        filter = item.filterDropdown;
                    }
                    else if (item.filters && item.filters.length > 0) {
                        filter = (_J$X_(HeadCellFilter["default"], { column: item, popOverContainer: popOverContainer, onFilter: onFilter, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("filter-".concat(colIndex)) }));
                    }
                    // th的最后一行才可调整列宽
                    // 分组情况下 最后一行才和列配置个数对应
                    // 就可以根据index找到col 不依赖name
                    var noChildren = !((_a = item.children) === null || _a === void 0 ? void 0 : _a.length);
                    var cIndex = -1;
                    if (noChildren) {
                        // 根据name去tdColumns匹配出index
                        // 没设置name的 那一定不是要绑定数据的列 一般都是分组的上层 也不会出现调整列宽
                        cIndex = _this.tdColumns.findIndex(function (c) { return c.name === item.name; });
                    }
                    var children = !((_b = item.children) === null || _b === void 0 ? void 0 : _b.length) ? (_J$X_(React__default["default"].Fragment, null,
                        sort,
                        filter,
                        resizable ? (_J$X_("i", { className: cx('Table-thead-resizable'), onMouseDown: function (e) {
                                onResizeMouseDown === null || onResizeMouseDown === void 0 ? void 0 : onResizeMouseDown(e, cIndex);
                            } })) : null)) : null;
                    return (_J$X_(Cell["default"], { key: "cell-".concat(colIndex), wrapperComponent: "th", rowSpan: item.rowSpan, colSpan: item.colSpan, classnames: cx, classPrefix: classPrefix, selfSticky: selfSticky, fixed: item.fixed === true ? 'left' : item.fixed, className: cx({
                            'Table-cell-last': thIndex === maxCount
                        }), depth: item.depth, col: String(colIndex), testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("cell-".concat(colIndex)) }, typeof item.title === 'function'
                        ? item.title(children)
                        : item.title));
                }),
                isRightExpandable && index === 0 ? expandableCell : null));
        })));
    };
    return Head;
}(React__default["default"].PureComponent));

exports["default"] = Head;
