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

import { __extends, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import { BaseSelection } from './Selection.js';
import { autobind, themeable, localeable, uncontrollable } from 'amis-core';
import GroupedSelection from './GroupedSelection.js';
import TableCheckboxes from './TableSelection.js';
import ChainedCheckboxes from './ChainedSelection.js';
import { Icon } from './icons.js';
import Tree from './Tree.js';

/**
 * 关联多选框，仅支持两层关联选择。
 * 左边先点选，然后右边再次点选。
 * 可以满足，先从 tree 中选中一个元素，然后查出来一个列表再次勾选。
 */
var AssociatedSelection = /** @class */ (function (_super) {
    __extends(AssociatedSelection, _super);
    function AssociatedSelection() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            leftValue: _this.props.leftDefaultValue
        };
        return _this;
    }
    AssociatedSelection.prototype.componentDidMount = function () {
        var leftValue = this.state.leftValue;
        var _a = this.props, options = _a.options, onDeferLoad = _a.onDeferLoad, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b;
        if (leftValue) {
            var selectdOption = BaseSelection.resolveSelected(leftValue, options, function (option) { return option.ref; });
            if (selectdOption && onDeferLoad && selectdOption[deferField]) {
                onDeferLoad(selectdOption);
            }
        }
    };
    AssociatedSelection.prototype.leftOption2Value = function (option) {
        return option.value;
    };
    AssociatedSelection.prototype.handleLeftSelect = function (value) {
        var _a = this.props, options = _a.options, onDeferLoad = _a.onDeferLoad, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b;
        this.setState({ leftValue: value });
        var selectdOption = BaseSelection.resolveSelected(value, options, function (option) { return option.ref; });
        if (selectdOption && onDeferLoad && selectdOption[deferField]) {
            onDeferLoad(selectdOption);
        }
    };
    AssociatedSelection.prototype.handleLeftDeferLoad = function (option) {
        var _a = this.props, leftOptions = _a.leftOptions, onLeftDeferLoad = _a.onLeftDeferLoad, onDeferLoad = _a.onDeferLoad;
        if (typeof onLeftDeferLoad === 'function') {
            // TabsTransfer
            return onLeftDeferLoad === null || onLeftDeferLoad === void 0 ? void 0 : onLeftDeferLoad(option, leftOptions);
        }
        else if (typeof onDeferLoad === 'function') {
            // Select
            return onDeferLoad === null || onDeferLoad === void 0 ? void 0 : onDeferLoad(option);
        }
    };
    AssociatedSelection.prototype.handleRetry = function (option) {
        var onDeferLoad = this.props.onDeferLoad;
        onDeferLoad === null || onDeferLoad === void 0 ? void 0 : onDeferLoad(option);
    };
    AssociatedSelection.prototype.render = function () {
        var _a = this.props, cx = _a.classnames, className = _a.className, leftOptions = _a.leftOptions, options = _a.options, option2value = _a.option2value, rightMode = _a.rightMode, onChange = _a.onChange, columns = _a.columns, value = _a.value, disabled = _a.disabled, leftMode = _a.leftMode, cellRender = _a.cellRender, multiple = _a.multiple, itemRender = _a.itemRender, labelField = _a.labelField, virtualThreshold = _a.virtualThreshold, itemHeight = _a.itemHeight, loadingConfig = _a.loadingConfig, checkAll = _a.checkAll, checkAllLabel = _a.checkAllLabel, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b, testIdBuilder = _a.testIdBuilder;
        var selectdOption = BaseSelection.resolveSelected(this.state.leftValue, options, function (option) { return option.ref; });
        var __ = this.props.translate;
        return (React__default.createElement("div", { className: cx('AssociatedSelection', className) },
            React__default.createElement("div", { className: cx('AssociatedSelection-left') }, leftMode === 'tree' ? (React__default.createElement(Tree, { multiple: false, disabled: disabled, value: this.state.leftValue, options: leftOptions, onChange: this.handleLeftSelect, onDeferLoad: this.handleLeftDeferLoad, virtualThreshold: virtualThreshold, 
                // itemHeight={itemHeight}
                loadingConfig: loadingConfig, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('left-selection') })) : (React__default.createElement(GroupedSelection, { option2value: this.leftOption2Value, options: leftOptions, value: this.state.leftValue, disabled: disabled, onChange: this.handleLeftSelect, multiple: false, clearable: false, virtualThreshold: virtualThreshold, itemHeight: itemHeight, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('left-selection') }))),
            React__default.createElement("div", { className: cx('AssociatedSelection-right') }, this.state.leftValue ? (selectdOption ? (selectdOption[deferField] && !selectdOption.loaded ? (React__default.createElement("div", { className: cx('AssociatedSelection-box') },
                React__default.createElement("div", { className: cx('AssociatedSelection-reload', selectdOption.loading ? 'is-spin' : 'is-clickable'), onClick: selectdOption.loading
                        ? undefined
                        : this.handleRetry.bind(this, selectdOption) },
                    React__default.createElement(Icon, { icon: "reload", className: "icon" })),
                selectdOption.loading ? (React__default.createElement("p", null, __('loading'))) : (React__default.createElement("p", null, __('Transfer.refreshIcon'))))) : rightMode === 'table' ? (React__default.createElement(TableCheckboxes, { columns: columns, value: value, disabled: disabled, options: selectdOption.children || [], onChange: onChange, option2value: option2value, cellRender: cellRender, multiple: multiple, virtualThreshold: virtualThreshold, itemHeight: itemHeight, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('right-selection') })) : rightMode === 'tree' ? (React__default.createElement(Tree, { value: value, disabled: disabled, options: selectdOption.children || [], onChange: onChange, multiple: multiple, labelField: labelField, virtualThreshold: virtualThreshold, 
                // itemHeight={itemHeight}
                loadingConfig: loadingConfig, checkAllLabel: checkAllLabel, checkAll: checkAll, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('right-selection') })) : rightMode === 'chained' ? (React__default.createElement(ChainedCheckboxes, { value: value, disabled: disabled, options: selectdOption.children || [], onChange: onChange, option2value: option2value, multiple: multiple, itemRender: itemRender, labelField: labelField, virtualThreshold: virtualThreshold, itemHeight: itemHeight, loadingConfig: loadingConfig, checkAllLabel: checkAllLabel, checkAll: checkAll, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('right-selection') })) : (React__default.createElement(GroupedSelection, { value: value, disabled: disabled, options: selectdOption.children || [], onChange: onChange, option2value: option2value, multiple: multiple, itemRender: itemRender, labelField: labelField, virtualThreshold: virtualThreshold, itemHeight: itemHeight, checkAllLabel: checkAllLabel, checkAll: checkAll, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('right-selection') }))) : (React__default.createElement("div", { className: cx('AssociatedSelection-box') }, __('Transfer.configError')))) : (React__default.createElement("div", { className: cx('AssociatedSelection-box') }, __('Transfer.selectFromLeft'))))));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "leftOption2Value", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "handleLeftSelect", null);
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Object]),
        __metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "handleLeftDeferLoad", null);
    return AssociatedSelection;
}(BaseSelection));
var AssociatedCheckboxes = themeable(localeable(uncontrollable(AssociatedSelection, {
    value: 'onChange'
})));

export { AssociatedSelection, AssociatedCheckboxes as default };
