"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 _ui = require("../../ui");
var _radio = _interopRequireDefault(require("../../radio"));
var _radioButton = _interopRequireDefault(require("../../radio-button"));
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); }
function createReactData() {
  return {};
}
function createInternalData() {
  return {
    // isLoaded: false
  };
}
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeRadioGroup',
  mixins: [_ui.globalMixins.sizeMixin, _ui.globalMixins.permissionMixin],
  props: {
    value: [String, Number, Boolean],
    disabled: {
      type: Boolean,
      default: null
    },
    readonly: {
      type: Boolean,
      default: null
    },
    type: String,
    status: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().radioGroup.status;
      }
    },
    options: Array,
    optionProps: Object,
    strict: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().radioGroup.strict;
      }
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().radioGroup.size || (0, _ui.getConfig)().size;
      }
    },
    defaultConfig: Object
  },
  inject: {
    $xeForm: {
      default: null
    },
    formItemInfo: {
      from: 'xeFormItemInfo',
      default: null
    }
  },
  provide: function provide() {
    var $xeRadioGroup = this;
    return {
      $xeRadioGroup: $xeRadioGroup
    };
  },
  data: function data() {
    var reactData = createReactData();
    return Object.assign(Object.assign({}, {}), {
      xID: _xeUtils.default.uniqueId(),
      name: _xeUtils.default.uniqueId('xe_group_'),
      reactData: reactData
    });
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeIsReadonly: function computeIsReadonly() {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      var $xeForm = $xeRadioGroup.$xeForm;
      var readonly = props.readonly;
      if (readonly === null) {
        if ($xeForm) {
          return $xeForm.readonly;
        }
        return false;
      }
      return readonly;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      var $xeForm = $xeRadioGroup.$xeForm;
      var disabled = props.disabled;
      if (disabled === null) {
        if ($xeForm) {
          return $xeForm.disabled;
        }
        return false;
      }
      return disabled;
    },
    computePropsOpts: function computePropsOpts() {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      return Object.assign({}, props.optionProps);
    },
    computeDefaultOpts: function computeDefaultOpts() {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      return Object.assign({}, props.defaultConfig);
    },
    computeLabelField: function computeLabelField() {
      var $xeRadioGroup = this;
      var propsOpts = $xeRadioGroup.computePropsOpts;
      return propsOpts.label || 'label';
    },
    computeValueField: function computeValueField() {
      var $xeRadioGroup = this;
      var propsOpts = $xeRadioGroup.computePropsOpts;
      return propsOpts.value || 'value';
    },
    computeDisabledField: function computeDisabledField() {
      var $xeRadioGroup = this;
      var propsOpts = $xeRadioGroup.computePropsOpts;
      return propsOpts.disabled || 'disabled';
    },
    computeIconField: function computeIconField() {
      var $xeRadioGroup = this;
      var propsOpts = $xeRadioGroup.computePropsOpts;
      return propsOpts.icon || 'icon';
    },
    computeTitleField: function computeTitleField() {
      var $xeRadioGroup = this;
      var propsOpts = $xeRadioGroup.computePropsOpts;
      return propsOpts.title || 'title';
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeRadioGroup = this;
      $xeRadioGroup.$emit(type, (0, _ui.createEvent)(evnt, {
        $radioGroup: $xeRadioGroup
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeRadioGroup = this;
      var _events = $xeRadioGroup._events;
      $xeRadioGroup.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeRadioGroup.$emit('modelValue', value);
      } else {
        $xeRadioGroup.$emit('model-value', value);
      }
    },
    emitDefaultValue: function emitDefaultValue(value) {
      var $xeRadioGroup = this;
      $xeRadioGroup.emitModel(value);
      $xeRadioGroup.dispatchEvent('default-change', {
        value: value
      }, null);
    },
    loadData: function loadData(datas) {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      var internalData = $xeRadioGroup.internalData;
      var isLoaded = internalData.isLoaded;
      var defaultOpts = $xeRadioGroup.computeDefaultOpts;
      var valueField = $xeRadioGroup.computeValueField;
      if (!isLoaded) {
        var selectMode = defaultOpts.selectMode;
        if (datas.length > 0 && _xeUtils.default.eqNull(props.value)) {
          if (selectMode === 'first' || selectMode === 'last') {
            var selectItem = _xeUtils.default[selectMode](datas);
            if (selectItem) {
              $xeRadioGroup.$nextTick(function () {
                if (_xeUtils.default.eqNull(props.value)) {
                  $xeRadioGroup.emitDefaultValue(selectItem[valueField]);
                }
              });
            }
          }
          internalData.isLoaded = true;
        }
      }
      return $xeRadioGroup.$nextTick();
    },
    handleChecked: function handleChecked(params, evnt) {
      var $xeRadioGroup = this;
      var $xeForm = $xeRadioGroup.$xeForm;
      var formItemInfo = $xeRadioGroup.formItemInfo;
      var checkedValue = params.checkedValue,
        checkedLabel = params.checkedLabel;
      var value = checkedValue;
      $xeRadioGroup.emitModel(value);
      $xeRadioGroup.dispatchEvent('change', {
        value: value,
        label: value,
        checkedValue: checkedValue,
        checkedLabel: checkedLabel
      }, evnt);
      // 自动更新校验状态
      if ($xeForm && formItemInfo) {
        $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value);
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeRadioGroup = this;
      var props = $xeRadioGroup;
      var slots = $xeRadioGroup.$scopedSlots;
      var options = props.options,
        type = props.type,
        status = props.status;
      var vSize = $xeRadioGroup.computeSize;
      var defaultSlot = slots.default;
      var valueField = $xeRadioGroup.computeValueField;
      var labelField = $xeRadioGroup.computeLabelField;
      var disabledField = $xeRadioGroup.computeDisabledField;
      var iconField = $xeRadioGroup.computeIconField;
      var titleField = $xeRadioGroup.computeTitleField;
      var btnComp = type === 'button' ? _radioButton.default : _radio.default;
      return h('div', {
        class: ['vxe-radio-group', "type--".concat(type === 'button' ? type : ''), _defineProperty(_defineProperty({}, "theme--".concat(status), status), "size--".concat(vSize), vSize)]
      }, defaultSlot ? defaultSlot({}) : options ? options.map(function (item) {
        return h(btnComp, {
          key: item[valueField],
          props: {
            checkedValue: item[valueField],
            content: item[labelField],
            icon: item[iconField],
            title: item[titleField],
            disabled: item[disabledField]
          }
        });
      }) : []);
    }
  },
  watch: {
    options: function options(val) {
      var $xeRadioGroup = this;
      $xeRadioGroup.loadData(val);
    }
  },
  created: function created() {
    var $xeRadioGroup = this;
    $xeRadioGroup.internalData = createInternalData();
  },
  mounted: function mounted() {
    var $xeRadioGroup = this;
    var props = $xeRadioGroup;
    $xeRadioGroup.$nextTick(function () {
      var options = props.options;
      if (options) {
        $xeRadioGroup.loadData(options);
      }
    });
  },
  beforeDestroy: function beforeDestroy() {
    var $xeRadioGroup = this;
    var reactData = $xeRadioGroup.reactData;
    var internalData = $xeRadioGroup.internalData;
    _xeUtils.default.assign(internalData, createInternalData());
    _xeUtils.default.assign(reactData, createReactData());
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */