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

'use strict';

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

var tslib = require('tslib');
var Selection = require('./Selection.js');
var amisCore = require('amis-core');
require('react');
var Checkbox = require('./Checkbox.js');
var Spinner = require('./Spinner.js');
var icons = require('./icons.js');

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var TreeSelection = /** @class */ (function (_super) {
    tslib.__extends(TreeSelection, _super);
    function TreeSelection() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            expanded: []
        };
        return _this;
    }
    TreeSelection.prototype.componentDidMount = function () {
        this.syncExpanded();
    };
    TreeSelection.prototype.componentDidUpdate = function (prevProps) {
        var props = this.props;
        if (props.expand !== prevProps.expand ||
            props.options !== prevProps.options) {
            this.syncExpanded();
        }
    };
    TreeSelection.prototype.syncExpanded = function () {
        var options = this.props.options;
        var mode = this.props.expand;
        var expanded = [];
        if (!Array.isArray(options)) {
            return;
        }
        if (mode === 'first' || mode === 'root') {
            options.every(function (option, index) {
                if (Array.isArray(option.children)) {
                    expanded.push("".concat(index));
                    return mode === 'root';
                }
                return true;
            });
        }
        else if (mode === 'all') {
            amisCore.everyTree(options, function (option, index, level, paths, indexes) {
                if (Array.isArray(option.children)) {
                    expanded.push("".concat(indexes.concat(index).join('-')));
                }
                return true;
            });
        }
        this.setState({ expanded: expanded });
    };
    TreeSelection.prototype.toggleOption = function (option) {
        var _a = this.props, value = _a.value, onChange = _a.onChange, option2value = _a.option2value, options = _a.options, onDeferLoad = _a.onDeferLoad, disabled = _a.disabled, multiple = _a.multiple, clearable = _a.clearable, valueField = _a.valueField, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b;
        if (disabled || option.disabled) {
            return;
        }
        else if (option[deferField] && !option.loaded) {
            onDeferLoad === null || onDeferLoad === void 0 ? void 0 : onDeferLoad(option);
            return;
        }
        var valueArray = Selection.BaseSelection.value2array(value, options, option2value, valueField);
        if (option.value === void 0 &&
            Array.isArray(option.children) &&
            option.children.length &&
            multiple) {
            var someCheckedFn_1 = function (child) {
                return (Array.isArray(child.children) && child.children.length
                    ? child.children.some(someCheckedFn_1)
                    : false) ||
                    (child.value !== void 0 && ~valueArray.indexOf(child));
            };
            var someChecked_1 = option.children.some(someCheckedFn_1);
            var eachFn_1 = function (child) {
                if (Array.isArray(child.children) && child.children.length) {
                    child.children.forEach(eachFn_1);
                }
                if (child.value !== void 0) {
                    var idx = valueArray.indexOf(child);
                    ~idx && valueArray.splice(idx, 1);
                    if (!someChecked_1) {
                        valueArray.push(child);
                    }
                }
            };
            option.children.forEach(eachFn_1);
        }
        else {
            var idx = valueArray.indexOf(option);
            if (~idx && (multiple || clearable)) {
                valueArray.splice(idx, 1);
            }
            else if (multiple) {
                valueArray.push(option);
            }
            else {
                valueArray = [option];
            }
        }
        var newValue = option2value
            ? valueArray.map(function (item) { return option2value(item); })
            : valueArray;
        onChange && onChange(multiple ? newValue : newValue[0]);
    };
    TreeSelection.prototype.toggleCollapsed = function (option, index) {
        var _a = this.props, onDeferLoad = _a.onDeferLoad, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b;
        var expanded = this.state.expanded.concat();
        var idx = expanded.indexOf(index);
        if (~idx) {
            expanded.splice(idx, 1);
        }
        else {
            expanded.push(index);
        }
        this.setState({
            expanded: expanded
        }, option[deferField] && onDeferLoad ? function () { return onDeferLoad(option); } : undefined);
    };
    TreeSelection.prototype.renderItem = function (option, index, indexes) {
        var _this = this;
        if (indexes === void 0) { indexes = []; }
        var _a = this.props, labelClassName = _a.labelClassName, disabled = _a.disabled, cx = _a.classnames, itemClassName = _a.itemClassName, itemRender = _a.itemRender, multiple = _a.multiple, loadingConfig = _a.loadingConfig, _b = _a.deferField, deferField = _b === void 0 ? 'defer' : _b;
        var id = indexes.join('-');
        var valueArray = this.valueArray;
        var partial = false;
        var checked = false;
        var hasChildren = Array.isArray(option.children) && option.children.length;
        if (option.value === void 0 && hasChildren) {
            var allchecked_1 = true;
            var partialChecked_1 = false;
            var eachFn_2 = function (child) {
                if (Array.isArray(child.children) && child.children.length) {
                    child.children.forEach(eachFn_2);
                }
                if (child.value !== void 0) {
                    var isIn = !!~valueArray.indexOf(child);
                    if (isIn && !partialChecked_1) {
                        partialChecked_1 = true;
                    }
                    else if (!isIn && allchecked_1) {
                        allchecked_1 = false;
                    }
                    checked = partialChecked_1;
                    partial = partialChecked_1 && !allchecked_1;
                }
            };
            option.children.forEach(eachFn_2);
        }
        else {
            checked = !!~valueArray.indexOf(option);
        }
        var expaned = !!~this.state.expanded.indexOf(id);
        return (_J$X_("div", { key: index, className: cx('TreeSelection-item', disabled || option.disabled || (option[deferField] && option.loading)
                ? 'is-disabled'
                : '', expaned ? 'is-expanded' : '') },
            _J$X_("div", { className: cx('TreeSelection-itemInner', itemClassName, option.className, checked ? 'is-active' : ''), onClick: function () { return _this.toggleOption(option); } },
                hasChildren || option[deferField] ? (_J$X_("a", { onClick: function (e) {
                        e.stopPropagation();
                        _this.toggleCollapsed(option, id);
                    }, className: cx('Table-expandBtn', expaned ? 'is-active' : '') },
                    _J$X_(icons.Icon, { icon: "right-arrow-bold", className: "icon" }))) : null,
                option[deferField] && option.loading ? (_J$X_(Spinner["default"], { loadingConfig: loadingConfig, show: true, size: "sm" })) : null,
                multiple && (!option[deferField] || option.loaded) ? (_J$X_(Checkbox["default"], { size: "sm", checked: checked, partial: partial, disabled: disabled || option.disabled, labelClassName: labelClassName, description: option.description })) : null,
                _J$X_("div", { className: cx('TreeSelection-itemLabel') }, itemRender(option, {
                    index: index,
                    multiple: multiple,
                    checked: checked,
                    onChange: function () { return _this.toggleOption(option); },
                    disabled: disabled || option.disabled,
                    classnames: cx
                })),
                option[deferField] && option.loading ? (_J$X_(Spinner["default"], { loadingConfig: loadingConfig, show: true, size: "sm" })) : null),
            hasChildren ? (_J$X_("div", { className: cx('TreeSelection-sublist') }, option.children.map(function (option, key) {
                return _this.renderItem(option, key, indexes.concat(key));
            }))) : null));
    };
    TreeSelection.prototype.render = function () {
        var _this = this;
        var _a;
        var _b = this.props, value = _b.value, options = _b.options, className = _b.className, placeholder = _b.placeholder, cx = _b.classnames, option2value = _b.option2value, placeholderRender = _b.placeholderRender, valueField = _b.valueField, __ = _b.translate;
        this.valueArray = Selection.BaseSelection.value2array(value, options, option2value, valueField);
        var body = [];
        if (Array.isArray(options) && options.length) {
            body = options.map(function (option, key) { return _this.renderItem(option, key, [key]); });
        }
        return (_J$X_("div", { className: cx('TreeSelection', className) }, body && body.length ? (body) : (_J$X_("div", { className: cx('TreeSelection-placeholder') }, (_a = placeholderRender === null || placeholderRender === void 0 ? void 0 : placeholderRender(this.props)) !== null && _a !== void 0 ? _a : __(placeholder)))));
    };
    TreeSelection.defaultProps = tslib.__assign(tslib.__assign({}, Selection.BaseSelection.defaultProps), { expand: 'first' });
    return TreeSelection;
}(Selection.BaseSelection));
var TreeSelection$1 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(TreeSelection, {
    value: 'onChange'
})));

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