import { defineComponent as M, useSlots as U, computed as C, ref as w, unref as o, createVNode as s, Fragment as f, openBlock as h, createBlock as p, resolveDynamicComponent as E, createElementBlock as K, mergeProps as y, createSlots as x, renderList as B, withCtx as v, renderSlot as P, normalizeProps as A, guardReactiveProps as S } from "vue";
import { Switch as F, Popconfirm as L } from "@aplus-frontend/antdv";
import { omit as _, isNil as j, isFunction as q } from "lodash-unified";
import "../../utils/index.mjs";
import "../../hooks/index.mjs";
import { useControllableValue as G } from "../../hooks/useControllableValue.mjs";
import { isDef as d } from "../../utils/is.mjs";
const Y = /* @__PURE__ */ M({
  name: "ApFieldSwitch",
  __name: "index",
  props: {
    mode: {},
    class: {},
    style: {},
    emptyText: {
      default: "--"
    },
    customReadMode: {},
    id: {},
    prefixCls: {},
    size: {
      default: "default"
    },
    disabled: {
      type: Boolean,
      default: void 0
    },
    checkedChildren: {},
    unCheckedChildren: {},
    tabindex: {},
    autofocus: {
      type: Boolean
    },
    loading: {
      type: Boolean,
      default: void 0
    },
    checked: {},
    checkedValue: {
      default: !0
    },
    unCheckedValue: {
      default: !1
    },
    onChange: {},
    onKeydown: {},
    onMouseup: {},
    "onUpdate:checked": {},
    onBlur: {},
    onFocus: {},
    onAction: {},
    confirm: {}
  },
  emits: ["update:checked"],
  setup(t, {
    expose: N,
    emit: R
  }) {
    const i = U(), e = t, g = C(() => _(e, ["onUpdate:checked", "emptyText"])), $ = R, m = w(), {
      value: c,
      updateValue: a
    } = G(e, $, {
      valuePropName: "checked"
    }), l = w(!1);
    async function V(n, r) {
      if (!d(e.confirm)) {
        if (!d(e.onAction)) {
          a(n);
          return;
        }
        l.value = !0;
        try {
          await e.onAction(n, r) && a(n);
        } finally {
          l.value = !1;
        }
      }
    }
    async function T() {
      const n = o(c) === e.checkedValue ? e.unCheckedValue : e.checkedValue;
      if (!d(e.onAction)) {
        a(n);
        return;
      }
      await e.onAction(n) && a(n);
    }
    const z = C(() => {
      const n = o(c);
      return j(n) ? s(f, null, [e.emptyText]) : n === e.checkedValue ? s(f, null, [e.checkedChildren || i.checkedChildren?.() || `${e.checkedValue}`]) : s(f, null, [e.unCheckedChildren || i.unCheckedChildren?.() || `${e.unCheckedValue}`]);
    }), D = C(() => d(e.confirm) ? q(e.confirm) ? e.confirm(o(c)) : e.confirm : {});
    return N({
      focus: () => m.value?.focus(),
      blur: () => m.value?.blur()
    }), (n, r) => t.mode === "read" ? (h(), p(E(z.value), {
      key: 0
    })) : (h(), K(f, {
      key: 1
    }, [!o(d)(t.confirm) || t.disabled ? (h(), p(o(F), y({
      key: 0,
      ref_key: "switchRef",
      ref: m
    }, g.value, {
      checked: o(c),
      loading: t.loading ?? l.value,
      onClick: V
    }), x({
      _: 2
    }, [B(i, (b, u) => ({
      name: u,
      fn: v((k) => [P(n.$slots, u, A(S(k || {})))])
    }))]), 1040, ["checked", "loading"])) : (h(), p(o(L), y({
      key: 1,
      disabled: l.value
    }, D.value, {
      onConfirm: T
    }), {
      default: v(() => [s(o(F), y(g.value, {
        checked: o(c),
        loading: t.loading ?? l.value,
        onClick: V
      }), x({
        _: 2
      }, [B(i, (b, u) => ({
        name: u,
        fn: v((k) => [P(n.$slots, u, A(S(k || {})))])
      }))]), 1040, ["checked", "loading"])]),
      _: 3
    }, 16, ["disabled"]))], 64));
  }
});
export {
  Y as default
};
