"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
var _comp = require("../../ui/src/comp");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _utils = require("../../ui/src/utils");
var _ui = require("../../ui");
var _log = require("../../ui/src/log");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const {
  warnLog
} = (0, _log.createComponentLog)('radio-button');
var _default = exports.default = (0, _comp.defineVxeComponent)({
  name: 'VxeRadioButton',
  props: {
    modelValue: [String, Number, Boolean],
    checkedValue: {
      type: [String, Number, Boolean],
      default: undefined
    },
    title: [String, Number],
    content: [String, Number],
    icon: String,
    disabled: {
      type: Boolean,
      default: null
    },
    strict: {
      type: Boolean,
      default: () => (0, _ui.getConfig)().radioButton.strict
    },
    size: {
      type: String,
      default: () => (0, _ui.getConfig)().radioButton.size || (0, _ui.getConfig)().size
    },
    /**
     * 已废弃，被 checkedValue 替换
     * @deprecated
     */
    label: {
      type: [String, Number, Boolean],
      default: null
    }
  },
  emits: ['update:modelValue', 'change'],
  setup(props, context) {
    const {
      slots,
      emit
    } = context;
    const $xeForm = (0, _vue.inject)('$xeForm', null);
    const formItemInfo = (0, _vue.inject)('xeFormItemInfo', null);
    const $xeRadioGroup = (0, _vue.inject)('$xeRadioGroup', null);
    const xID = _xeUtils.default.uniqueId();
    const reactData = (0, _vue.reactive)({});
    const {
      computeSize
    } = (0, _ui.useSize)(props);
    const $xeRadioButton = {
      xID,
      props,
      context,
      reactData
    };
    const computeCheckValue = (0, _vue.computed)(() => {
      const {
        checkedValue,
        label
      } = props;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    });
    const computeIsDisabled = (0, _vue.computed)(() => {
      const {
        disabled
      } = props;
      if (disabled === null) {
        if ($xeRadioGroup) {
          const {
            computeIsDisabled: computeIsGroupDisabled,
            computeIsReadonly: computeIsGroupReadonly
          } = $xeRadioGroup.getComputeMaps();
          const isGroupDisabled = computeIsGroupDisabled.value;
          const isGroupReadonly = computeIsGroupReadonly.value;
          return isGroupDisabled || isGroupReadonly;
        }
      }
      return disabled;
    });
    const computeName = (0, _vue.computed)(() => {
      return $xeRadioGroup ? $xeRadioGroup.name : null;
    });
    const computeStrict = (0, _vue.computed)(() => {
      return $xeRadioGroup ? $xeRadioGroup.props.strict : props.strict;
    });
    const computeIsChecked = (0, _vue.computed)(() => {
      const checkValue = computeCheckValue.value;
      if ($xeRadioGroup) {
        return $xeRadioGroup.props.modelValue === checkValue;
      }
      return props.modelValue === checkValue;
    });
    const dispatchEvent = (type, params, evnt) => {
      emit(type, (0, _ui.createEvent)(evnt, {
        $radioGroup: $xeRadioGroup
      }, params));
    };
    const radioButtonMethods = {
      dispatchEvent
    };
    const radioButtonPrivateMethods = {};
    Object.assign($xeRadioButton, radioButtonMethods, radioButtonPrivateMethods);
    const handleValue = (checkedValue, evnt) => {
      const {
        content
      } = props;
      if ($xeRadioGroup) {
        $xeRadioGroup.handleChecked({
          label: checkedValue,
          checkedValue,
          checkedLabel: content
        }, evnt);
      } else {
        emit('update:modelValue', checkedValue);
        dispatchEvent('change', {
          value: checkedValue,
          label: checkedValue,
          checkedValue,
          checkedLabel: content
        }, evnt);
        // 自动更新校验状态
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, checkedValue);
        }
      }
    };
    const changeEvent = evnt => {
      const isDisabled = computeIsDisabled.value;
      if (!isDisabled) {
        const checkValue = computeCheckValue.value;
        handleValue(checkValue, evnt);
      }
    };
    const clickEvent = evnt => {
      const isDisabled = computeIsDisabled.value;
      const isStrict = computeStrict.value;
      if (!isDisabled && !isStrict) {
        const isChecked = computeIsChecked.value;
        if (isChecked) {
          handleValue(null, evnt);
        }
      }
    };
    const renderVN = () => {
      const {
        icon,
        content
      } = props;
      const vSize = computeSize.value;
      const isDisabled = computeIsDisabled.value;
      const name = computeName.value;
      const isChecked = computeIsChecked.value;
      const checkValue = computeCheckValue.value;
      const defaultSlot = slots.default;
      if ($xeRadioGroup) {
        const {
          computeIsReadonly: computeIsGroupReadonly
        } = $xeRadioGroup.getComputeMaps();
        const isGroupReadonly = computeIsGroupReadonly.value;
        if (isGroupReadonly) {
          if (isChecked) {
            return (0, _vue.h)('label', {
              class: ['vxe-radio vxe-radio--button-view', {
                [`size--${vSize}`]: vSize,
                'is--readonly': isGroupReadonly
              }]
            }, [(0, _vue.h)('span', {
              class: 'vxe-radio--label'
            }, defaultSlot ? defaultSlot({}) : [icon ? (0, _vue.h)('span', {
              class: 'vxe-radio--button-icon'
            }, [(0, _vue.h)('i', {
              class: icon
            })]) : (0, _ui.renderEmptyElement)($xeRadioButton), content ? (0, _vue.h)('span', {
              class: 'vxe-radio--button-content'
            }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeRadioButton)])]);
          }
          return (0, _ui.renderEmptyElement)($xeRadioButton);
        }
      }
      return (0, _vue.h)('label', {
        key: `${checkValue}`,
        class: ['vxe-radio vxe-radio--button', {
          [`size--${vSize}`]: vSize,
          'is--disabled': isDisabled
        }],
        title: props.title
      }, [(0, _vue.h)('input', {
        class: 'vxe-radio--input',
        type: 'radio',
        name,
        checked: isChecked,
        disabled: isDisabled,
        onChange: changeEvent,
        onClick: clickEvent
      }), (0, _vue.h)('span', {
        class: ['vxe-radio--label', {
          'is--disabled': isDisabled
        }]
      }, defaultSlot ? defaultSlot({}) : [icon ? (0, _vue.h)('span', {
        class: 'vxe-radio--button-icon'
      }, [(0, _vue.h)('i', {
        class: icon
      })]) : (0, _ui.renderEmptyElement)($xeRadioButton), content ? (0, _vue.h)('span', {
        class: 'vxe-radio--button-content'
      }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeRadioButton)])]);
    };
    (0, _vue.onMounted)(() => {
      if (props.label !== null) {
        warnLog('vxe.error.delProp', ['label', 'checked-value']);
      }
    });
    $xeRadioButton.renderVN = renderVN;
    return renderVN;
  }
});