import { defineComponent, computed, onBeforeMount, ref, watch, onMounted, nextTick, createVNode } from 'vue';
import { LoadingOutlined } from '@ant-design/icons-vue';
import '../../../utils/index.mjs';
import { useInjectFormItemContext } from '../../form/src/FormItemContext.mjs';
import '../../wave/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import '../../config-provider/index.mjs';
import useStyle from '../style/index.mjs';
import { switchProps } from './props.mjs';
import { useInjectDisabled } from '../../config-provider/src/context/disabledContext.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { KeyCode } from '../../../utils/KeyCode.mjs';
import Wave from '../../wave/src/Wave.mjs';
import { omit } from '../../../utils/lang/omit.mjs';
import { getPropsSlot } from '../../../utils/vue/props.mjs';

var ASwitch = /* @__PURE__ */ 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 = useInjectFormItemContext();
    const disabledContext = useInjectDisabled();
    const mergedDisabled = computed(() => {
      var _a;
      return (_a = props.disabled) != null ? _a : disabledContext.value;
    });
    onBeforeMount(() => {
      devWarning(!("defaultChecked" in attrs), "Switch", "'defaultChecked' is deprecated, please use 'v-model:checked'");
      devWarning(!("value" in attrs), "Switch", "`value` is not validate prop, do you mean `checked`?");
    });
    const checked = ref(props.checked !== void 0 ? props.checked : attrs.defaultChecked);
    const checkedStatus = computed(() => checked.value === props.checkedValue);
    watch(() => props.checked, () => {
      checked.value = props.checked;
    });
    const {
      prefixCls,
      direction,
      size
    } = useConfigInject("switch", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    const refSwitchNode = 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
    });
    onMounted(() => {
      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.LEFT)
        setChecked(props.unCheckedValue, e);
      else if (e.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 = 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(createVNode(Wave, null, {
      default: () => {
        var _a;
        return [createVNode("button", {
          ...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
        }, [createVNode("div", {
          "class": `${prefixCls.value}-handle`
        }, [props.loading ? createVNode(LoadingOutlined, {
          "class": `${prefixCls.value}-loading-icon`
        }, null) : null]), createVNode("span", {
          "class": `${prefixCls.value}-inner`
        }, [createVNode("span", {
          "class": `${prefixCls.value}-inner-checked`
        }, [getPropsSlot(slots, props, "checkedChildren")]), createVNode("span", {
          "class": `${prefixCls.value}-inner-unchecked`
        }, [getPropsSlot(slots, props, "unCheckedChildren")])])])];
      }
    }));
  }
});

export { ASwitch as default };
//# sourceMappingURL=Switch.mjs.map
