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

import { __extends, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { BaseSelection } from './Selection.js';
import { resolveVariable, autobind, themeable, localeable } from 'amis-core';
import TransferSearch from './TransferSearch.js';
import './icons.js';
import TableCheckboxes from './TableSelection.js';
import SvgClose from '../icons/close.svg.js';

/**
 * 结果表格(暂时不支持结果排序)
 */
var BaseResultTableSelection = /** @class */ (function (_super) {
    __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, valueField = props.valueField;
        var valueArray = BaseSelection.value2array(value, options, option2value, valueField);
        return {
            tableOptions: valueArray
        };
    };
    BaseResultTableSelection.prototype.domSearchRef = function (ref) {
        while (ref && ref.getWrappedInstance) {
            ref = ref.getWrappedInstance();
        }
        this.searchRef = ref;
    };
    BaseResultTableSelection.prototype.handleCloseItem = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, option2value = _a.option2value, options = _a.options, disabled = _a.disabled, valueField = _a.valueField;
        var _b = this.state, searching = _b.searching, searchTableOptions = _b.searchTableOptions;
        if (disabled || option.disabled) {
            return;
        }
        // 删除普通值
        var valueArray = BaseSelection.value2array(value, options, option2value, valueField);
        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 = BaseSelection.value2array(searchTableOptions, options, option2value, valueField);
            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.clearInput = function () {
        var _a, _b;
        if (this.props.searchable) {
            (_b = (_a = this.searchRef) === null || _a === void 0 ? void 0 : _a.clearInput) === null || _b === void 0 ? void 0 : _b.call(_a);
        }
        this.clearSearch();
    };
    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, virtualThreshold = _a.virtualThreshold, itemHeight = _a.itemHeight, testIdBuilder = _a.testIdBuilder;
        var _b = this.state, searching = _b.searching, tableOptions = _b.tableOptions, searchTableOptions = _b.searchTableOptions;
        return (React__default.createElement("div", { className: cx('ResultTableList', className) }, Array.isArray(value) && value.length ? (React__default.createElement(TableCheckboxes, { columns: columns, options: !searching ? tableOptions : searchTableOptions, value: value, disabled: disabled, option2value: option2value, onChange: onChange, multiple: false, resultMode: true, virtualThreshold: virtualThreshold, itemHeight: itemHeight, cellRender: function (column, option, colIndex, rowIndex) {
                var raw = cellRender(column, option, colIndex, rowIndex);
                var itemTIB = testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("item-".concat(option.value || rowIndex));
                if (colIndex === columns.length - 1) {
                    return (React__default.createElement(React__default.Fragment, null,
                        raw,
                        React__default.createElement("span", __assign({ className: cx('ResultTableList-close-btn'), onClick: function (e) {
                                e.stopPropagation();
                                _this.handleCloseItem(option);
                            } }, itemTIB === null || itemTIB === void 0 ? void 0 : itemTIB.getChild("close").getTestId()),
                            React__default.createElement(SvgClose, null))));
                }
                return raw;
            } })) : (React__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.searchPlaceholder, searchPlaceholder = _b === void 0 ? __('Transfer.searchKeyword') : _b;
        return (React__default.createElement("div", { className: cx('Selections', className) },
            title ? React__default.createElement("div", { className: cx('Selections-title') }, title) : null,
            searchable ? (React__default.createElement(TransferSearch, { ref: this.domSearchRef, placeholder: searchPlaceholder, onSearch: this.search, onCancelSearch: this.clearSearch })) : null,
            this.renderTable()));
    };
    BaseResultTableSelection.defaultProps = __assign(__assign({}, BaseSelection.defaultProps), { cellRender: function (column, option, colIndex, rowIndex) { return React__default.createElement("span", null, resolveVariable(column.name, option)); } });
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "domSearchRef", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "handleCloseItem", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [String]),
        __metadata("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "search", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "clearSearch", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], BaseResultTableSelection.prototype, "clearInput", null);
    return BaseResultTableSelection;
}(BaseSelection));
var ResultTableList = themeable(localeable(BaseResultTableSelection));

export { BaseResultTableSelection, ResultTableList as default };
