"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
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 }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var _createComponentLog = (0, _log.createComponentLog)('radio-button'),
  warnLog = _createComponentLog.warnLog;
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeRadioButton',
  mixins: [_ui.globalMixins.sizeMixin, _ui.globalMixins.permissionMixin],
  props: {
    value: [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: function _default() {
        return (0, _ui.getConfig)().radioButton.strict;
      }
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().radioButton.size || (0, _ui.getConfig)().size;
      }
    },
    /**
     * 已废弃，被 checkedValue 替换
     * @deprecated
     */
    label: {
      type: [String, Number, Boolean],
      default: null
    }
  },
  inject: {
    $xeForm: {
      default: null
    },
    formItemInfo: {
      from: 'xeFormItemInfo',
      default: null
    },
    $xeRadioGroup: {
      default: null
    }
  },
  data: function data() {
    var reactData = {};
    return {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData
    };
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeCheckValue: function computeCheckValue() {
      var $xeRadioButton = this;
      var props = $xeRadioButton;
      var checkedValue = props.checkedValue,
        label = props.label;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeRadioButton = this;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      var disabled = $xeRadioButton.disabled;
      if (disabled === null) {
        if ($xeRadioGroup) {
          var isGroupDisabled = $xeRadioGroup.computeIsDisabled;
          var isGroupReadonly = $xeRadioGroup.computeIsReadonly;
          return isGroupDisabled || isGroupReadonly;
        }
      }
      return disabled;
    },
    computeName: function computeName() {
      var $xeRadioButton = this;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      return $xeRadioGroup ? $xeRadioGroup.name : null;
    },
    computeStrict: function computeStrict() {
      var $xeRadioButton = this;
      var props = $xeRadioButton;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      return $xeRadioGroup ? $xeRadioGroup.strict : props.strict;
    },
    computeIsChecked: function computeIsChecked() {
      var $xeRadioButton = this;
      var props = $xeRadioButton;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      var checkValue = $xeRadioButton.computeCheckValue;
      if ($xeRadioGroup) {
        return $xeRadioGroup.value === checkValue;
      }
      return props.value === checkValue;
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeRadioButton = this;
      $xeRadioButton.$emit(type, (0, _ui.createEvent)(evnt, {
        $radioButton: $xeRadioButton
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeRadioButton = this;
      var _events = $xeRadioButton._events;
      $xeRadioButton.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeRadioButton.$emit('modelValue', value);
      } else {
        $xeRadioButton.$emit('model-value', value);
      }
    },
    handleValue: function handleValue(checkedValue, evnt) {
      var $xeRadioButton = this;
      var props = $xeRadioButton;
      var $xeForm = $xeRadioButton.$xeForm;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      var formItemInfo = $xeRadioButton.formItemInfo;
      var content = props.content;
      if ($xeRadioGroup) {
        $xeRadioGroup.handleChecked({
          label: checkedValue,
          checkedValue: checkedValue,
          checkedLabel: content
        }, evnt);
      } else {
        $xeRadioButton.emitModel(checkedValue);
        $xeRadioButton.dispatchEvent('change', {
          value: checkedValue,
          label: checkedValue,
          checkedValue: checkedValue,
          checkedLabel: content
        }, evnt);
        // 自动更新校验状态
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, checkedValue);
        }
      }
    },
    changeEvent: function changeEvent(evnt) {
      var $xeRadioButton = this;
      var isDisabled = $xeRadioButton.computeIsDisabled;
      if (!isDisabled) {
        var checkValue = $xeRadioButton.computeCheckValue;
        $xeRadioButton.handleValue(checkValue, evnt);
      }
    },
    clickEvent: function clickEvent(evnt) {
      var $xeRadioButton = this;
      var isDisabled = $xeRadioButton.computeIsDisabled;
      var isStrict = $xeRadioButton.computeStrict;
      if (!isDisabled && !isStrict) {
        var isChecked = $xeRadioButton.computeIsChecked;
        if (isChecked) {
          $xeRadioButton.handleValue(null, evnt);
        }
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeRadioButton = this;
      var props = $xeRadioButton;
      var slots = $xeRadioButton.$scopedSlots;
      var $xeRadioGroup = $xeRadioButton.$xeRadioGroup;
      var icon = props.icon,
        content = props.content;
      var vSize = $xeRadioButton.computeSize;
      var isDisabled = $xeRadioButton.computeIsDisabled;
      var name = $xeRadioButton.computeName;
      var isChecked = $xeRadioButton.computeIsChecked;
      var checkValue = $xeRadioButton.computeCheckValue;
      var defaultSlot = slots.default;
      if ($xeRadioGroup) {
        var isGroupReadonly = $xeRadioGroup.computeIsReadonly;
        if (isGroupReadonly) {
          if (isChecked) {
            return h('label', {
              class: ['vxe-radio vxe-radio--button-view', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--readonly', isGroupReadonly)]
            }, [h('span', {
              class: 'vxe-checkbox--label'
            }, defaultSlot ? defaultSlot({}) : [icon ? h('span', {
              class: 'vxe-radio--button-icon'
            }, [h('i', {
              class: icon
            })]) : (0, _ui.renderEmptyElement)($xeRadioButton), content ? h('span', {
              class: 'vxe-radio--button-content'
            }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeRadioButton)])]);
          }
          return (0, _ui.renderEmptyElement)($xeRadioButton);
        }
      }
      return h('label', {
        key: "".concat(checkValue),
        class: ['vxe-radio vxe-radio--button', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--disabled', isDisabled)],
        attrs: {
          title: props.title
        }
      }, [h('input', {
        class: 'vxe-radio--input',
        domProps: {
          checked: isChecked
        },
        attrs: {
          type: 'radio',
          name: name,
          disabled: isDisabled
        },
        on: {
          change: $xeRadioButton.changeEvent,
          click: $xeRadioButton.clickEvent
        }
      }), h('span', {
        class: ['vxe-radio--label', {
          'is--disabled': isDisabled
        }]
      }, defaultSlot ? defaultSlot({}) : [icon ? h('span', {
        class: 'vxe-radio--button-icon'
      }, [h('i', {
        class: icon
      })]) : (0, _ui.renderEmptyElement)($xeRadioButton), content ? h('span', {
        class: 'vxe-radio--button-content'
      }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeRadioButton)])]);
    }
  },
  mounted: function mounted() {
    var $xeRadioButton = this;
    var props = $xeRadioButton;
    if (props.label !== null) {
      warnLog('vxe.error.delProp', ['label', 'checked-value']);
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */