'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var CloseOutlined = require('@ant-design/icons-vue');
var FormItemContext = require('../form/FormItemContext.js');
var index$1 = require('../_util/vue-types/index.js');
var KeyCode = require('../_util/KeyCode.js');
var index$3 = require('../vc-wave/index.js');
var warning = require('../_util/warning.js');
var type = require('../_util/type.js');
var index$4 = require('../_util/props-util/index.js');
require('../hooks/index.js');
var omit = require('../_util/omit.js');
var DisabledContext = require('../config-provider/DisabledContext.js');
var index$2 = require('./style/index.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');

const SwitchSizes = type.tuple("small", "default");
const switchProps = () => ({
  "id": String,
  "prefixCls": String,
  "size": index$1.default.oneOf(SwitchSizes),
  "disabled": {
    type: Boolean,
    default: void 0
  },
  "checkedChildren": index$1.default.any,
  "unCheckedChildren": index$1.default.any,
  "tabindex": index$1.default.oneOfType([index$1.default.string, index$1.default.number]),
  "autofocus": {
    type: Boolean,
    default: void 0
  },
  "loading": {
    type: Boolean,
    default: void 0
  },
  "checked": index$1.default.oneOfType([index$1.default.string, index$1.default.number, index$1.default.looseBool]),
  "checkedValue": index$1.default.oneOfType([index$1.default.string, index$1.default.number, index$1.default.looseBool]).def(true),
  "unCheckedValue": index$1.default.oneOfType([index$1.default.string, index$1.default.number, index$1.default.looseBool]).def(false),
  "onChange": {
    type: Function
  },
  "onClick": {
    type: Function
  },
  "onKeydown": {
    type: Function
  },
  "onMouseup": {
    type: Function
  },
  "onUpdate:checked": {
    type: Function
  },
  "onBlur": Function,
  "onFocus": Function
});
const Switch = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ASwitch",
  __ANT_SWITCH: true,
  inheritAttrs: false,
  props: switchProps(),
  slots: Object,
  // emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown', 'blur'],
  setup(props, {
    attrs,
    slots,
    expose,
    emit
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const disabledContext = DisabledContext.useInjectDisabled();
    const mergedDisabled = vue.computed(() => {
      var _a;
      return (_a = props.disabled) != null ? _a : disabledContext.value;
    });
    vue.onBeforeMount(() => {
      warning.default(!("defaultChecked" in attrs), "Switch", "'defaultChecked' is deprecated, please use 'v-model:checked'");
      warning.default(!("value" in attrs), "Switch", "`value` is not validate prop, do you mean `checked`?");
    });
    const checked = vue.ref(props.checked !== void 0 ? props.checked : attrs.defaultChecked);
    const checkedStatus = vue.computed(() => checked.value === props.checkedValue);
    vue.watch(() => props.checked, () => {
      checked.value = props.checked;
    });
    const {
      prefixCls,
      direction,
      size
    } = useConfigInject.default("switch", props);
    const [wrapSSR, hashId] = index$2.default(prefixCls);
    const refSwitchNode = vue.ref();
    const focus = () => {
      var _a;
      (_a = refSwitchNode.value) == null ? void 0 : _a.focus();
    };
    const blur = () => {
      var _a;
      (_a = refSwitchNode.value) == null ? void 0 : _a.blur();
    };
    expose({
      focus,
      blur
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        if (props.autofocus && !mergedDisabled.value)
          refSwitchNode.value.focus();
      });
    });
    const setChecked = (check, e) => {
      if (mergedDisabled.value)
        return;
      emit("update:checked", check);
      emit("change", check, e);
      formItemContext.onFieldChange();
    };
    const handleBlur = (e) => {
      emit("blur", e);
    };
    const handleClick = (e) => {
      focus();
      const newChecked = checkedStatus.value ? props.unCheckedValue : props.checkedValue;
      setChecked(newChecked, e);
      emit("click", newChecked, e);
    };
    const handleKeyDown = (e) => {
      if (e.keyCode === KeyCode.default.LEFT)
        setChecked(props.unCheckedValue, e);
      else if (e.keyCode === KeyCode.default.RIGHT)
        setChecked(props.checkedValue, e);
      emit("keydown", e);
    };
    const handleMouseUp = (e) => {
      var _a;
      (_a = refSwitchNode.value) == null ? void 0 : _a.blur();
      emit("mouseup", e);
    };
    const classNames = vue.computed(() => ({
      [`${prefixCls.value}-small`]: size.value === "small",
      [`${prefixCls.value}-loading`]: props.loading,
      [`${prefixCls.value}-checked`]: checkedStatus.value,
      [`${prefixCls.value}-disabled`]: mergedDisabled.value,
      [prefixCls.value]: true,
      [`${prefixCls.value}-rtl`]: direction.value === "rtl",
      [hashId.value]: true
    }));
    return () => wrapSSR(vue.createVNode(index$3.default, null, {
      default: () => {
        var _a;
        return [vue.createVNode("button", vue.mergeProps(omit.default(props, ["prefixCls", "checkedChildren", "unCheckedChildren", "checked", "autofocus", "checkedValue", "unCheckedValue", "id", "onChange", "onUpdate:checked"]), attrs, {
          "id": (_a = props.id) != null ? _a : formItemContext.id.value,
          "onKeydown": handleKeyDown,
          "onClick": handleClick,
          "onBlur": handleBlur,
          "onMouseup": handleMouseUp,
          "type": "button",
          "role": "switch",
          "aria-checked": checked.value,
          "disabled": mergedDisabled.value || props.loading,
          "class": [attrs.class, classNames.value],
          "ref": refSwitchNode
        }), [vue.createVNode("div", {
          "class": `${prefixCls.value}-handle`
        }, [props.loading ? vue.createVNode(CloseOutlined.LoadingOutlined, {
          "class": `${prefixCls.value}-loading-icon`
        }, null) : null]), vue.createVNode("span", {
          "class": `${prefixCls.value}-inner`
        }, [vue.createVNode("span", {
          "class": `${prefixCls.value}-inner-checked`
        }, [index$4.getPropsSlot(slots, props, "checkedChildren")]), vue.createVNode("span", {
          "class": `${prefixCls.value}-inner-unchecked`
        }, [index$4.getPropsSlot(slots, props, "unCheckedChildren")])])])];
      }
    }));
  }
});
var index = type.withInstall(Switch);

exports.SwitchSizes = SwitchSizes;
exports.default = index;
exports.switchProps = switchProps;
//# sourceMappingURL=index.js.map
