"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'),
  warnLog = _createComponentLog.warnLog;
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeRadio',
  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],
    disabled: {
      type: Boolean,
      default: null
    },
    name: String,
    strict: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().radio.strict;
      }
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().radio.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 $xeRadio = this;
      var props = $xeRadio;
      var checkedValue = props.checkedValue,
        label = props.label;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeRadio = this;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      var disabled = $xeRadio.disabled;
      if (disabled === null) {
        if ($xeRadioGroup) {
          var isGroupDisabled = $xeRadioGroup.computeIsDisabled;
          var isGroupReadonly = $xeRadioGroup.computeIsReadonly;
          return isGroupDisabled || isGroupReadonly;
        }
      }
      return disabled;
    },
    computeName: function computeName() {
      var $xeRadio = this;
      var props = $xeRadio;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      return $xeRadioGroup ? $xeRadioGroup.name : props.name;
    },
    computeStrict: function computeStrict() {
      var $xeRadio = this;
      var props = $xeRadio;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      return $xeRadioGroup ? $xeRadioGroup.strict : props.strict;
    },
    computeIsChecked: function computeIsChecked() {
      var $xeRadio = this;
      var props = $xeRadio;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      var checkValue = $xeRadio.computeCheckValue;
      if ($xeRadioGroup) {
        return $xeRadioGroup.value === checkValue;
      }
      return props.value === checkValue;
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeRadio = this;
      $xeRadio.$emit(type, (0, _ui.createEvent)(evnt, {
        $radio: $xeRadio
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeRadio = this;
      var _events = $xeRadio._events;
      $xeRadio.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeRadio.$emit('modelValue', value);
      } else {
        $xeRadio.$emit('model-value', value);
      }
    },
    handleValue: function handleValue(checkedValue, evnt) {
      var $xeRadio = this;
      var props = $xeRadio;
      var formItemInfo = $xeRadio.formItemInfo;
      var $xeForm = $xeRadio.$xeForm;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      var content = props.content;
      if ($xeRadioGroup) {
        $xeRadioGroup.handleChecked({
          label: checkedValue,
          checkedValue: checkedValue,
          checkedLabel: content
        }, evnt);
      } else {
        $xeRadio.emitModel(checkedValue);
        $xeRadio.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 $xeRadio = this;
      var isDisabled = $xeRadio.computeIsDisabled;
      if (!isDisabled) {
        var checkValue = $xeRadio.computeCheckValue;
        $xeRadio.handleValue(checkValue, evnt);
      }
    },
    clickEvent: function clickEvent(evnt) {
      var $xeRadio = this;
      var isDisabled = $xeRadio.computeIsDisabled;
      var isStrict = $xeRadio.computeStrict;
      if (!isDisabled && !isStrict) {
        var isChecked = $xeRadio.computeIsChecked;
        if (isChecked) {
          $xeRadio.handleValue(null, evnt);
        }
      }
    },
    //
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeRadio = this;
      var props = $xeRadio;
      var $xeRadioGroup = $xeRadio.$xeRadioGroup;
      var content = props.content;
      var slots = $xeRadio.$scopedSlots;
      var vSize = $xeRadio.computeSize;
      var isDisabled = $xeRadio.computeIsDisabled;
      var name = $xeRadio.computeName;
      var isChecked = $xeRadio.computeIsChecked;
      var checkValue = $xeRadio.computeCheckValue;
      var defaultSlot = slots.default;
      if ($xeRadioGroup) {
        var isGroupReadonly = $xeRadioGroup.computeIsReadonly;
        if (isGroupReadonly) {
          if (isChecked) {
            return h('label', {
              class: ['vxe-radio vxe-radio--view', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--readonly', isGroupReadonly)]
            }, [h('span', {
              class: 'vxe-checkbox--label'
            }, defaultSlot ? defaultSlot({}) : (0, _utils.getFuncText)(content))]);
          }
          return (0, _ui.renderEmptyElement)($xeRadio);
        }
      }
      return h('label', {
        key: "".concat(checkValue),
        class: ['vxe-radio vxe-radio--default', _defineProperty(_defineProperty(_defineProperty({}, "size--".concat(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: name,
          disabled: isDisabled
        },
        on: {
          change: $xeRadio.changeEvent,
          click: $xeRadio.clickEvent
        }
      }), h('span', {
        class: ['vxe-radio--icon', isChecked ? (0, _ui.getIcon)().RADIO_CHECKED : isDisabled ? (0, _ui.getIcon)().RADIO_DISABLED_UNCHECKED : (0, _ui.getIcon)().RADIO_UNCHECKED]
      }), h('span', {
        class: 'vxe-radio--label'
      }, defaultSlot ? defaultSlot({}) : (0, _utils.getFuncText)(content))]);
    }
  },
  mounted: function mounted() {
    var $xeRadio = this;
    var props = $xeRadio;
    if (props.label !== null) {
      warnLog('vxe.error.delProp', ['label', 'checked-value']);
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */