"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 _checkbox = _interopRequireDefault(require("../../checkbox"));
var _checkboxButton = _interopRequireDefault(require("../../checkbox-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: 'VxeCheckboxGroup',
  mixins: [_ui.globalMixins.sizeMixin],
  props: {
    value: Array,
    options: Array,
    optionProps: Object,
    readonly: {
      type: Boolean,
      default: null
    },
    type: String,
    disabled: {
      type: Boolean,
      default: null
    },
    status: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().checkboxGroup.status;
      }
    },
    max: {
      type: [String, Number],
      default: null
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().checkboxGroup.size || (0, _ui.getConfig)().size;
      }
    },
    defaultConfig: Object
  },
  inject: {
    $xeForm: {
      default: null
    },
    formItemInfo: {
      from: 'xeFormItemInfo',
      default: null
    }
  },
  provide: function provide() {
    var $xeCheckboxGroup = this;
    return {
      $xeCheckboxGroup: $xeCheckboxGroup
    };
  },
  data: function data() {
    var reactData = createReactData();
    return Object.assign(Object.assign({}, {}), {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData
    });
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeIsReadonly: function computeIsReadonly() {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var $xeForm = $xeCheckboxGroup.$xeForm;
      var readonly = props.readonly;
      if (readonly === null) {
        if ($xeForm) {
          return $xeForm.readonly;
        }
        return false;
      }
      return readonly;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var $xeForm = $xeCheckboxGroup.$xeForm;
      var disabled = props.disabled;
      if (disabled === null) {
        if ($xeForm) {
          return $xeForm.disabled;
        }
        return false;
      }
      return disabled;
    },
    computeIsMaximize: function computeIsMaximize() {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var value = props.value,
        max = props.max;
      if (max) {
        return (value ? value.length : 0) >= _xeUtils.default.toNumber(max);
      }
      return false;
    },
    computeDefaultOpts: function computeDefaultOpts() {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      return Object.assign({}, props.defaultConfig);
    },
    computePropsOpts: function computePropsOpts() {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      return Object.assign({}, props.optionProps);
    },
    computeLabelField: function computeLabelField() {
      var $xeCheckboxGroup = this;
      var propsOpts = $xeCheckboxGroup.computePropsOpts;
      return propsOpts.label || 'label';
    },
    computeValueField: function computeValueField() {
      var $xeCheckboxGroup = this;
      var propsOpts = $xeCheckboxGroup.computePropsOpts;
      return propsOpts.value || 'value';
    },
    computeIconField: function computeIconField() {
      var $xeCheckboxGroup = this;
      var propsOpts = $xeCheckboxGroup.computePropsOpts;
      return propsOpts.icon || 'icon';
    },
    computeTitleField: function computeTitleField() {
      var $xeCheckboxGroup = this;
      var propsOpts = $xeCheckboxGroup.computePropsOpts;
      return propsOpts.title || 'title';
    },
    computeDisabledField: function computeDisabledField() {
      var $xeCheckboxGroup = this;
      var propsOpts = $xeCheckboxGroup.computePropsOpts;
      return propsOpts.disabled || 'disabled';
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeCheckboxGroup = this;
      $xeCheckboxGroup.$emit(type, (0, _ui.createEvent)(evnt, {
        $checkboxGroup: $xeCheckboxGroup
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeCheckboxGroup = this;
      var _events = $xeCheckboxGroup._events;
      $xeCheckboxGroup.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeCheckboxGroup.$emit('modelValue', value);
      } else {
        $xeCheckboxGroup.$emit('model-value', value);
      }
    },
    emitDefaultValue: function emitDefaultValue(value) {
      var $xeCheckboxGroup = this;
      $xeCheckboxGroup.emitModel(value);
      $xeCheckboxGroup.dispatchEvent('default-change', {
        value: value
      }, null);
    },
    loadData: function loadData(datas) {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var internalData = $xeCheckboxGroup.internalData;
      var isLoaded = internalData.isLoaded;
      var defaultOpts = $xeCheckboxGroup.computeDefaultOpts;
      var valueField = $xeCheckboxGroup.computeValueField;
      if (!isLoaded) {
        var selectMode = defaultOpts.selectMode;
        if (datas.length > 0 && _xeUtils.default.eqNull(props.value)) {
          if (selectMode === 'all') {
            $xeCheckboxGroup.$nextTick(function () {
              $xeCheckboxGroup.emitDefaultValue(datas.map(function (item) {
                return item[valueField];
              }));
            });
          } else if (selectMode === 'first' || selectMode === 'last') {
            var selectItem = _xeUtils.default[selectMode](datas);
            if (selectItem) {
              $xeCheckboxGroup.$nextTick(function () {
                if (_xeUtils.default.eqNull(props.value)) {
                  $xeCheckboxGroup.emitDefaultValue([selectItem[valueField]]);
                }
              });
            }
          }
          internalData.isLoaded = true;
        }
      }
      return $xeCheckboxGroup.$nextTick();
    },
    handleChecked: function handleChecked(params, evnt) {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var $xeForm = $xeCheckboxGroup.$xeForm;
      var formItemInfo = $xeCheckboxGroup.formItemInfo;
      var checked = params.checked,
        label = params.label;
      var checklist = props.value || [];
      var checkIndex = checklist.indexOf(label);
      if (checked) {
        if (checkIndex === -1) {
          checklist.push(label);
        }
      } else {
        checklist.splice(checkIndex, 1);
      }
      $xeCheckboxGroup.emitModel(checklist);
      $xeCheckboxGroup.dispatchEvent('change', Object.assign({}, params, {
        checklist: checklist,
        value: checklist
      }), evnt);
      // 自动更新校验状态
      if ($xeForm && formItemInfo) {
        $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, checklist);
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeCheckboxGroup = this;
      var props = $xeCheckboxGroup;
      var slots = $xeCheckboxGroup.$scopedSlots;
      var status = props.status,
        options = props.options,
        type = props.type;
      var defaultSlot = slots.default;
      var valueField = $xeCheckboxGroup.computeValueField;
      var labelField = $xeCheckboxGroup.computeLabelField;
      var disabledField = $xeCheckboxGroup.computeDisabledField;
      var iconField = $xeCheckboxGroup.computeIconField;
      var titleField = $xeCheckboxGroup.computeTitleField;
      var isReadonly = $xeCheckboxGroup.computeIsReadonly;
      var isDisabled = $xeCheckboxGroup.computeIsDisabled;
      var btnComp = type === 'button' ? _checkboxButton.default : _checkbox.default;
      return h('div', {
        class: ['vxe-checkbox-group', "type--".concat(type === 'button' ? type : ''), _defineProperty(_defineProperty(_defineProperty({}, "theme--".concat(status), status), 'is--readonly', isReadonly), 'is--disabled', isDisabled)]
      }, 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 $xeCheckboxGroup = this;
      $xeCheckboxGroup.loadData(val);
    }
  },
  created: function created() {
    var $xeCheckboxGroup = this;
    $xeCheckboxGroup.internalData = createInternalData();
  },
  mounted: function mounted() {
    var $xeCheckboxGroup = this;
    var props = $xeCheckboxGroup;
    $xeCheckboxGroup.$nextTick(function () {
      var options = props.options;
      if (options) {
        $xeCheckboxGroup.loadData(options);
      }
    });
  },
  beforeDestroy: function beforeDestroy() {
    var $xeCheckboxGroup = this;
    var reactData = $xeCheckboxGroup.reactData;
    var internalData = $xeCheckboxGroup.internalData;
    _xeUtils.default.assign(reactData, createReactData());
    _xeUtils.default.assign(internalData, createInternalData());
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */