"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
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 }; }
const {
  warnLog
} = (0, _log.createComponentLog)('checkbox');
var _default = exports.default = (0, _comp.defineVxeComponent)({
  name: 'VxeCheckbox',
  props: {
    modelValue: [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: () => (0, _ui.getConfig)().checkbox.size || (0, _ui.getConfig)().size
    }
  },
  emits: ['update:modelValue', 'change'],
  setup(props, context) {
    const {
      slots,
      emit
    } = context;
    const $xeForm = (0, _vue.inject)('$xeForm', null);
    const formItemInfo = (0, _vue.inject)('xeFormItemInfo', null);
    const $xeCheckboxGroup = (0, _vue.inject)('$xeCheckboxGroup', null);
    const xID = _xeUtils.default.uniqueId();
    const reactData = (0, _vue.reactive)({});
    const $xeCheckbox = {
      xID,
      props,
      context,
      reactData
    };
    let checkboxMethods = {};
    const {
      computeSize
    } = (0, _ui.useSize)(props);
    const computeCheckValue = (0, _vue.computed)(() => {
      const {
        checkedValue,
        label
      } = props;
      return _xeUtils.default.isUndefined(checkedValue) ? label : checkedValue;
    });
    const computeIsChecked = (0, _vue.computed)(() => {
      const checkValue = computeCheckValue.value;
      if ($xeCheckboxGroup) {
        return _xeUtils.default.includes($xeCheckboxGroup.props.modelValue, checkValue);
      }
      return props.modelValue === props.checkedValue;
    });
    const computeIsDisabled = (0, _vue.computed)(() => {
      const {
        disabled
      } = props;
      const isChecked = computeIsChecked.value;
      if (disabled === null) {
        if ($xeCheckboxGroup) {
          const {
            computeIsDisabled: computeIsGroupDisabled,
            computeIsReadonly: computeIsGroupReadonly,
            computeIsMaximize: computeIsGroupMaximize
          } = $xeCheckboxGroup.getComputeMaps();
          const isGroupDisabled = computeIsGroupDisabled.value;
          const isGroupReadonly = computeIsGroupReadonly.value;
          const isGroupMaximize = computeIsGroupMaximize.value;
          return isGroupDisabled || isGroupReadonly || isGroupMaximize && !isChecked;
        }
      }
      return disabled;
    });
    const changeEvent = evnt => {
      const {
        uncheckedValue
      } = props;
      const isDisabled = computeIsDisabled.value;
      const checkValue = computeCheckValue.value;
      if (!isDisabled) {
        const checked = evnt.target.checked;
        if ($xeCheckboxGroup) {
          const value = checkValue;
          const params = {
            checked,
            value,
            label: value
          };
          $xeCheckboxGroup.handleChecked(params, evnt);
        } else {
          const value = checked ? checkValue : uncheckedValue;
          const params = {
            checked,
            value,
            label: value
          };
          emit('update:modelValue', value);
          checkboxMethods.dispatchEvent('change', params, evnt);
          // 自动更新校验状态
          if ($xeForm && formItemInfo) {
            $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value);
          }
        }
      }
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, (0, _ui.createEvent)(evnt, {
        $checkbox: $xeCheckbox
      }, params));
    };
    checkboxMethods = {
      dispatchEvent
    };
    Object.assign($xeCheckbox, checkboxMethods);
    const renderVN = () => {
      const {
        content
      } = props;
      const vSize = computeSize.value;
      const isDisabled = computeIsDisabled.value;
      const isChecked = computeIsChecked.value;
      const checkValue = computeCheckValue.value;
      const indeterminate = !isChecked && props.indeterminate;
      const defaultSlot = slots.default;
      if ($xeCheckboxGroup) {
        const {
          computeIsReadonly: computeIsGroupReadonly
        } = $xeCheckboxGroup.getComputeMaps();
        const isGroupReadonly = computeIsGroupReadonly.value;
        if (isGroupReadonly) {
          if (isChecked) {
            return (0, _vue.h)('label', {
              class: ['vxe-checkbox vxe-checkbox--view', {
                [`size--${vSize}`]: vSize,
                'is--readonly': isGroupReadonly
              }]
            }, [(0, _vue.h)('span', {
              class: 'vxe-checkbox--label'
            }, defaultSlot ? defaultSlot({}) : (0, _utils.getFuncText)(content))]);
          }
          return (0, _ui.renderEmptyElement)($xeCheckbox);
        }
      }
      return (0, _vue.h)('label', {
        key: `${checkValue}`,
        class: ['vxe-checkbox vxe-checkbox--default', {
          [`size--${vSize}`]: vSize,
          'is--indeterminate': indeterminate,
          'is--disabled': isDisabled,
          'is--checked': isChecked
        }],
        title: props.title
      }, [(0, _vue.h)('input', {
        class: 'vxe-checkbox--input',
        type: 'checkbox',
        disabled: isDisabled,
        checked: isChecked,
        onChange: changeEvent
      }), (0, _vue.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]
      }), (0, _vue.h)('span', {
        class: 'vxe-checkbox--label'
      }, defaultSlot ? defaultSlot({}) : (0, _utils.getFuncText)(content))]);
    };
    (0, _vue.onMounted)(() => {
      if (props.label !== null) {
        warnLog('vxe.error.delProp', ['label', 'checked-value']);
      }
    });
    $xeCheckbox.renderVN = renderVN;
    return $xeCheckbox;
  },
  render() {
    return this.renderVN();
  }
});