import { jsx as d } from "react/jsx-runtime";
import { useRef as g, useState as w, isValidElement as r, useImperativeHandle as y } from "react";
import { useMemoizedFn as n, useReactive as v } from "ahooks";
import l from "lodash/noop";
import H from "./confirm.js";
function R({ ref: f, ...i }) {
  const [s, e] = w(""), [B, c] = w(""), t = v({
    show: !1,
    title: "",
    content: "",
    onConfirm: l,
    onCancel: l,
    // 其他属性
    loading: !1,
    showCancelButton: !0,
    showCloseButton: !0,
    showConfirmButton: !0,
    confirmButtonText: "Confirm",
    confirmButtonProps: {},
    cancelButtonText: "Cancel",
    cancelButtonProps: {}
  }), m = n((o = {}) => {
    r(o.title) ? (e(o.title), t.title = "") : (e(""), t.title = o.title), r(o.content) ? (c(o.content), t.content = "") : (c(""), t.content = o.content), t.onConfirm = o.onConfirm || l, t.onCancel = o.onCancel || l, t.showCloseButton = o.showCloseButton ?? !0, t.showCancelButton = o.showCancelButton ?? !0, t.showConfirmButton = o.showConfirmButton ?? !0, o.confirmButtonText && (t.confirmButtonText = o.confirmButtonText), o.confirmButtonProps && (t.confirmButtonProps = o.confirmButtonProps), o.cancelButtonText && (t.cancelButtonText = o.cancelButtonText), o.cancelButtonProps && (t.cancelButtonProps = o.cancelButtonProps), t.loading = !1, t.show = !0;
  }), x = n(() => {
    e(""), c(""), t.title = "", t.content = "", t.onConfirm = l, t.onCancel = l, t.confirmButtonText = "Confirm", t.confirmButtonProps = {}, t.cancelButtonText = "Cancel", t.cancelButtonProps = {}, t.showConfirmButton = !0, t.showCancelButton = !0, t.showCloseButton = !0;
  }), u = n(() => {
    t.show = !1, setTimeout(() => {
      x();
    }, 300);
  }), h = n((o) => {
    o.title && (r(o.title) ? (e(o.title), t.title = "") : (e(""), t.title = o.title)), o.content && (r(o.content) ? (c(o.content), t.content = "") : (c(""), t.content = o.content));
  }), T = n((o, p) => {
    u(), t?.onCancel(o, p);
  }, []), a = n(async (o) => {
    t.loading = !0;
    try {
      await t?.onConfirm(u, o);
    } finally {
      t.loading = !1;
    }
  }, []);
  y(f, () => ({
    open: m,
    close: u,
    update: h
  }), [m, u, h]);
  const P = s || t.title, C = B || t.content;
  return /* @__PURE__ */ d(
    H,
    {
      ...i,
      open: t.show,
      title: P,
      onConfirm: a,
      onCancel: T,
      confirmButton: {
        text: t.confirmButtonText,
        props: {
          variant: "contained",
          color: "primary",
          loading: t.loading,
          ...t.confirmButtonProps
        }
      },
      showCloseButton: t.showCloseButton,
      showCancelButton: t.showCancelButton,
      showConfirmButton: t.showConfirmButton,
      cancelButton: {
        text: t.cancelButtonText,
        props: {
          variant: "outlined",
          color: "primary",
          ...t.cancelButtonProps
        }
      },
      children: C instanceof Function ? C() : C
    }
  );
}
function I(f = {}) {
  const i = g(null), s = n((...t) => {
    i.current?.open(...t);
  }), e = n((...t) => {
    i.current?.close(...t);
  }), B = n((...t) => {
    i.current?.update(...t);
  }), c = {
    open: s,
    close: e,
    update: B
  };
  return {
    confirmHolder: /* @__PURE__ */ d(R, { ...f, ref: i }),
    confirmApi: c
  };
}
export {
  I as default
};
