'use client';
import { jsxs as c, jsx as e } from "react/jsx-runtime";
import q, { useState as P, useMemo as I, useEffect as G, useCallback as T } from "react";
import { Button as R, Divider as H, Link as J } from "@heroui/react";
import { motion as K } from "framer-motion";
import { ArrowPathIcon as Y, EnvelopeIcon as p, ExclamationTriangleIcon as z, CheckCircleIcon as Q } from "@heroicons/react/24/outline";
import { useMagicLink as X } from "../../../hooks/use-magic-link.js";
import { useAuth as Z } from "../../../hooks/use-auth.js";
import { useConfig as j } from "../../../hooks/use-config.js";
import B from "../../forms/form-wrapper.js";
const A = q.memo(({
  status: t,
  type: o,
  error: w,
  onRetry: m,
  onResend: a,
  size: l = "md",
  radius: r = "md"
}) => {
  const { components: v } = j(), n = v.Button ?? R, k = () => {
    switch (t) {
      case "verifying":
        return {
          icon: /* @__PURE__ */ e(Y, { className: "w-8 h-8 text-primary-600 animate-spin" }),
          title: "Verifying...",
          subtitle: "Please wait while we verify your magic link.",
          bgColor: "bg-primary-100 dark:bg-primary-900/30"
        };
      case "success":
        return {
          icon: /* @__PURE__ */ e(Q, { className: "w-8 h-8 text-success-600" }),
          title: "Verification Successful!",
          subtitle: x(o),
          bgColor: "bg-success-100 dark:bg-success-900/30"
        };
      case "error":
        return {
          icon: /* @__PURE__ */ e(z, { className: "w-8 h-8 text-danger-600" }),
          title: "Verification Failed",
          subtitle: w || "The magic link is invalid or has expired.",
          bgColor: "bg-danger-100 dark:bg-danger-900/30"
        };
      case "expired":
        return {
          icon: /* @__PURE__ */ e(z, { className: "w-8 h-8 text-warning-600" }),
          title: "Link Expired",
          subtitle: "This magic link has expired. Please request a new one.",
          bgColor: "bg-warning-100 dark:bg-warning-900/30"
        };
      default:
        return {
          icon: /* @__PURE__ */ e(p, { className: "w-8 h-8 text-default-500" }),
          title: "Magic Link",
          subtitle: "Click the link in your email to continue.",
          bgColor: "bg-default-100 dark:bg-default-900/30"
        };
    }
  }, x = (b) => {
    switch (b) {
      case "sign-in":
        return "You have been successfully signed in.";
      case "sign-up":
        return "Your account has been created and verified.";
      case "email-verification":
        return "Your email address has been verified.";
      case "password-reset":
        return "You can now reset your password.";
      case "organization-invite":
        return "Welcome to the organization!";
      default:
        return "Verification completed successfully.";
    }
  }, d = k();
  return /* @__PURE__ */ c("div", { className: "text-center space-y-4", children: [
    /* @__PURE__ */ e(
      K.div,
      {
        initial: { scale: 0 },
        animate: { scale: 1 },
        className: `mx-auto w-16 h-16 rounded-full flex items-center justify-center ${d.bgColor}`,
        children: d.icon
      }
    ),
    /* @__PURE__ */ c("div", { children: [
      /* @__PURE__ */ e("h3", { className: "text-xl font-semibold text-foreground mb-2", children: d.title }),
      /* @__PURE__ */ e("p", { className: "text-default-500 text-sm", children: d.subtitle })
    ] }),
    t === "error" && /* @__PURE__ */ c("div", { className: "space-y-2", children: [
      m && /* @__PURE__ */ e(
        n,
        {
          variant: "bordered",
          size: l,
          radius: r,
          onPress: m,
          children: "Try Again"
        }
      ),
      a && /* @__PURE__ */ e(
        n,
        {
          variant: "light",
          size: l,
          radius: r,
          onPress: a,
          startContent: /* @__PURE__ */ e(p, { className: "w-4 h-4" }),
          children: "Resend Magic Link"
        }
      )
    ] }),
    t === "expired" && a && /* @__PURE__ */ e(
      n,
      {
        color: "primary",
        onPress: a,
        size: l,
        radius: r,
        startContent: /* @__PURE__ */ e(p, { className: "w-4 h-4" }),
        children: "Send New Magic Link"
      }
    )
  ] });
});
A.displayName = "VerificationStatus";
function F({
  token: t,
  type: o = "sign-in",
  onSuccess: w,
  onError: m,
  title: a,
  subtitle: l,
  redirectUrl: r,
  showResend: v = !0,
  email: n,
  variant: k = "default",
  size: x = "md",
  className: d = "",
  autoVerify: b = !0
}) {
  const {
    verifyMagicLink: y,
    verifyFromUrl: U,
    sendMagicLink: N,
    extractTokenFromUrl: C,
    isLoading: $,
    error: ee
  } = X(), { components: W, linksPath: D } = j(), { reload: V } = Z(), O = W.Button ?? R, [u, f] = P("idle"), [_, g] = P(null), s = I(() => t || C(), [t, C]);
  G(() => {
    b && s && u === "idle" && L();
  }, [b, s, u]);
  const L = T(async () => {
    if (!s) {
      f("error"), g("No verification token found.");
      return;
    }
    try {
      f("verifying"), g(null);
      const i = await y(s);
      if (i.success) {
        if (f("success"), await V(), w?.(i), r)
          setTimeout(() => {
            window.location.href = r;
          }, 2e3);
        else if (i.user) {
          const E = {
            "sign-in": "/dashboard",
            "sign-up": "/welcome",
            "email-verification": "/dashboard",
            "password-reset": "/auth/reset-password",
            "organization-invite": "/organization/welcome"
          }[o];
          E && setTimeout(() => {
            window.location.href = E;
          }, 2e3);
        }
      } else
        i.error?.includes("expired") ? f("expired") : f("error"), g(i.error || "Verification failed"), m?.(new Error(i.error || "Verification failed"));
    } catch (i) {
      f("error");
      const h = i instanceof Error ? i.message : "Verification failed";
      g(h), m?.(i instanceof Error ? i : new Error(h));
    }
  }, [s, y, V, w, m, r, o]), M = T(async () => {
    if (!n) {
      console.warn("Cannot resend magic link: no email provided");
      return;
    }
    try {
      await N(n, {
        redirectUrl: r || window.location.href
      }), f("idle"), g(null);
    } catch (i) {
      console.error("Failed to resend magic link:", i);
    }
  }, [n, N, r]), S = I(() => {
    const i = () => {
      if (a) return a;
      switch (o) {
        case "sign-in":
          return "Magic Link Sign In";
        case "sign-up":
          return "Verify Your Account";
        case "email-verification":
          return "Verify Email Address";
        case "password-reset":
          return "Reset Password Link";
        case "organization-invite":
          return "Organization Invitation";
        default:
          return "Magic Link Verification";
      }
    }, h = () => l || (u === "idle" && !s ? "Click the magic link in your email to continue." : "Verifying your magic link...");
    return {
      size: x,
      variant: "flat",
      className: `space-y-6 ${d}`,
      title: i(),
      subtitle: h(),
      showCard: k === "card"
    };
  }, [a, l, o, x, d, k, u, s]);
  return s || u !== "idle" ? /* @__PURE__ */ c(B, { ...S, children: [
    /* @__PURE__ */ e(
      A,
      {
        status: u,
        type: o,
        error: _,
        onRetry: L,
        onResend: v && n ? M : void 0
      }
    ),
    u === "success" && r && /* @__PURE__ */ e("div", { className: "text-center", children: /* @__PURE__ */ c("div", { className: "flex items-center justify-center gap-2 text-sm text-default-500", children: [
      /* @__PURE__ */ e(Y, { className: "w-4 h-4 animate-spin" }),
      /* @__PURE__ */ e("span", { children: "Redirecting..." })
    ] }) })
  ] }) : /* @__PURE__ */ e(B, { ...S, children: /* @__PURE__ */ c("div", { className: "text-center space-y-4", children: [
    /* @__PURE__ */ e("div", { className: "mx-auto w-16 h-16 rounded-full bg-default-100 dark:bg-default-900/30 flex items-center justify-center", children: /* @__PURE__ */ e(p, { className: "w-8 h-8 text-default-500" }) }),
    /* @__PURE__ */ c("div", { children: [
      /* @__PURE__ */ e("h3", { className: "text-xl font-semibold text-foreground mb-2", children: "Check Your Email" }),
      /* @__PURE__ */ e("p", { className: "text-default-500 text-sm", children: "We've sent a magic link to your email address. Click the link to continue." })
    ] }),
    v && n && /* @__PURE__ */ c("div", { className: "space-y-2", children: [
      /* @__PURE__ */ e("p", { className: "text-sm text-default-400", children: "Didn't receive the email?" }),
      /* @__PURE__ */ e(
        O,
        {
          variant: "light",
          size: "sm",
          onPress: M,
          isLoading: $,
          startContent: /* @__PURE__ */ e(p, { className: "w-4 h-4" }),
          children: "Resend Magic Link"
        }
      )
    ] }),
    /* @__PURE__ */ e(H, { className: "my-4" }),
    /* @__PURE__ */ e("div", { className: "text-center", children: /* @__PURE__ */ e(J, { href: D?.signIn || "/auth/sign-in", size: "sm", children: "Back to Sign In" }) })
  ] }) });
}
function de(t) {
  return /* @__PURE__ */ e(F, { ...t, variant: "card" });
}
function ue(t) {
  return /* @__PURE__ */ e(F, { ...t, type: "organization-invite" });
}
export {
  F as MagicLinkVerify,
  de as MagicLinkVerifyCard,
  ue as OrganizationInviteVerification
};
//# sourceMappingURL=magic-link.js.map
