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

import { __extends, __assign, __decorate, __metadata, __spreadArray, __read } from 'tslib';
import React__default from 'react';
import omit from 'lodash/omit';
import { autobind, themeable, localeable, uncontrollable } from 'amis-core';
import { BaseSelection } from './Selection.js';
import DropDownSelection from './DropDownSelection.js';

var ChainedDropdownSelection = /** @class */ (function (_super) {
    __extends(ChainedDropdownSelection, _super);
    function ChainedDropdownSelection(props) {
        var _this = _super.call(this, props) || this;
        _this.state = _this.computed(props.value, props.options);
        return _this;
    }
    ChainedDropdownSelection.prototype.componentDidUpdate = function (prevProps) {
        var _a = this.props, options = _a.options, value = _a.value;
        if (options !== prevProps.options || prevProps.value !== value) {
            this.setState(this.computed(value, options));
        }
    };
    ChainedDropdownSelection.prototype.computed = function (value, options) {
        var valueField = this.props.valueField;
        var values = [];
        var getValues = function (opts, arr) {
            if (arr === void 0) { arr = []; }
            opts.forEach(function (item) {
                var _a;
                var cValue = valueField ? item[valueField] : (_a = item === null || item === void 0 ? void 0 : item.value) !== null && _a !== void 0 ? _a : '';
                if (cValue === value) {
                    values = __spreadArray(__spreadArray([], __read(arr), false), [cValue], false);
                }
                else if (item.children) {
                    getValues(item.children, __spreadArray(__spreadArray([], __read(arr), false), [cValue], false));
                }
            });
        };
        getValues(options);
        return {
            values: values,
            stacks: this.computedStask(values)
        };
    };
    ChainedDropdownSelection.prototype.getFlatOptions = function (options) {
        return options.map(function (item) { return omit(item, 'children'); });
    };
    ChainedDropdownSelection.prototype.handleSelect = function (index, value) {
        var _this = this;
        // 当前层级点击时，需要重新设置下values的值，以及重新计算stacks列表
        var values = this.state.values;
        values.splice(index, values.length - index);
        value && values.push(value);
        var stacks = this.computedStask(values);
        this.setState({
            stacks: stacks,
            values: values
        }, function () {
            var _a, _b;
            (_b = (_a = _this.props) === null || _a === void 0 ? void 0 : _a.onChange) === null || _b === void 0 ? void 0 : _b.call(_a, value);
        });
    };
    // 根据树结构层级，寻找最后一层
    ChainedDropdownSelection.prototype.computedStask = function (values) {
        var _this = this;
        var _a = this.props, options = _a.options, valueField = _a.valueField;
        var getDeep = function (opts, index, tems) {
            tems.push(_this.getFlatOptions(opts));
            opts.forEach(function (op) {
                var _a;
                var cValue = valueField ? op[valueField] : (_a = op === null || op === void 0 ? void 0 : op.value) !== null && _a !== void 0 ? _a : '';
                if (cValue === values[index] &&
                    op.children &&
                    values.length - 1 >= index) {
                    getDeep(op.children, index + 1, tems);
                }
            });
            return tems;
        };
        return getDeep(options, 0, []);
    };
    ChainedDropdownSelection.prototype.render = function () {
        var _this = this;
        var _a = this.state, stacks = _a.stacks, values = _a.values;
        var _b = this.props, className = _b.className, cx = _b.classnames, testIdBuilder = _b.testIdBuilder;
        return (React__default.createElement("div", { className: cx('ChainedDropdownSelection', className) }, stacks.map(function (item, index) { return (React__default.createElement("div", { className: cx('ChainedDropdownSelection-item'), key: index },
            React__default.createElement(DropDownSelection, __assign({}, _this.props, { value: values[index], options: item, onChange: function (value) { return _this.handleSelect(index, value); }, testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild("chained-".concat(index)) })))); })));
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", [Number, String]),
        __metadata("design:returntype", void 0)
    ], ChainedDropdownSelection.prototype, "handleSelect", null);
    return ChainedDropdownSelection;
}(BaseSelection));
var ChainedDropdownSelection$1 = themeable(localeable(uncontrollable(ChainedDropdownSelection, {
    value: 'onChange'
})));

export { ChainedDropdownSelection, ChainedDropdownSelection$1 as default };
