import { defineVxeComponent } from '../../ui/src/comp';
import XEUtils from 'xe-utils';
import { getFuncText } from '../../ui/src/utils';
import { getConfig, createEvent, globalMixins, renderEmptyElement } from '../../ui';
export default {
    name: 'VxeCheckboxButton',
    mixins: [
        globalMixins.sizeMixin,
        globalMixins.permissionMixin
    ],
    props: {
        value: [String, Number, Boolean],
        label: {
            type: [String, Number, Boolean],
            default: null
        },
        title: [String, Number],
        checkedValue: {
            type: [String, Number, Boolean],
            default: true
        },
        uncheckedValue: {
            type: [String, Number, Boolean],
            default: false
        },
        content: [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({}, {}), { computeIsChecked() {
            const $xeCheckbox = this;
            const props = $xeCheckbox;
            const $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
            if ($xeCheckboxGroup) {
                return XEUtils.includes($xeCheckboxGroup.value, props.label);
            }
            return props.value === props.checkedValue;
        },
        computeIsDisabled() {
            const $xeCheckbox = this;
            const props = $xeCheckbox;
            const $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
            const { disabled } = props;
            const isChecked = $xeCheckbox.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 $xeCheckbox = this;
            $xeCheckbox.$emit(type, createEvent(evnt, { $checkbox: $xeCheckbox }, params));
        },
        emitModel(value) {
            const $xeCheckbox = this;
            const { _events } = $xeCheckbox;
            $xeCheckbox.$emit('input', value);
            if (_events && _events.modelValue) {
                $xeCheckbox.$emit('modelValue', value);
            }
            else {
                $xeCheckbox.$emit('model-value', value);
            }
        },
        changeEvent(evnt) {
            const $xeCheckbox = this;
            const props = $xeCheckbox;
            const $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
            const $xeForm = $xeCheckbox.$xeForm;
            const formItemInfo = $xeCheckbox.formItemInfo;
            const { checkedValue, uncheckedValue } = props;
            const isDisabled = $xeCheckbox.computeIsDisabled;
            if (!isDisabled) {
                const checked = evnt.target.checked;
                const value = checked ? checkedValue : uncheckedValue;
                const params = { checked, value, label: props.label };
                if ($xeCheckboxGroup) {
                    $xeCheckboxGroup.handleChecked(params, evnt);
                }
                else {
                    $xeCheckbox.emitModel(value);
                    $xeCheckbox.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 { label, content } = props;
            const vSize = $xeCheckboxButton.computeSize;
            const isDisabled = $xeCheckboxButton.computeIsDisabled;
            const isChecked = $xeCheckboxButton.computeIsChecked;
            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({}) : getFuncText(content))
                        ]);
                    }
                    return renderEmptyElement($xeCheckboxButton);
                }
            }
            return h('label', {
                key: label,
                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'
                }, defaultSlot ? defaultSlot({}) : getFuncText(props.content))
            ]);
        }
    },
    render(h) {
        return this.renderVN(h);
    }
};
