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

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var isEqual = require('lodash/isEqual');
var cx = require('classnames');
var amisCore = require('amis-core');
var Checkbox = require('./Checkbox.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var isEqual__default = /*#__PURE__*/_interopDefaultLegacy(isEqual);
var cx__default = /*#__PURE__*/_interopDefaultLegacy(cx);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var BaseSelection = /** @class */ (function (_super) {
    tslib.__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 (_J$X_("span", tslib.__assign({ title: title, className: "".concat(cx__default["default"]({ '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 = amisCore.findTree(options, function (option) { return isEqual__default["default"](option2value(option), value); }, valueField
                ? {
                    value: amisCore.getOptionValue(value, valueField),
                    resolve: amisCore.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 amisCore.findTree(options, function (option) { return isEqual__default["default"](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 = amisCore.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(tslib.__spreadArray(tslib.__spreadArray([], tslib.__read(value), false), tslib.__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 (_J$X_(Checkbox["default"], { 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 (_J$X_("div", { className: cx('Selection', className, inline ? 'Selection--inline' : ''), onClick: onClick }, body && body.length ? body : _J$X_("div", null, __(placeholder))));
    };
    var _a;
    _a = BaseSelection;
    BaseSelection.defaultProps = {
        placeholder: 'placeholder.noOption',
        itemRender: _a.itemRender,
        multiple: true,
        clearable: false,
        virtualThreshold: 1000,
        itemHeight: 32
    };
    tslib.__decorate([
        amisCore.autobind,
        tslib.__metadata("design:type", Function),
        tslib.__metadata("design:paramtypes", []),
        tslib.__metadata("design:returntype", void 0)
    ], BaseSelection.prototype, "toggleAll", null);
    return BaseSelection;
}(React__default["default"].Component));
var Selection = /** @class */ (function (_super) {
    tslib.__extends(Selection, _super);
    function Selection() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return Selection;
}(BaseSelection));
var Selection$1 = amisCore.themeable(amisCore.localeable(amisCore.uncontrollable(Selection, {
    value: 'onChange'
})));

exports.BaseSelection = BaseSelection;
exports.Selection = Selection;
exports["default"] = Selection$1;
