"use strict";
/**
 * 结果表格(暂时不支持结果排序)
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseResultTableSelection = void 0;
var tslib_1 = require("tslib");
var react_1 = (0, tslib_1.__importDefault)(require("react"));
var Selection_1 = require("./Selection");
var theme_1 = require("../theme");
var Select_1 = require("./Select");
var tpl_builtin_1 = require("../utils/tpl-builtin");
var locale_1 = require("../locale");
var helper_1 = require("../utils/helper");
var TransferSearch_1 = (0, tslib_1.__importDefault)(require("./TransferSearch"));
var icons_1 = require("./icons");
var TableSelection_1 = (0, tslib_1.__importDefault)(require("./TableSelection"));
var BaseResultTableSelection = /** @class */ (function (_super) {
    (0, tslib_1.__extends)(BaseResultTableSelection, _super);
    function BaseResultTableSelection() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            tableOptions: [],
            searching: false,
            searchTableOptions: []
        };
        return _this;
    }
    BaseResultTableSelection.getDerivedStateFromProps = function (props) {
        var options = props.options, value = props.value, option2value = props.option2value;
        var valueArray = Selection_1.BaseSelection.value2array(value, options, option2value);
        return {
            tableOptions: valueArray
        };
    };
    BaseResultTableSelection.prototype.handleCloseItem = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, option2value = _a.option2value, options = _a.options, disabled = _a.disabled;
        var _b = this.state, searching = _b.searching, searchTableOptions = _b.searchTableOptions;
        if (disabled || option.disabled) {
            return;
        }
        // 删除普通值
        var valueArray = Selection_1.BaseSelection.value2array(value, options, option2value);
        var idx = valueArray.indexOf(option);
        valueArray.splice(idx, 1);
        var newValue = option2value
            ? valueArray.map(function (item) { return option2value(item); })
            : valueArray;
        onChange && onChange(newValue);
        if (searching) {
            var searchArray = Selection_1.BaseSelection.value2array(searchTableOptions, options, option2value);
            var searchIdx = searchArray.indexOf(option);
            searchTableOptions.splice(searchIdx, 1);
            this.setState({ searchTableOptions: searchTableOptions });
        }
    };
    BaseResultTableSelection.prototype.search = function (inputValue) {
        // 结果为空，直接清空
        if (!inputValue) {
            this.clearSearch();
            return;
        }
        var _a = this.props, value = _a.value, onSearch = _a.onSearch;
        var searchOptions = (value || []).filter(function (item) {
            return onSearch === null || onSearch === void 0 ? void 0 : onSearch(inputValue, item);
        });
        this.setState({
            searching: true,
            searchTableOptions: searchOptions
        });
    };
    BaseResultTableSelection.prototype.clearSearch = function () {
        this.setState({
            searching: false,
            searchTableOptions: []
        });
    };
    BaseResultTableSelection.prototype.renderTable = function () {
        var _this = this;
        var _a = this.props, cx = _a.classnames, className = _a.className, columns = _a.columns, cellRender = _a.cellRender, value = _a.value, disabled = _a.disabled, option2value = _a.option2value, onChange = _a.onChange, __ = _a.translate, placeholder = _a.placeholder;
        var _b = this.state, searching = _b.searching, tableOptions = _b.tableOptions, searchTableOptions = _b.searchTableOptions;
        return (react_1.default.createElement("div", { className: cx('ResultTableList', className) }, Array.isArray(value) && value.length ? (react_1.default.createElement(TableSelection_1.default, { columns: columns, options: !searching ? tableOptions : searchTableOptions, value: value, disabled: disabled, option2value: option2value, onChange: onChange, multiple: false, resultMode: true, cellRender: function (column, option, colIndex, rowIndex) {
                var raw = cellRender(column, option, colIndex, rowIndex);
                if (colIndex === columns.length - 1) {
                    return (react_1.default.createElement(react_1.default.Fragment, null,
                        raw,
                        react_1.default.createElement("span", { className: cx('ResultTableList-close-btn'), onClick: function (e) {
                                e.stopPropagation();
                                _this.handleCloseItem(option);
                            } },
                            react_1.default.createElement(icons_1.CloseIcon, null))));
                }
                return raw;
            } })) : (react_1.default.createElement("div", { className: cx('Selections-placeholder') }, __(placeholder)))));
    };
    BaseResultTableSelection.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, className = _a.className, title = _a.title, searchable = _a.searchable, __ = _a.translate, _b = _a.placeholder, placeholder = _b === void 0 ? __('Transfer.searchKeyword') : _b;
        return (react_1.default.createElement("div", { className: cx('Selections', className) },
            title ? react_1.default.createElement("div", { className: cx('Selections-title') }, title) : null,
            searchable ? (react_1.default.createElement(TransferSearch_1.default, { placeholder: placeholder, onSearch: this.search, onCancelSearch: this.clearSearch })) : null,
            this.renderTable()));
    };
    var _a;
    BaseResultTableSelection.defaultProps = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, Selection_1.BaseSelection.defaultProps), { cellRender: function (column, option, colIndex, rowIndex) { return react_1.default.createElement("span", null, (0, tpl_builtin_1.resolveVariable)(column.name, option)); } });
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [typeof (_a = typeof Select_1.Option !== "undefined" && Select_1.Option) === "function" ? _a : Object]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "handleCloseItem", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", [String]),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "search", null);
    (0, tslib_1.__decorate)([
        helper_1.autobind,
        (0, tslib_1.__metadata)("design:type", Function),
        (0, tslib_1.__metadata)("design:paramtypes", []),
        (0, tslib_1.__metadata)("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "clearSearch", null);
    return BaseResultTableSelection;
}(Selection_1.BaseSelection));
exports.BaseResultTableSelection = BaseResultTableSelection;
exports.default = (0, theme_1.themeable)((0, locale_1.localeable)(BaseResultTableSelection));
//# sourceMappingURL=./components/ResultTableList.js.map
