"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)('checkbox'),
  warnLog = _createComponentLog.warnLog;
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeCheckbox',
  mixins: [_ui.globalMixins.sizeMixin],
  props: {
    value: [String, Number, Boolean],
    /**
     * 已废弃，被 checkedValue 替换
     * @deprecated
     */
    label: {
      type: [String, Number],
      default: null
    },
    indeterminate: Boolean,
    title: [String, Number],
    checkedValue: {
      type: [String, Number, Boolean],
      default: true
    },
    uncheckedValue: {
      type: [String, Number, Boolean],
      default: false
    },
    content: [String, Number],
    disabled: {
      type: Boolean,
      default: null
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().checkbox.size || (0, _ui.getConfig)().size;
      }
    }
  },
  inject: {
    $xeForm: {
      default: null
    },
    formItemInfo: {
      from: 'xeFormItemInfo',
      default: null
    },
    $xeCheckboxGroup: {
      default: null
    }
  },
  data: function data() {
    var reactData = {};
    return {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData
    };
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeCheckValue: function computeCheckValue() {
      var $xeCheckbox = this;
      var props = $xeCheckbox;
      var checkedValue = props.checkedValue,
        label = props.label;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    },
    computeIsChecked: function computeIsChecked() {
      var $xeCheckbox = this;
      var props = $xeCheckbox;
      var $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
      var checkValue = $xeCheckbox.computeCheckValue;
      if ($xeCheckboxGroup) {
        return _xeUtils.default.includes($xeCheckboxGroup.value, checkValue);
      }
      return props.value === props.checkedValue;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeCheckbox = this;
      var props = $xeCheckbox;
      var $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
      var disabled = props.disabled;
      var isChecked = $xeCheckbox.computeIsChecked;
      if (disabled === null) {
        if ($xeCheckboxGroup) {
          var isGroupDisabled = $xeCheckboxGroup.computeIsDisabled;
          var isGroupReadonly = $xeCheckboxGroup.computeIsReadonly;
          var isGroupMaximize = $xeCheckboxGroup.computeIsMaximize;
          return isGroupDisabled || isGroupReadonly || isGroupMaximize && !isChecked;
        }
      }
      return disabled;
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeCheckbox = this;
      $xeCheckbox.$emit(type, (0, _ui.createEvent)(evnt, {
        $checkbox: $xeCheckbox
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeCheckbox = this;
      var _events = $xeCheckbox._events;
      $xeCheckbox.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeCheckbox.$emit('modelValue', value);
      } else {
        $xeCheckbox.$emit('model-value', value);
      }
    },
    changeEvent: function changeEvent(evnt) {
      var $xeCheckbox = this;
      var props = $xeCheckbox;
      var $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
      var $xeForm = $xeCheckbox.$xeForm;
      var formItemInfo = $xeCheckbox.formItemInfo;
      var uncheckedValue = props.uncheckedValue;
      var isDisabled = $xeCheckbox.computeIsDisabled;
      var checkValue = $xeCheckbox.computeCheckValue;
      if (!isDisabled) {
        var checked = evnt.target.checked;
        if ($xeCheckboxGroup) {
          var value = checkValue;
          var params = {
            checked: checked,
            value: value,
            label: value
          };
          $xeCheckboxGroup.handleChecked(params, evnt);
        } else {
          var _value = checked ? checkValue : uncheckedValue;
          var _params = {
            checked: checked,
            value: _value,
            label: _value
          };
          $xeCheckbox.emitModel(_value);
          $xeCheckbox.dispatchEvent('change', _params, evnt);
          // 自动更新校验状态
          if ($xeForm && formItemInfo) {
            $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, _value);
          }
        }
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeCheckbox = this;
      var props = $xeCheckbox;
      var slots = $xeCheckbox.$scopedSlots;
      var $xeCheckboxGroup = $xeCheckbox.$xeCheckboxGroup;
      var content = props.content;
      var vSize = $xeCheckbox.computeSize;
      var isDisabled = $xeCheckbox.computeIsDisabled;
      var isChecked = $xeCheckbox.computeIsChecked;
      var checkValue = $xeCheckbox.computeCheckValue;
      var indeterminate = !isChecked && props.indeterminate;
      var defaultSlot = slots.default;
      if ($xeCheckboxGroup) {
        var isGroupReadonly = $xeCheckboxGroup.computeIsReadonly;
        if (isGroupReadonly) {
          if (isChecked) {
            return h('label', {
              class: ['vxe-checkbox vxe-checkbox--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)($xeCheckbox);
        }
      }
      return h('label', {
        key: "".concat(checkValue),
        class: ['vxe-checkbox vxe-checkbox--default', _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--indeterminate', indeterminate), 'is--disabled', isDisabled), 'is--checked', isChecked)],
        attrs: {
          title: props.title
        }
      }, [h('input', {
        class: 'vxe-checkbox--input',
        domProps: {
          checked: isChecked
        },
        attrs: {
          type: 'checkbox',
          disabled: isDisabled
        },
        on: {
          change: $xeCheckbox.changeEvent
        }
      }), h('span', {
        class: ['vxe-checkbox--icon', indeterminate ? (0, _ui.getIcon)().CHECKBOX_INDETERMINATE : isChecked ? (0, _ui.getIcon)().CHECKBOX_CHECKED : isDisabled ? (0, _ui.getIcon)().CHECKBOX_DISABLED_UNCHECKED : (0, _ui.getIcon)().CHECKBOX_UNCHECKED]
      }), h('span', {
        class: 'vxe-checkbox--label'
      }, defaultSlot ? defaultSlot({}) : (0, _utils.getFuncText)(content))]);
    }
  },
  mounted: function mounted() {
    var $xeCheckbox = this;
    var props = $xeCheckbox;
    if (props.label !== null) {
      warnLog('vxe.error.delProp', ['label', 'checked-value']);
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */