"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkboxGroupInjectionKey = void 0;
const vue_1 = require("vue");
const vooks_1 = require("vooks");
const _mixins_1 = require("../../_mixins");
const _utils_1 = require("../../_utils");
exports.checkboxGroupInjectionKey = Symbol('checkboxGroup');
const checkboxGroupProps = {
    min: Number,
    max: Number,
    size: String,
    value: Array,
    defaultValue: {
        type: Array,
        default: null
    },
    disabled: Boolean,
    // eslint-disable-next-line vue/prop-name-casing
    'onUpdate:value': [Function, Array],
    onUpdateValue: [Function, Array],
    // deprecated
    onChange: {
        type: [Function, Array],
        validator: () => {
            if (process.env.NODE_ENV !== 'production') {
                _utils_1.warn('checkbox-group', '`on-change` is deprecated, please use `on-update:value` instead.');
            }
            return true;
        },
        default: undefined
    }
};
exports.default = vue_1.defineComponent({
    name: 'CheckboxGroup',
    props: checkboxGroupProps,
    setup(props) {
        const { mergedClsPrefixRef } = _mixins_1.useConfig(props);
        const formItem = _mixins_1.useFormItem(props);
        const uncontrolledValueRef = vue_1.ref(props.defaultValue);
        const controlledValueRef = vue_1.computed(() => props.value);
        const mergedValueRef = vooks_1.useMergedState(controlledValueRef, uncontrolledValueRef);
        const checkedCount = vue_1.computed(() => {
            var _a;
            return ((_a = mergedValueRef.value) === null || _a === void 0 ? void 0 : _a.length) || 0;
        });
        const valueSetRef = vue_1.computed(() => {
            if (Array.isArray(mergedValueRef.value)) {
                return new Set(mergedValueRef.value);
            }
            return new Set();
        });
        function toggleCheckbox(checked, checkboxValue) {
            const { nTriggerFormInput, nTriggerFormChange } = formItem;
            const { onChange, 'onUpdate:value': _onUpdateValue, onUpdateValue } = props;
            if (Array.isArray(mergedValueRef.value)) {
                const groupValue = Array.from(mergedValueRef.value);
                const index = groupValue.findIndex((value) => value === checkboxValue);
                if (checked) {
                    if (!~index) {
                        groupValue.push(checkboxValue);
                        if (onUpdateValue)
                            _utils_1.call(onUpdateValue, groupValue);
                        if (_onUpdateValue)
                            _utils_1.call(_onUpdateValue, groupValue);
                        nTriggerFormInput();
                        nTriggerFormChange();
                        uncontrolledValueRef.value = groupValue;
                        // deprecated
                        if (onChange)
                            _utils_1.call(onChange, groupValue);
                    }
                }
                else {
                    if (~index) {
                        groupValue.splice(index, 1);
                        if (onUpdateValue)
                            _utils_1.call(onUpdateValue, groupValue);
                        if (_onUpdateValue)
                            _utils_1.call(_onUpdateValue, groupValue);
                        if (onChange)
                            _utils_1.call(onChange, groupValue); // deprecated
                        uncontrolledValueRef.value = groupValue;
                        nTriggerFormInput();
                        nTriggerFormChange();
                    }
                }
            }
            else {
                if (checked) {
                    if (onUpdateValue)
                        _utils_1.call(onUpdateValue, [checkboxValue]);
                    if (_onUpdateValue)
                        _utils_1.call(_onUpdateValue, [checkboxValue]);
                    if (onChange)
                        _utils_1.call(onChange, [checkboxValue]); // deprecated
                    uncontrolledValueRef.value = [checkboxValue];
                    nTriggerFormInput();
                    nTriggerFormChange();
                }
                else {
                    if (onUpdateValue)
                        _utils_1.call(onUpdateValue, []);
                    if (_onUpdateValue)
                        _utils_1.call(_onUpdateValue, []);
                    if (onChange)
                        _utils_1.call(onChange, []); // deprecated
                    uncontrolledValueRef.value = [];
                    nTriggerFormInput();
                    nTriggerFormChange();
                }
            }
        }
        vue_1.provide(exports.checkboxGroupInjectionKey, {
            checkedCountRef: checkedCount,
            maxRef: vue_1.toRef(props, 'max'),
            minRef: vue_1.toRef(props, 'min'),
            valueSetRef: valueSetRef,
            disabledRef: vue_1.toRef(props, 'disabled'),
            mergedSizeRef: formItem.mergedSizeRef,
            toggleCheckbox
        });
        return {
            mergedClsPrefix: mergedClsPrefixRef
        };
    },
    render() {
        return (vue_1.h("div", { class: `${this.mergedClsPrefix}-checkbox-group` }, this.$slots));
    }
});
