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

import { __extends, __assign, __awaiter, __generator, __spreadArray, __read } from 'tslib';
import React__default from 'react';
import { findDOMNode } from 'react-dom';
import isEqual from 'lodash/isEqual';
import { themeable, localeable } from 'amis-core';
import HeadCellDropDown from './HeadCellDropDown.js';
import Checkbox from '../Checkbox.js';
import Button from '../Button.js';
import { Icon } from '../icons.js';

/**
 * @file table/HeadCellFilter
 * @author fex
 */
var HeadCellFilter = /** @class */ (function (_super) {
    __extends(HeadCellFilter, _super);
    function HeadCellFilter(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            options: [],
            filteredValue: props.filteredValue || []
        };
        return _this;
    }
    HeadCellFilter.prototype.alterOptions = function (options) {
        var _this = this;
        options = options.map(function (option) { return (__assign(__assign({}, option), { selected: _this.state.filteredValue.indexOf(option.value) > -1 })); });
        return options;
    };
    HeadCellFilter.prototype.componentDidMount = function () {
        var column = this.props.column;
        if (column.filters && column.filters.length > 0) {
            this.setState({ options: this.alterOptions(column.filters) });
        }
    };
    HeadCellFilter.prototype.componentDidUpdate = function (prevProps, prevState) {
        var column = this.props.column;
        if (column.filters &&
            column.filters.length > 0 &&
            !isEqual(prevState.filteredValue, this.state.filteredValue)) {
            this.setState({ options: this.alterOptions(column.filters) });
        }
    };
    HeadCellFilter.prototype.render = function () {
        var _this = this;
        var options = this.state.options;
        var _a = this.props, column = _a.column, popOverContainer = _a.popOverContainer, cx = _a.classnames, ns = _a.classPrefix, testIdBuilder = _a.testIdBuilder;
        var filterProps = {
            filterDropdown: function (payload) {
                var setSelectedKeys = payload.setSelectedKeys; payload.selectedKeys; var confirm = payload.confirm; payload.clearFilters;
                return options && options.length > 0 ? (React__default.createElement("ul", { className: cx('DropDown-menu') },
                    !column.filterMultiple
                        ? options.map(function (option, index) { return (React__default.createElement("li", __assign({ key: index, className: cx({
                                'is-active': option.selected
                            }), onClick: function () {
                                return _this.handleClick(confirm, setSelectedKeys, [option.value]);
                            } }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("".concat(index)).getTestId()), option.text)); })
                        : options.map(function (option, index) { return (React__default.createElement("li", { key: index },
                            React__default.createElement(Checkbox, { classPrefix: ns, onChange: function (e) {
                                    return _this.handleCheck(confirm, setSelectedKeys, e ? [option.value] : option.value);
                                }, checked: option.selected, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("ckbx-".concat(index)) }, option.text))); }),
                    column.filterMultiple ? (React__default.createElement("li", { key: "dropDown-multiple-menu", className: cx('DropDown-multiple-menu') },
                        React__default.createElement(Button, { size: 'xs', level: 'primary', onClick: function () { return _this.handleConfirmClick(confirm); }, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("btn-confirm") }, "\u786E\u5B9A"),
                        React__default.createElement(Button, { size: 'xs', onClick: function () {
                                return _this.handleCancelClick(confirm, setSelectedKeys);
                            }, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("btn-cancel") }, "\u53D6\u6D88"))) : null)) : null;
            },
            setSelectedKeys: function (keys) {
                return _this.setState({ filteredValue: keys });
            }
        };
        return (React__default.createElement(HeadCellDropDown, __assign({ className: "".concat(ns, "TableCell-filterBtn"), layerClassName: "".concat(ns, "TableCell-filterPopOver"), filterIcon: React__default.createElement(Icon, { icon: "column-filter", className: "icon", iconContent: "table-filter-icon", testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("icon") }), active: column.filtered ||
                (options && options.some(function (item) { return item.selected; })), popOverContainer: popOverContainer
                ? popOverContainer
                : function () { return findDOMNode(_this); }, selectedKeys: this.state.filteredValue }, filterProps)));
    };
    HeadCellFilter.prototype.handleClick = function (confirm, setSelectedKeys, selectedKeys) {
        return __awaiter(this, void 0, void 0, function () {
            var _a, onFilter, column;
            return __generator(this, function (_b) {
                _a = this.props, onFilter = _a.onFilter, column = _a.column;
                setSelectedKeys && setSelectedKeys(selectedKeys);
                onFilter &&
                    onFilter({
                        filterName: column.name,
                        filterValue: selectedKeys === null || selectedKeys === void 0 ? void 0 : selectedKeys.join(',')
                    });
                confirm();
                return [2 /*return*/];
            });
        });
    };
    HeadCellFilter.prototype.handleCheck = function (confirm, setSelectedKeys, selectedKeys) {
        var filteredValue = this.state.filteredValue;
        // 选中
        if (Array.isArray(selectedKeys)) {
            setSelectedKeys && setSelectedKeys(__spreadArray(__spreadArray([], __read(filteredValue), false), __read(selectedKeys), false));
        }
        else {
            // 取消选中
            setSelectedKeys &&
                setSelectedKeys(filteredValue.filter(function (v) { return v !== selectedKeys; }));
        }
    };
    HeadCellFilter.prototype.handleConfirmClick = function (confirm) {
        var _a;
        var _b = this.props, onFilter = _b.onFilter, column = _b.column;
        onFilter && onFilter((_a = {}, _a[column.name] = this.state.filteredValue, _a));
        confirm();
    };
    HeadCellFilter.prototype.handleCancelClick = function (confirm, setSelectedKeys) {
        var _a;
        setSelectedKeys && setSelectedKeys([]);
        var _b = this.props, onFilter = _b.onFilter, column = _b.column;
        onFilter && onFilter((_a = {}, _a[column.name] = '', _a));
        confirm();
    };
    HeadCellFilter.defaultProps = {
        filteredValue: [],
        filterMultiple: false
    };
    return HeadCellFilter;
}(React__default.PureComponent));
var HeadCellFilter$1 = themeable(localeable(HeadCellFilter));

export { HeadCellFilter, HeadCellFilter$1 as default };
