"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 _utils = require("../../ui/src/utils");
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 _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeSwitch',
  mixins: [_ui.globalMixins.sizeMixin, _ui.globalMixins.permissionMixin],
  props: {
    value: [String, Number, Boolean],
    disabled: {
      type: Boolean,
      default: null
    },
    readonly: {
      type: Boolean,
      default: null
    },
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().switch.size || (0, _ui.getConfig)().size;
      }
    },
    openLabel: String,
    closeLabel: String,
    openValue: {
      type: [String, Number, Boolean],
      default: true
    },
    closeValue: {
      type: [String, Number, Boolean],
      default: false
    },
    openIcon: String,
    closeIcon: String,
    openActiveIcon: String,
    closeActiveIcon: String
  },
  inject: {
    $xeForm: {
      default: null
    },
    formItemInfo: {
      from: 'xeFormItemInfo',
      default: null
    }
  },
  data: function data() {
    var reactData = {
      isActivated: false,
      hasAnimat: false,
      offsetLeft: 0
    };
    var internalData = {};
    return {
      xID: _xeUtils.default.uniqueId(),
      reactData: reactData,
      internalData: internalData
    };
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeIsReadonly: function computeIsReadonly() {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var $xeForm = $xeSwitch.$xeForm;
      var readonly = props.readonly;
      if (readonly === null) {
        if ($xeForm) {
          return $xeForm.readonly;
        }
        return false;
      }
      return readonly;
    },
    computeIsDisabled: function computeIsDisabled() {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var $xeForm = $xeSwitch.$xeForm;
      var disabled = props.disabled;
      if (disabled === null) {
        if ($xeForm) {
          return $xeForm.disabled;
        }
        return false;
      }
      return disabled;
    },
    computeOnShowLabel: function computeOnShowLabel() {
      var $xeSwitch = this;
      var props = $xeSwitch;
      return (0, _utils.getFuncText)(props.openLabel);
    },
    computeOffShowLabel: function computeOffShowLabel() {
      var $xeSwitch = this;
      var props = $xeSwitch;
      return (0, _utils.getFuncText)(props.closeLabel);
    },
    computeIsChecked: function computeIsChecked() {
      var $xeSwitch = this;
      var props = $xeSwitch;
      return props.value === props.openValue;
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeSwitch = this;
      $xeSwitch.$emit(type, (0, _ui.createEvent)(evnt, {
        $switch: $xeSwitch
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeSwitch = this;
      var _events = $xeSwitch._events;
      $xeSwitch.$emit('input', value);
      if (_events && _events.modelValue) {
        $xeSwitch.$emit('modelValue', value);
      } else {
        $xeSwitch.$emit('model-value', value);
      }
    },
    focus: function focus() {
      var $xeSwitch = this;
      var reactData = $xeSwitch.reactData;
      var btnElem = $xeSwitch.$refs.refButton;
      reactData.isActivated = true;
      if (btnElem) {
        btnElem.focus();
      }
      return $xeSwitch.$nextTick();
    },
    blur: function blur() {
      var $xeSwitch = this;
      var reactData = $xeSwitch.reactData;
      var btnElem = $xeSwitch.$refs.refButton;
      if (btnElem) {
        btnElem.blur();
      }
      reactData.isActivated = false;
      return $xeSwitch.$nextTick();
    },
    clickEvent: function clickEvent(evnt) {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var reactData = $xeSwitch.reactData;
      var internalData = $xeSwitch.internalData;
      var $xeForm = $xeSwitch.$xeForm;
      var formItemInfo = $xeSwitch.formItemInfo;
      var isDisabled = $xeSwitch.computeIsDisabled;
      var isReadonly = $xeSwitch.computeIsReadonly;
      if (!(isDisabled || isReadonly)) {
        var isChecked = $xeSwitch.computeIsChecked;
        clearTimeout(internalData.atTimeout);
        var value = isChecked ? props.closeValue : props.openValue;
        reactData.hasAnimat = true;
        $xeSwitch.emitModel(value);
        $xeSwitch.dispatchEvent('change', {
          value: value
        }, evnt);
        // 自动更新校验状态
        if ($xeForm && formItemInfo) {
          $xeForm.triggerItemEvent(evnt, formItemInfo.itemConfig.field, value);
        }
        internalData.atTimeout = setTimeout(function () {
          reactData.hasAnimat = false;
          internalData.atTimeout = undefined;
        }, 400);
      }
    },
    focusEvent: function focusEvent(evnt) {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var reactData = $xeSwitch.reactData;
      reactData.isActivated = true;
      $xeSwitch.dispatchEvent('focus', {
        value: props.value
      }, evnt);
    },
    blurEvent: function blurEvent(evnt) {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var reactData = $xeSwitch.reactData;
      reactData.isActivated = false;
      $xeSwitch.dispatchEvent('blur', {
        value: props.value
      }, evnt);
    },
    //
    // Render
    //
    renderVN: function renderVN(h) {
      var $xeSwitch = this;
      var props = $xeSwitch;
      var reactData = $xeSwitch.reactData;
      var openIcon = props.openIcon,
        closeIcon = props.closeIcon,
        openActiveIcon = props.openActiveIcon,
        closeActiveIcon = props.closeActiveIcon;
      var vSize = $xeSwitch.computeSize;
      var isChecked = $xeSwitch.computeIsChecked;
      var onShowLabel = $xeSwitch.computeOnShowLabel;
      var offShowLabel = $xeSwitch.computeOffShowLabel;
      var isDisabled = $xeSwitch.computeIsDisabled;
      var isReadonly = $xeSwitch.computeIsReadonly;
      if (isReadonly) {
        return h('div', {
          class: ['vxe-switch', isChecked ? 'is--on' : 'is--off', _defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--readonly', isReadonly)]
        }, isChecked ? onShowLabel || (0, _ui.getI18n)('vxe.switch.onText') : offShowLabel || (0, _ui.getI18n)('vxe.switch.offText'));
      }
      return h('div', {
        class: ['vxe-switch', isChecked ? 'is--on' : 'is--off', _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--disabled', isDisabled), 'is--readonly', isReadonly), 'is--animat', reactData.hasAnimat)]
      }, [h('button', {
        ref: 'refButton',
        class: 'vxe-switch--button',
        attrs: {
          type: 'button',
          disabled: isDisabled || isReadonly
        },
        on: {
          click: $xeSwitch.clickEvent,
          focus: $xeSwitch.focusEvent,
          blur: $xeSwitch.blurEvent
        }
      }, [h('span', {
        class: 'vxe-switch--label vxe-switch--label-on'
      }, [openIcon ? h('i', {
        class: ['vxe-switch--label-icon', openIcon]
      }) : (0, _ui.renderEmptyElement)($xeSwitch), onShowLabel]), h('span', {
        class: 'vxe-switch--label vxe-switch--label-off'
      }, [closeIcon ? h('i', {
        class: ['vxe-switch--label-icon', closeIcon]
      }) : (0, _ui.renderEmptyElement)($xeSwitch), offShowLabel]), h('span', {
        class: ['vxe-switch--icon']
      }, openActiveIcon || closeActiveIcon ? [h('i', {
        class: isChecked ? openActiveIcon : closeActiveIcon
      })] : [])])]);
    }
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */