import { ref as f, unref as p, defineComponent as C, computed as b, watchEffect as B, resolveComponent as d, openBlock as g, createBlock as $, mergeProps as _, withCtx as m, createTextVNode as I, toDisplayString as z, createSlots as D, createVNode as v, renderList as h, renderSlot as F, normalizeProps as k, guardReactiveProps as A } from "vue";
import { Button as N, Input as O } from "ant-design-vue";
import { tryOnUnmounted as P } from "@vueuse/core";
const T = {
  type: {
    type: String,
    default: "button"
  }
};
function j(t) {
  const n = f(t), e = f(!1);
  let o;
  function r() {
    o && window.clearInterval(o);
  }
  function a() {
    e.value = !1, r(), o = null;
  }
  function u() {
    p(e) || o || (e.value = !0, o = setInterval(() => {
      p(n) === 1 ? (a(), n.value = t) : n.value -= 1;
    }, 1e3));
  }
  function l() {
    n.value = t, a();
  }
  function s() {
    l(), u();
  }
  return P(() => {
    l();
  }), { start: u, reset: l, restart: s, clear: r, stop: a, currentCount: n, isStart: e };
}
const V = {
  value: { type: [Object, Number, String, Array] },
  count: { type: Number, default: 60 },
  beforeStartFunc: {
    type: Function,
    default: null
  }
}, E = C({
  name: "CountButton",
  components: { Button: N },
  props: V,
  setup(t) {
    const n = f(!1), { currentCount: e, isStart: o, start: r, reset: a } = j(t.count), u = (c) => typeof c == "function", l = b(() => p(o) ? `${p(e)}秒后重新获取` : "获取验证码");
    B(() => {
      t.value === void 0 && a();
    });
    async function s() {
      const { beforeStartFunc: c } = t;
      if (c && u(c)) {
        n.value = !0;
        try {
          await c() && r();
        } finally {
          n.value = !1;
        }
      } else
        r();
    }
    return { handleStart: s, currentCount: e, loading: n, getButtonText: l, isStart: o };
  }
}), w = (t, n) => {
  const e = t.__vccOpts || t;
  for (const [o, r] of n)
    e[o] = r;
  return e;
};
function G(t, n, e, o, r, a) {
  const u = d("Button");
  return g(), $(u, _(t.$attrs, {
    disabled: t.isStart,
    onClick: t.handleStart,
    loading: t.loading
  }), {
    default: m(() => [
      I(z(t.getButtonText), 1)
    ]),
    _: 1
  }, 16, ["disabled", "onClick", "loading"]);
}
const y = /* @__PURE__ */ w(E, [["render", G]]), L = {
  value: { type: String },
  size: {
    type: String,
    validator: (t) => ["default", "large", "small"].includes(t)
  },
  count: { type: Number, default: 60 },
  sendCodeApi: {
    type: Function,
    default: null
  }
}, R = C({
  name: "CountDownInput",
  components: { CountButton: y, Input: O },
  inheritAttrs: !1,
  props: L,
  setup(t) {
    return { prefixCls: "countdown-input" };
  }
});
function U(t, n, e, o, r, a) {
  const u = d("CountButton"), l = d("Input");
  return g(), $(l, _(t.$attrs, {
    class: t.prefixCls,
    size: t.size,
    value: t.value
  }), D({
    addonAfter: m(() => [
      v(u, {
        size: t.size,
        count: t.count,
        value: t.value,
        beforeStartFunc: t.sendCodeApi
      }, null, 8, ["size", "count", "value", "beforeStartFunc"])
    ]),
    _: 2
  }, [
    h(Object.keys(t.$slots).filter((s) => s !== "addonAfter"), (s) => ({
      name: s,
      fn: m((c) => [
        F(t.$slots, s, k(A(c || {})))
      ])
    }))
  ]), 1040, ["class", "size", "value"]);
}
const S = /* @__PURE__ */ w(R, [["render", U]]);
const q = "G", i = /* @__PURE__ */ C({
  name: `${q}CountDown`,
  props: T,
  emits: [],
  components: {
    CountButton: y,
    CountDownInput: S
  },
  setup(t, n) {
    return () => t.type === "button" ? v(y, t, null) : v(S, t, null);
  }
});
i.install = function(t, n) {
  t.component(n || i.name, i);
};
const M = {
  title: "CountDown 倒计时",
  category: "数据描述",
  status: "100%",
  install(t) {
    t.component(i.name, i);
  }
};
export {
  i as CountDown,
  M as default
};
