import { defineVxeComponent } from '../../ui/src/comp';
import XEUtils from 'xe-utils';
import { getFuncText } from '../../ui/src/utils';
import { getConfig, createEvent, globalMixins, renderEmptyElement } from '../../ui';
import { createComponentLog } from '../../ui/src/log';
const { warnLog } = createComponentLog('checkbox-button');
export default {
    name: 'VxeCheckboxButton',
    mixins: [
        globalMixins.sizeMixin,
        globalMixins.permissionMixin
    ],
    props: {
        value: [String, Number, Boolean],
        /**
         * 已废弃，被 checkedValue 替换
         * @deprecated
         */
        label: {
            type: [String, Number, Boolean],
            default: null
        },
        title: [String, Number],
        checkedValue: {
            type: [String, Number, Boolean],
            default: true
        },
        uncheckedValue: {
            type: [String, Number, Boolean],
            default: undefined
        },
        content: [String, Number],
        icon: [String, Number],
        disabled: {
            type: Boolean,
            default: null
        },
        size: {
            type: String,
            default: () => getConfig().checkboxButton.size || getConfig().size
        }
    },
    inject: {
        $xeForm: {
            default: null
        },
        formItemInfo: {
            from: 'xeFormItemInfo',
            default: null
        },
        $xeCheckboxGroup: {
            default: null
        }
    },
    data() {
        const reactData = {};
        return {
            xID: XEUtils.uniqueId(),
            reactData
        };
    },
    computed: Object.assign(Object.assign({}, {}), { computeCheckValue() {
            const $xeCheckboxButton = this;
            const props = $xeCheckboxButton;
            const { checkedValue, label } = props;
            return XEUtils.isUndefined(checkedValue) ? label : checkedValue;
        },
        computeIsChecked() {
            const $xeCheckboxButton = this;
            const props = $xeCheckboxButton;
            const $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
            const checkValue = $xeCheckboxButton.computeCheckValue;
            if ($xeCheckboxGroup) {
                return XEUtils.includes($xeCheckboxGroup.value, checkValue);
            }
            return props.value === checkValue;
        },
        computeIsDisabled() {
            const $xeCheckboxButton = this;
            const props = $xeCheckboxButton;
            const $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
            const { disabled } = props;
            const isChecked = $xeCheckboxButton.computeIsChecked;
            if (disabled === null) {
                if ($xeCheckboxGroup) {
                    const isGroupDisabled = $xeCheckboxGroup.computeIsDisabled;
                    const isGroupReadonly = $xeCheckboxGroup.computeIsReadonly;
                    const isGroupMaximize = $xeCheckboxGroup.computeIsMaximize;
                    return isGroupDisabled || isGroupReadonly || (isGroupMaximize && !isChecked);
                }
            }
            return disabled;
        } }),
    methods: {
        //
        // Method
        //
        dispatchEvent(type, params, evnt) {
            const $xeCheckboxButton = this;
            $xeCheckboxButton.$emit(type, createEvent(evnt, { $checkboxButton: $xeCheckboxButton }, params));
        },
        emitModel(value) {
            const $xeCheckboxButton = this;
            const { _events } = $xeCheckboxButton;
            $xeCheckboxButton.$emit('input', value);
            if (_events && _events.modelValue) {
                $xeCheckboxButton.$emit('modelValue', value);
            }
            else {
                $xeCheckboxButton.$emit('model-value', value);
            }
        },
        changeEvent(evnt) {
            const $xeCheckboxButton = this;
            const props = $xeCheckboxButton;
            const $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
            const $xeForm = $xeCheckboxButton.$xeForm;
            const formItemInfo = $xeCheckboxButton.formItemInfo;
            const { uncheckedValue } = props;
            const isDisabled = $xeCheckboxButton.computeIsDisabled;
            const checkValue = $xeCheckboxButton.computeCheckValue;
            if (!isDisabled) {
                const checked = evnt.target.checked;
                if ($xeCheckboxGroup) {
                    const value = checkValue;
                    const params = { checked, value, label: value };
                    $xeCheckboxGroup.handleChecked(params, evnt);
                }
                else {
                    const value = checked ? checkValue : uncheckedValue;
                    const params = { checked, value, label: value };
                    $xeCheckboxButton.emitModel(value);
                    $xeCheckboxButton.dispatchEvent('change', params, evnt);
                    // 自动更新校验状态
                    if ($xeForm && formItemInfo) {
                        $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value);
                    }
                }
            }
        },
        //
        // Render
        //
        renderVN(h) {
            const $xeCheckboxButton = this;
            const props = $xeCheckboxButton;
            const slots = $xeCheckboxButton.$scopedSlots;
            const $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
            const { icon, content } = props;
            const vSize = $xeCheckboxButton.computeSize;
            const isDisabled = $xeCheckboxButton.computeIsDisabled;
            const isChecked = $xeCheckboxButton.computeIsChecked;
            const checkValue = $xeCheckboxButton.computeCheckValue;
            const defaultSlot = slots.default;
            if ($xeCheckboxGroup) {
                const isGroupReadonly = $xeCheckboxGroup.computeIsReadonly;
                if (isGroupReadonly) {
                    if (isChecked) {
                        return h('label', {
                            class: ['vxe-checkbox vxe-checkbox--button-view', {
                                    [`size--${vSize}`]: vSize,
                                    'is--readonly': isGroupReadonly
                                }]
                        }, [
                            h('span', {
                                class: 'vxe-checkbox--label'
                            }, defaultSlot
                                ? defaultSlot({})
                                : [
                                    icon
                                        ? h('span', {
                                            class: 'vxe-checkbox--button-icon'
                                        }, [
                                            h('i', { class: icon })
                                        ])
                                        : renderEmptyElement($xeCheckboxButton),
                                    content
                                        ? h('span', {
                                            class: 'vxe-checkbox--button-content'
                                        }, getFuncText(content))
                                        : renderEmptyElement($xeCheckboxButton)
                                ])
                        ]);
                    }
                    return renderEmptyElement($xeCheckboxButton);
                }
            }
            return h('label', {
                key: `${checkValue}`,
                class: ['vxe-checkbox vxe-checkbox--button', {
                        [`size--${vSize}`]: vSize,
                        'is--disabled': isDisabled
                    }],
                attrs: {
                    title: props.title
                }
            }, [
                h('input', {
                    class: 'vxe-checkbox--input',
                    domProps: {
                        checked: isChecked
                    },
                    attrs: {
                        type: 'checkbox',
                        disabled: isDisabled
                    },
                    on: {
                        change: $xeCheckboxButton.changeEvent
                    }
                }),
                h('span', {
                    class: ['vxe-checkbox--label', {
                            'is--disabled': isDisabled
                        }]
                }, defaultSlot
                    ? defaultSlot({})
                    : [
                        icon
                            ? h('span', {
                                class: 'vxe-checkbox--button-icon'
                            }, [
                                h('i', { class: icon })
                            ])
                            : renderEmptyElement($xeCheckboxButton),
                        content
                            ? h('span', {
                                class: 'vxe-checkbox--button-content'
                            }, getFuncText(content))
                            : renderEmptyElement($xeCheckboxButton)
                    ])
            ]);
        }
    },
    mounted() {
        const $xeCheckboxButton = this;
        const props = $xeCheckboxButton;
        if (props.label !== null) {
            warnLog('vxe.error.delProp', ['label', 'checked-value']);
        }
    },
    render(h) {
        return this.renderVN(h);
    }
};
