"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const event = require("../../../constants/event.js");
const types = require("../../../utils/types.js");
const error = require("../../../utils/error.js");
const style = require("../../../utils/dom/style.js");
const index = require("../../../hooks/use-namespace/index.js");
const index$1 = require("../../icon/index.js");
const useFormCommonProps = require("../../form/src/hooks/use-form-common-props.js");
const useFormItem = require("../../form/src/hooks/use-form-item.js");
const _switch = require("./switch.js");
const index$2 = require("../../../../../@element-plus/icons-vue/dist/index.js");
const Vue = require("vue");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const _hoisted_1 = [
  "id",
  "aria-checked",
  "aria-disabled",
  "aria-label",
  "name",
  "true-value",
  "false-value",
  "disabled",
  "tabindex"
];
const _hoisted_2 = ["aria-hidden"];
const _hoisted_3 = { key: 1 };
const _hoisted_4 = { key: 1 };
const _hoisted_5 = ["aria-hidden"];
const COMPONENT_NAME = "ElSwitch";
var switch_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ Vue.defineComponent({
  name: COMPONENT_NAME,
  __name: "switch",
  props: _switch.switchProps,
  emits: _switch.switchEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { formItem } = useFormItem.useFormItem();
    const switchSize = useFormCommonProps.useFormSize();
    const ns = index.useNamespace("switch");
    const { inputId } = useFormItem.useFormItemInputId(props, { formItemContext: formItem });
    const switchDisabled = useFormCommonProps.useFormDisabled(Vue.computed(() => {
      if (props.loading) return true;
    }));
    const isControlled = Vue.ref(props.modelValue !== false);
    const input = Vue.shallowRef();
    const switchKls = Vue.computed(() => [
      ns.b(),
      ns.m(switchSize.value),
      ns.is("disabled", switchDisabled.value),
      ns.is("checked", checked.value)
    ]);
    const labelLeftKls = Vue.computed(() => [
      ns.e("label"),
      ns.em("label", "left"),
      ns.is("active", !checked.value)
    ]);
    const labelRightKls = Vue.computed(() => [
      ns.e("label"),
      ns.em("label", "right"),
      ns.is("active", checked.value)
    ]);
    const coreStyle = Vue.computed(() => ({ width: style.addUnit(props.width) }));
    Vue.watch(() => props.modelValue, () => {
      isControlled.value = true;
    });
    const actualValue = Vue.computed(() => {
      return isControlled.value ? props.modelValue : false;
    });
    const checked = Vue.computed(() => actualValue.value === props.activeValue);
    if (![props.activeValue, props.inactiveValue].includes(actualValue.value)) {
      emit(event.UPDATE_MODEL_EVENT, props.inactiveValue);
      emit(event.CHANGE_EVENT, props.inactiveValue);
      emit(event.INPUT_EVENT, props.inactiveValue);
    }
    Vue.watch(checked, (val) => {
      var _a;
      input.value.checked = val;
      if (props.validateEvent) (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, "change").catch((err) => error.debugWarn(err));
    });
    const handleChange = () => {
      const val = checked.value ? props.inactiveValue : props.activeValue;
      emit(event.UPDATE_MODEL_EVENT, val);
      emit(event.CHANGE_EVENT, val);
      emit(event.INPUT_EVENT, val);
      Vue.nextTick(() => {
        input.value.checked = checked.value;
      });
    };
    const switchValue = () => {
      if (switchDisabled.value) return;
      const { beforeChange } = props;
      if (!beforeChange) {
        handleChange();
        return;
      }
      const shouldChange = beforeChange();
      if (![shared_esmBundler.isPromise(shouldChange), types.isBoolean(shouldChange)].includes(true)) error.throwError(COMPONENT_NAME, "beforeChange must return type `Promise<boolean>` or `boolean`");
      if (shared_esmBundler.isPromise(shouldChange)) shouldChange.then((result) => {
        if (result) handleChange();
      }).catch((e) => {
        error.debugWarn(COMPONENT_NAME, `some error occurred: ${e}`);
      });
      else if (shouldChange) handleChange();
    };
    const focus = () => {
      var _a, _b;
      (_b = (_a = input.value) == null ? void 0 : _a.focus) == null ? void 0 : _b.call(_a);
    };
    Vue.onMounted(() => {
      input.value.checked = checked.value;
    });
    __expose({
      focus,
      checked
    });
    return (_ctx, _cache) => {
      return Vue.openBlock(), Vue.createElementBlock("div", {
        class: Vue.normalizeClass(switchKls.value),
        onClick: Vue.withModifiers(switchValue, ["prevent"])
      }, [
        Vue.createElementVNode("input", {
          id: Vue.unref(inputId),
          ref_key: "input",
          ref: input,
          class: Vue.normalizeClass(Vue.unref(ns).e("input")),
          type: "checkbox",
          role: "switch",
          "aria-checked": checked.value,
          "aria-disabled": Vue.unref(switchDisabled),
          "aria-label": __props.ariaLabel,
          name: __props.name,
          "true-value": __props.activeValue,
          "false-value": __props.inactiveValue,
          disabled: Vue.unref(switchDisabled),
          tabindex: __props.tabindex,
          onChange: handleChange,
          onKeydown: Vue.withKeys(switchValue, ["enter"])
        }, null, 42, _hoisted_1),
        !__props.inlinePrompt && (__props.inactiveIcon || __props.inactiveText || _ctx.$slots.inactive) ? (Vue.openBlock(), Vue.createElementBlock("span", {
          key: 0,
          class: Vue.normalizeClass(labelLeftKls.value)
        }, [Vue.renderSlot(_ctx.$slots, "inactive", {}, () => [__props.inactiveIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.inactiveIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true), !__props.inactiveIcon && __props.inactiveText ? (Vue.openBlock(), Vue.createElementBlock("span", {
          key: 1,
          "aria-hidden": checked.value
        }, Vue.toDisplayString(__props.inactiveText), 9, _hoisted_2)) : Vue.createCommentVNode("v-if", true)])], 2)) : Vue.createCommentVNode("v-if", true),
        Vue.createElementVNode("span", {
          class: Vue.normalizeClass(Vue.unref(ns).e("core")),
          style: Vue.normalizeStyle(coreStyle.value)
        }, [__props.inlinePrompt ? (Vue.openBlock(), Vue.createElementBlock("div", {
          key: 0,
          class: Vue.normalizeClass(Vue.unref(ns).e("inner"))
        }, [!checked.value ? (Vue.openBlock(), Vue.createElementBlock("div", {
          key: 0,
          class: Vue.normalizeClass(Vue.unref(ns).e("inner-wrapper"))
        }, [Vue.renderSlot(_ctx.$slots, "inactive", {}, () => [__props.inactiveIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.inactiveIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true), !__props.inactiveIcon && __props.inactiveText ? (Vue.openBlock(), Vue.createElementBlock("span", _hoisted_3, Vue.toDisplayString(__props.inactiveText), 1)) : Vue.createCommentVNode("v-if", true)])], 2)) : (Vue.openBlock(), Vue.createElementBlock("div", {
          key: 1,
          class: Vue.normalizeClass(Vue.unref(ns).e("inner-wrapper"))
        }, [Vue.renderSlot(_ctx.$slots, "active", {}, () => [__props.activeIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.activeIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true), !__props.activeIcon && __props.activeText ? (Vue.openBlock(), Vue.createElementBlock("span", _hoisted_4, Vue.toDisplayString(__props.activeText), 1)) : Vue.createCommentVNode("v-if", true)])], 2))], 2)) : Vue.createCommentVNode("v-if", true), Vue.createElementVNode("div", { class: Vue.normalizeClass(Vue.unref(ns).e("action")) }, [__props.loading ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), {
          key: 0,
          class: Vue.normalizeClass(Vue.unref(ns).is("loading"))
        }, {
          default: Vue.withCtx(() => [Vue.createVNode(Vue.unref(index$2.Loading))]),
          _: 1
        }, 8, ["class"])) : checked.value ? Vue.renderSlot(_ctx.$slots, "active-action", { key: 1 }, () => [__props.activeActionIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.activeActionIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true)]) : !checked.value ? Vue.renderSlot(_ctx.$slots, "inactive-action", { key: 2 }, () => [__props.inactiveActionIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.inactiveActionIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true)]) : Vue.createCommentVNode("v-if", true)], 2)], 6),
        !__props.inlinePrompt && (__props.activeIcon || __props.activeText || _ctx.$slots.active) ? (Vue.openBlock(), Vue.createElementBlock("span", {
          key: 1,
          class: Vue.normalizeClass(labelRightKls.value)
        }, [Vue.renderSlot(_ctx.$slots, "active", {}, () => [__props.activeIcon ? (Vue.openBlock(), Vue.createBlock(Vue.unref(index$1.ElIcon), { key: 0 }, {
          default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(__props.activeIcon)))]),
          _: 1
        })) : Vue.createCommentVNode("v-if", true), !__props.activeIcon && __props.activeText ? (Vue.openBlock(), Vue.createElementBlock("span", {
          key: 1,
          "aria-hidden": !checked.value
        }, Vue.toDisplayString(__props.activeText), 9, _hoisted_5)) : Vue.createCommentVNode("v-if", true)])], 2)) : Vue.createCommentVNode("v-if", true)
      ], 2);
    };
  }
});
exports.default = switch_vue_vue_type_script_setup_true_lang_default;
