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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var tslib = require('tslib');
require('react');
var Selection = require('./Selection.js');
var amisCore = require('amis-core');
var GroupedSelection = require('./GroupedSelection.js');
var TableSelection = require('./TableSelection.js');
var ChainedSelection = require('./ChainedSelection.js');
var icons = require('./icons.js');
var Tree = require('./Tree.js');

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var AssociatedSelection = /** @class */ (function (_super) {
    tslib.__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 = Selection.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 = Selection.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 = Selection.BaseSelection.resolveSelected(this.state.leftValue, options, function (option) { return option.ref; });
        var __ = this.props.translate;
        return (_J$X_("div", { className: cx('AssociatedSelection', className) },
            _J$X_("div", { className: cx('AssociatedSelection-left') }, leftMode === 'tree' ? (_J$X_(Tree["default"], { 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') })) : (_J$X_(GroupedSelection["default"], { 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') }))),
            _J$X_("div", { className: cx('AssociatedSelection-right') }, this.state.leftValue ? (selectdOption ? (selectdOption[deferField] && !selectdOption.loaded ? (_J$X_("div", { className: cx('AssociatedSelection-box') },
                _J$X_("div", { className: cx('AssociatedSelection-reload', selectdOption.loading ? 'is-spin' : 'is-clickable'), onClick: selectdOption.loading
                        ? undefined
                        : this.handleRetry.bind(this, selectdOption) },
                    _J$X_(icons.Icon, { icon: "reload", className: "icon" })),
                selectdOption.loading ? (_J$X_("p", null, __('loading'))) : (_J$X_("p", null, __('Transfer.refreshIcon'))))) : rightMode === 'table' ? (_J$X_(TableSelection["default"], { 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' ? (_J$X_(Tree["default"], { 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' ? (_J$X_(ChainedSelection["default"], { 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') })) : (_J$X_(GroupedSelection["default"], { 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') }))) : (_J$X_("div", { className: cx('AssociatedSelection-box') }, __('Transfer.configError')))) : (_J$X_("div", { className: cx('AssociatedSelection-box') }, __('Transfer.selectFromLeft'))))));
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "leftOption2Value", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "handleLeftSelect", null);
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", [Object]),
        tslib.__metadata("design:returntype", void 0)
    ], AssociatedSelection.prototype, "handleLeftDeferLoad", null);
    return AssociatedSelection;
}(Selection.BaseSelection));
var AssociatedCheckboxes = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(AssociatedSelection, {
    value: 'onChange'
})));

exports.AssociatedSelection = AssociatedSelection;
exports["default"] = AssociatedCheckboxes;
