/**
 * amis-ui v6.12.0-beta.0
 * Copyright 2018-2025 fex
 */

import { __extends, __values, __assign, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { resolveVariableAndFilter, autobind, themeable, localeable } from 'amis-core';
import ResultBox from './ResultBox.js';
import UserSelect from './UserSelect.js';
import PopUp from './PopUp.js';
import { Icon } from './icons.js';
import ThemedTabs, { Tab } from './Tabs.js';

/**
 * @file 移动端人员、部门、角色、岗位选择
 * @author fex
 */
var UserTabSelect = /** @class */ (function (_super) {
    __extends(UserTabSelect, _super);
    function UserTabSelect(props) {
        var _this = _super.call(this, props) || this;
        _this.unmounted = false;
        _this.state = {
            isOpened: false,
            isSelectOpened: false,
            inputValue: '',
            options: [],
            breadList: [],
            searchList: [],
            selection: props.selection ? props.selection : [],
            isSearch: false,
            searchLoading: false,
            isEdit: false,
            activeKey: 0
        };
        return _this;
    }
    UserTabSelect.prototype.componentDidMount = function () { };
    UserTabSelect.prototype.componentDidUpdate = function (prevProps) { };
    UserTabSelect.prototype.componentWillUnmount = function () {
        this.unmounted = true;
    };
    UserTabSelect.prototype.onClose = function () {
        this.setState({
            isOpened: false,
            isSearch: false,
            inputValue: '',
            searchList: [],
            searchLoading: false,
            activeKey: 0,
            selection: []
        });
    };
    UserTabSelect.prototype.onOpen = function () {
        var _a = this.props.selection, selection = _a === void 0 ? [] : _a;
        this.setState({
            isOpened: true,
            selection: selection.slice()
        });
    };
    UserTabSelect.prototype.handleSubmit = function () {
        var onChange = this.props.onChange;
        onChange(this.state.selection);
        this.onClose();
    };
    UserTabSelect.prototype.handleSelectChange = function (option, isReplace, isDelete) {
        var _a = this.props, multiple = _a.multiple, _b = _a.valueField, valueField = _b === void 0 ? 'value' : _b;
        var selection = this.state.selection.slice();
        var selectionVals = selection.map(function (option) { return option[valueField]; });
        if (isDelete) {
            selection = selection.filter(function (item) { return item[valueField] !== option[valueField]; });
        }
        else if (isReplace && Array.isArray(option)) {
            selection = option.slice();
        }
        else if (!Array.isArray(option)) {
            var pos = selectionVals.indexOf(option[valueField]);
            if (pos !== -1) {
                selection.splice(pos, 1);
            }
            else {
                if (multiple) {
                    selection.push(option);
                }
                else {
                    selection = [option];
                }
            }
        }
        this.setState({
            selection: selection
        });
        return false;
    };
    UserTabSelect.prototype.handleImmediateChange = function (option) {
        var onChange = this.props.onChange;
        if (Array.isArray(option)) {
            this.setState({
                selection: option
            });
            onChange(option);
        }
    };
    UserTabSelect.prototype.handleTabChange = function (key) {
        this.setState({
            activeKey: key
        });
    };
    UserTabSelect.prototype.getResult = function () {
        var e_1, _a, e_2, _b;
        var _c = this.props, selection = _c.selection, tabOptions = _c.tabOptions, _d = _c.valueField, valueField = _d === void 0 ? 'value' : _d, _e = _c.labelField, labelField = _e === void 0 ? 'label' : _e;
        var _selection = (selection === null || selection === void 0 ? void 0 : selection.slice()) || [];
        if (tabOptions) {
            try {
                for (var tabOptions_1 = __values(tabOptions), tabOptions_1_1 = tabOptions_1.next(); !tabOptions_1_1.done; tabOptions_1_1 = tabOptions_1.next()) {
                    var item = tabOptions_1_1.value;
                    var _loop_1 = function (item2) {
                        var res = _selection.find(function (item) { return item[valueField] === item2[valueField]; });
                        if (res) {
                            res[labelField] = item2[labelField];
                        }
                    };
                    try {
                        for (var _f = (e_2 = void 0, __values(item.options)), _g = _f.next(); !_g.done; _g = _f.next()) {
                            var item2 = _g.value;
                            _loop_1(item2);
                        }
                    }
                    catch (e_2_1) { e_2 = { error: e_2_1 }; }
                    finally {
                        try {
                            if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
                        }
                        finally { if (e_2) throw e_2.error; }
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (tabOptions_1_1 && !tabOptions_1_1.done && (_a = tabOptions_1.return)) _a.call(tabOptions_1);
                }
                finally { if (e_1) throw e_1.error; }
            }
        }
        return _selection;
    };
    UserTabSelect.prototype.render = function () {
        var _this = this;
        var _a = this.props, cx = _a.classnames, __ = _a.translate, _b = _a.placeholder, placeholder = _b === void 0 ? '请选择' : _b, tabOptions = _a.tabOptions, onSearch = _a.onSearch, deferLoad = _a.deferLoad, data = _a.data, disabled = _a.disabled, displayFields = _a.displayFields, mobileUI = _a.mobileUI;
        var _c = this.state, activeKey = _c.activeKey, isOpened = _c.isOpened;
        return (React__default.createElement("div", { className: cx('UserTabSelect') },
            React__default.createElement(ResultBox, { className: cx('UserTabSelect-input', isOpened ? 'is-active' : ''), allowInput: false, result: this.getResult(), onResultChange: this.handleImmediateChange, onResultClick: this.onOpen, placeholder: placeholder, mobileUI: mobileUI, disabled: disabled }),
            React__default.createElement(PopUp, { isShow: isOpened, className: cx("UserTabSelect-popup"), onHide: this.onClose, showClose: false },
                React__default.createElement("div", { className: cx('UserTabSelect-wrap') },
                    React__default.createElement("div", { className: cx('UserSelect-navbar') },
                        React__default.createElement("span", { className: "left-arrow-box", onClick: this.onClose },
                            React__default.createElement(Icon, { icon: "left-arrow", className: "icon" })),
                        React__default.createElement("div", { className: cx('UserSelect-navbar-title') }, "\u4EBA\u5458\u9009\u62E9")),
                    React__default.createElement(ThemedTabs, { mode: "tiled", className: cx('UserTabSelect-tabs'), onSelect: this.handleTabChange, activeKey: activeKey }, tabOptions === null || tabOptions === void 0 ? void 0 : tabOptions.map(function (item, index) {
                        return (React__default.createElement(Tab, __assign({}, _this.props, { eventKey: index, key: index, title: item.title, className: "TabsTransfer-tab" }),
                            React__default.createElement(UserSelect, __assign({ isTab: true, selection: _this.state.selection, showResultBox: false }, item, { options: typeof item.options === 'string' && data
                                    ? resolveVariableAndFilter(item.options, data, '| raw')
                                    : item.options, multiple: true, controlled: true, displayFields: displayFields, onChange: _this.handleSelectChange, onSearch: function (input, cancelExecutor) {
                                    return item.searchable && onSearch
                                        ? onSearch(input, cancelExecutor, {
                                            searchApi: item.searchApi,
                                            searchParam: item.searchParam,
                                            searchTerm: item.searchTerm
                                        })
                                        : undefined;
                                }, deferLoad: function (data, isRef, param) {
                                    return deferLoad(data, isRef, __assign({ deferApi: item.deferApi }, (param || {})));
                                } }))));
                    })),
                    React__default.createElement("div", { className: cx('UserTabSelect-footer') },
                        React__default.createElement("button", { type: "button", className: cx('Button Button--md Button--primary'), onClick: this.handleSubmit }, __('UserSelect.sure')))))));
    };
    UserTabSelect.defaultProps = {};
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "onClose", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "onOpen", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleSubmit", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object, Boolean, Boolean]),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleSelectChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Array]),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleImmediateChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Number]),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleTabChange", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "getResult", null);
    return UserTabSelect;
}(React__default.Component));
var UserTabSelect$1 = themeable(localeable(UserTabSelect));

export { UserTabSelect, UserTabSelect$1 as default };
