"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-button'),
  warnLog = _createComponentLog.warnLog;
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeCheckboxButton',
  mixins: [_ui.globalMixins.sizeMixin, _ui.globalMixins.permissionMixin],
  props: {
    value: [String, Number, Boolean],
    /**
     * 已废弃，被 checkedValue 替换
     * @deprecated
     */
    label: {
      type: [String, Number, Boolean],
      default: null
    },
    title: [String, Number],
    checkedValue: {
      type: [String, Number, Boolean],
      default: true
    },
    uncheckedValue: {
      type: [String, Number, Boolean],
      default: undefined
    },
    content: [String, Number],
    icon: [String, Number],
    disabled: {
      type: Boolean,
      default: null
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().checkboxButton.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 $xeCheckboxButton = this;
      var props = $xeCheckboxButton;
      var checkedValue = props.checkedValue,
        label = props.label;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    },
    computeIsChecked: function computeIsChecked() {
      var $xeCheckboxButton = this;
      var props = $xeCheckboxButton;
      var $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
      var checkValue = $xeCheckboxButton.computeCheckValue;
      if ($xeCheckboxGroup) {
        return _xeUtils.default.includes($xeCheckboxGroup.value, checkValue);
      }
      return props.value === checkValue;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeCheckboxButton = this;
      var props = $xeCheckboxButton;
      var $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
      var disabled = props.disabled;
      var isChecked = $xeCheckboxButton.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 $xeCheckboxButton = this;
      $xeCheckboxButton.$emit(type, (0, _ui.createEvent)(evnt, {
        $checkboxButton: $xeCheckboxButton
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeCheckboxButton = this;
      var _events = $xeCheckboxButton._events;
      $xeCheckboxButton.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeCheckboxButton.$emit('modelValue', value);
      } else {
        $xeCheckboxButton.$emit('model-value', value);
      }
    },
    changeEvent: function changeEvent(evnt) {
      var $xeCheckboxButton = this;
      var props = $xeCheckboxButton;
      var $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
      var $xeForm = $xeCheckboxButton.$xeForm;
      var formItemInfo = $xeCheckboxButton.formItemInfo;
      var uncheckedValue = props.uncheckedValue;
      var isDisabled = $xeCheckboxButton.computeIsDisabled;
      var checkValue = $xeCheckboxButton.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
          };
          $xeCheckboxButton.emitModel(_value);
          $xeCheckboxButton.dispatchEvent('change', _params, evnt);
          // 自动更新校验状态
          if ($xeForm && formItemInfo) {
            $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, _value);
          }
        }
      }
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeCheckboxButton = this;
      var props = $xeCheckboxButton;
      var slots = $xeCheckboxButton.$scopedSlots;
      var $xeCheckboxGroup = $xeCheckboxButton.$xeCheckboxGroup;
      var icon = props.icon,
        content = props.content;
      var vSize = $xeCheckboxButton.computeSize;
      var isDisabled = $xeCheckboxButton.computeIsDisabled;
      var isChecked = $xeCheckboxButton.computeIsChecked;
      var checkValue = $xeCheckboxButton.computeCheckValue;
      var defaultSlot = slots.default;
      if ($xeCheckboxGroup) {
        var isGroupReadonly = $xeCheckboxGroup.computeIsReadonly;
        if (isGroupReadonly) {
          if (isChecked) {
            return h('label', {
              class: ['vxe-checkbox vxe-checkbox--button-view', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--readonly', isGroupReadonly)]
            }, [h('span', {
              class: 'vxe-checkbox--label'
            }, defaultSlot ? defaultSlot({}) : [icon ? h('span', {
              class: 'vxe-checkbox--button-icon'
            }, [h('i', {
              class: icon
            })]) : (0, _ui.renderEmptyElement)($xeCheckboxButton), content ? h('span', {
              class: 'vxe-checkbox--button-content'
            }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeCheckboxButton)])]);
          }
          return (0, _ui.renderEmptyElement)($xeCheckboxButton);
        }
      }
      return h('label', {
        key: "".concat(checkValue),
        class: ['vxe-checkbox vxe-checkbox--button', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--disabled', isDisabled)],
        attrs: {
          title: props.title
        }
      }, [h('input', {
        class: 'vxe-checkbox--input',
        domProps: {
          checked: isChecked
        },
        attrs: {
          type: 'checkbox',
          disabled: isDisabled
        },
        on: {
          change: $xeCheckboxButton.changeEvent
        }
      }), h('span', {
        class: ['vxe-checkbox--label', {
          'is--disabled': isDisabled
        }]
      }, defaultSlot ? defaultSlot({}) : [icon ? h('span', {
        class: 'vxe-checkbox--button-icon'
      }, [h('i', {
        class: icon
      })]) : (0, _ui.renderEmptyElement)($xeCheckboxButton), content ? h('span', {
        class: 'vxe-checkbox--button-content'
      }, (0, _utils.getFuncText)(content)) : (0, _ui.renderEmptyElement)($xeCheckboxButton)])]);
    }
  },
  mounted: function mounted() {
    var $xeCheckboxButton = this;
    var props = $xeCheckboxButton;
    if (props.label !== null) {
      warnLog('vxe.error.delProp', ['label', 'checked-value']);
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */