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