'use strict';

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

var vue = require('vue');
var iconsVue = require('@ant-design/icons-vue');
require('../../../utils/index.cjs');
var FormItemContext = require('../../form/src/FormItemContext.cjs');
require('../../wave/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
require('../../config-provider/index.cjs');
var index = require('../style/index.cjs');
var props = require('./props.cjs');
var disabledContext = require('../../config-provider/src/context/disabledContext.cjs');
var log = require('../../../utils/log.cjs');
var KeyCode = require('../../../utils/KeyCode.cjs');
var Wave = require('../../wave/src/Wave.cjs');
var omit = require('../../../utils/lang/omit.cjs');
var props$1 = require('../../../utils/vue/props.cjs');

var ASwitch = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ASwitch",
  __ANT_SWITCH: true,
  inheritAttrs: false,
  props: props.switchProps(),
  slots: Object,
  // emits: ['update:checked', 'mouseup', 'change', 'click', 'keydown', 'blur'],
  setup(props, {
    attrs,
    slots,
    expose,
    emit
  }) {
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const disabledContext$1 = disabledContext.useInjectDisabled();
    const mergedDisabled = vue.computed(() => {
      var _a;
      return (_a = props.disabled) != null ? _a : disabledContext$1.value;
    });
    vue.onBeforeMount(() => {
      log.devWarning(!("defaultChecked" in attrs), "Switch", "'defaultChecked' is deprecated, please use 'v-model:checked'");
      log.devWarning(!("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.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.KeyCode.LEFT)
        setChecked(props.unCheckedValue, e);
      else if (e.keyCode === KeyCode.KeyCode.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(Wave.default, null, {
      default: () => {
        var _a;
        return [vue.createVNode("button", {
          ...omit.omit(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(iconsVue.LoadingOutlined, {
          "class": `${prefixCls.value}-loading-icon`
        }, null) : null]), vue.createVNode("span", {
          "class": `${prefixCls.value}-inner`
        }, [vue.createVNode("span", {
          "class": `${prefixCls.value}-inner-checked`
        }, [props$1.getPropsSlot(slots, props, "checkedChildren")]), vue.createVNode("span", {
          "class": `${prefixCls.value}-inner-unchecked`
        }, [props$1.getPropsSlot(slots, props, "unCheckedChildren")])])])];
      }
    }));
  }
});

exports.default = ASwitch;
//# sourceMappingURL=Switch.cjs.map
