/**
 * 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 amisCore = require('amis-core');
var ResultBox = require('./ResultBox.js');
var UserSelect = require('./UserSelect.js');
var PopUp = require('./PopUp.js');
var icons = require('./icons.js');
var Tabs = require('./Tabs.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 UserTabSelect = /** @class */ (function (_super) {
    tslib.__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 = tslib.__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, tslib.__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 (_J$X_("div", { className: cx('UserTabSelect') },
            _J$X_(ResultBox["default"], { className: cx('UserTabSelect-input', isOpened ? 'is-active' : ''), allowInput: false, result: this.getResult(), onResultChange: this.handleImmediateChange, onResultClick: this.onOpen, placeholder: placeholder, mobileUI: mobileUI, disabled: disabled }),
            _J$X_(PopUp["default"], { isShow: isOpened, className: cx("UserTabSelect-popup"), onHide: this.onClose, showClose: false },
                _J$X_("div", { className: cx('UserTabSelect-wrap') },
                    _J$X_("div", { className: cx('UserSelect-navbar') },
                        _J$X_("span", { className: "left-arrow-box", onClick: this.onClose },
                            _J$X_(icons.Icon, { icon: "left-arrow", className: "icon" })),
                        _J$X_("div", { className: cx('UserSelect-navbar-title') }, "\u4EBA\u5458\u9009\u62E9")),
                    _J$X_(Tabs["default"], { mode: "tiled", className: cx('UserTabSelect-tabs'), onSelect: this.handleTabChange, activeKey: activeKey }, tabOptions === null || tabOptions === void 0 ? void 0 : tabOptions.map(function (item, index) {
                        return (_J$X_(Tabs.Tab, tslib.__assign({}, _this.props, { eventKey: index, key: index, title: item.title, className: "TabsTransfer-tab" }),
                            _J$X_(UserSelect["default"], tslib.__assign({ isTab: true, selection: _this.state.selection, showResultBox: false }, item, { options: typeof item.options === 'string' && data
                                    ? amisCore.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, tslib.__assign({ deferApi: item.deferApi }, (param || {})));
                                } }))));
                    })),
                    _J$X_("div", { className: cx('UserTabSelect-footer') },
                        _J$X_("button", { type: "button", className: cx('Button Button--md Button--primary'), onClick: this.handleSubmit }, __('UserSelect.sure')))))));
    };
    UserTabSelect.defaultProps = {};
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "onClose", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "onOpen", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleSubmit", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object, Boolean, Boolean]),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleSelectChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Array]),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleImmediateChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Number]),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "handleTabChange", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], UserTabSelect.prototype, "getResult", null);
    return UserTabSelect;
}(React__default["default"].Component));
var UserTabSelect$1 = amisCore.themeable(amisCore.localeable(UserTabSelect));

exports.UserTabSelect = UserTabSelect;
exports["default"] = UserTabSelect$1;
