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

import { __extends, __assign, __spreadArray, __read, __decorate, __metadata } from 'tslib';
import React__default from 'react';
import isEqual from 'lodash/isEqual';
import cx from 'classnames';
import { findTree, getOptionValue, getOptionValueBindField, flattenTree, autobind, themeable, localeable, uncontrollable } from 'amis-core';
import Checkbox from './Checkbox.js';

/**
 * @file Checkboxes
 * @description 多选输入框
 * @author fex
 */
var BaseSelection = /** @class */ (function (_super) {
    __extends(BaseSelection, _super);
    function BaseSelection() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    BaseSelection.itemRender = function (option, states) {
        var label = option[(states === null || states === void 0 ? void 0 : states.labelField) || 'label'];
        var tip = option.tip || '';
        var classnames = states.classnames;
        var testIdBuilder = states.testIdBuilder;
        var canlabelTitle = typeof label === 'string' || typeof label === 'number';
        var canTipTitle = typeof tip === 'string' || typeof label === 'number';
        var title = canlabelTitle && canTipTitle ? "".concat(label, " ").concat(tip) : '';
        return (React__default.createElement("span", __assign({ title: title, className: "".concat(cx({ 'is-invalid': option === null || option === void 0 ? void 0 : option.__unmatched }), " ").concat(classnames('Selection-ellipsis-line')) }, testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild('span').getTestId()),
            label,
            tip));
    };
    BaseSelection.value2array = function (value, options, option2value, valueField) {
        if (option2value === void 0) { option2value = function (option) { return option; }; }
        if (value === void 0) {
            return [];
        }
        if (!Array.isArray(value)) {
            value = [value];
        }
        return value.map(function (value) {
            var option = findTree(options, function (option) { return isEqual(option2value(option), value); }, valueField
                ? {
                    value: getOptionValue(value, valueField),
                    resolve: getOptionValueBindField(valueField)
                }
                : undefined);
            return option || value;
        });
    };
    BaseSelection.resolveSelected = function (value, options, option2value) {
        if (option2value === void 0) { option2value = function (option) { return option; }; }
        value = Array.isArray(value) ? value[0] : value;
        return findTree(options, function (option) { return isEqual(option2value(option), value); });
    };
    // 获取两个数组的交集
    BaseSelection.prototype.intersectArray = function (arr1, arr2) {
        if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
            return [];
        }
        var len1 = arr1.length;
        var len2 = arr2.length;
        if (len1 < len2) {
            return this.intersectArray(arr2, arr1);
        }
        return Array.from(new Set(arr1.filter(function (item) { return arr2.includes(item); })));
    };
    BaseSelection.prototype.toggleOption = function (option) {
        var _b = this.props, value = _b.value, onChange = _b.onChange, option2value = _b.option2value, options = _b.options, disabled = _b.disabled, multiple = _b.multiple, clearable = _b.clearable, valueField = _b.valueField;
        if (disabled || option.disabled) {
            return;
        }
        var valueArray = BaseSelection.value2array(value, options, option2value, valueField);
        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]);
    };
    BaseSelection.prototype.getAvailableOptions = function () {
        var options = this.props.options;
        var flattendOptions = flattenTree(options, function (item) {
            return item.children ? null : item;
        }).filter(function (a) { return a && !a.disabled; });
        return flattendOptions;
    };
    BaseSelection.prototype.toggleAll = function () {
        var _b = this.props, value = _b.value, onChange = _b.onChange, option2value = _b.option2value; _b.options;
        var valueArray = [];
        var availableOptions = this.getAvailableOptions();
        var intersectOptions = this.intersectArray(value, availableOptions);
        if (!Array.isArray(value)) {
            valueArray = availableOptions;
        }
        else if (intersectOptions.length < availableOptions.length) {
            valueArray = Array.from(new Set(__spreadArray(__spreadArray([], __read(value), false), __read(availableOptions), false)));
        }
        else {
            valueArray = value.filter(function (item) { return !availableOptions.includes(item); });
        }
        var newValue = option2value
            ? valueArray.map(function (item) { return option2value(item); })
            : valueArray;
        onChange && onChange(newValue);
    };
    BaseSelection.prototype.render = function () {
        var _this = this;
        var _b = this.props, value = _b.value, options = _b.options, className = _b.className, placeholder = _b.placeholder, inline = _b.inline, labelClassName = _b.labelClassName, disabled = _b.disabled, cx = _b.classnames, option2value = _b.option2value, itemClassName = _b.itemClassName, itemRender = _b.itemRender, multiple = _b.multiple, labelField = _b.labelField, valueField = _b.valueField, onClick = _b.onClick, testIdBuilder = _b.testIdBuilder;
        var __ = this.props.translate;
        var valueArray = BaseSelection.value2array(value, options, option2value, valueField);
        var body = [];
        if (Array.isArray(options) && options.length) {
            body = options.map(function (option, key) { return (React__default.createElement(Checkbox, { type: multiple ? 'checkbox' : 'radio', className: cx(itemClassName, option.className), key: key, onChange: function () { return _this.toggleOption(option); }, checked: !!~valueArray.indexOf(option), disabled: disabled || option.disabled, labelClassName: labelClassName, description: option.description }, itemRender(option, {
                index: key,
                multiple: multiple,
                checked: !!~valueArray.indexOf(option),
                onChange: function () { return _this.toggleOption(option); },
                labelField: labelField,
                classnames: cx,
                disabled: disabled || option.disabled,
                testIdBuilder: testIdBuilder === null || testIdBuilder === void 0 ? void 0 : testIdBuilder.getChild(key)
            }))); });
        }
        return (React__default.createElement("div", { className: cx('Selection', className, inline ? 'Selection--inline' : ''), onClick: onClick }, body && body.length ? body : React__default.createElement("div", null, __(placeholder))));
    };
    var _a;
    _a = BaseSelection;
    BaseSelection.defaultProps = {
        placeholder: 'placeholder.noOption',
        itemRender: _a.itemRender,
        multiple: true,
        clearable: false,
        virtualThreshold: 1000,
        itemHeight: 32
    };
    __decorate([
        autobind,
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], BaseSelection.prototype, "toggleAll", null);
    return BaseSelection;
}(React__default.Component));
var Selection = /** @class */ (function (_super) {
    __extends(Selection, _super);
    function Selection() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return Selection;
}(BaseSelection));
var Selection$1 = themeable(localeable(uncontrollable(Selection, {
    value: 'onChange'
})));

export { BaseSelection, Selection, Selection$1 as default };
