'use client';
import { jsx as t, jsxs as f, Fragment as $ } from "react/jsx-runtime";
import { useState as g, useMemo as W, useEffect as q, useCallback as T } from "react";
import { Button as G } from "@heroui/react";
import { CheckCircleIcon as H, XCircleIcon as J, ArrowPathIcon as K, EnvelopeIcon as O } from "@heroicons/react/24/outline";
import { useAuth as Q } from "../../../../hooks/use-auth.js";
import { useConfig as Z } from "../../../../hooks/use-config.js";
import { withErrorBoundary as _ } from "../../common/error-boundary.js";
import { ResendEmailButton as L } from "./resend-email-button.js";
import { VerificationError as z, VerificationInput as ee } from "../../common/verification.js";
function re({
  organizationId: E,
  email: u,
  autoSubmit: N = !0,
  codeLength: h = 6,
  resendDelay: p = 30,
  maxResendAttempts: C = 3,
  expirationTime: b = 300,
  // 5 minutes
  onVerificationSuccess: k,
  onVerificationError: o,
  onCodeSent: B,
  onCodeResent: D,
  token: V
}) {
  const { resendVerification: M, verifyIdentity: I, extractEmailFromUrl: c, extractTokenFromUrl: l } = Q(), [n, P] = g(""), [x, i] = g("idle"), [S, e] = g(null), [d, R] = g(0), [w, A] = g(0), [v, j] = g(null), m = W(() => V || l(), [V, l]), a = W(() => u || c(), [u, c]);
  q(() => {
    if (w > 0) {
      const r = setTimeout(() => A((s) => s - 1), 1e3);
      return () => clearTimeout(r);
    }
  }, [w]), q(() => {
    if (v) {
      const s = setInterval(() => {
        /* @__PURE__ */ new Date() > v && (i("expired"), e("Verification code has expired"));
      }, 1e3);
      return () => clearInterval(s);
    }
  }, [v]), q(() => {
    N && n.length === h && x === "sent" && y();
  }, [n, h, N, x]), q(() => {
    m && m !== "" && U();
  }, [n, h, N, x]);
  const U = T(async () => {
    if (!m) {
      i("error"), e("No verification token found.");
      return;
    }
    try {
      i("verifying"), e(null);
      const r = await I("email", {
        token: m
      });
      if (r.verified)
        i("verified"), k?.({
          email: a || "",
          verified: !0
        });
      else
        throw new Error(r.message ?? "Invalid verification code");
    } catch (r) {
      const s = r instanceof Error ? r : new Error("Verification failed");
      i("error"), e(s.message), o?.(s);
    }
  }, [m]), F = T(async () => {
    if (!a || !E) {
      e("Missing required parameters for email verification");
      return;
    }
    try {
      i("sending"), e(null);
      const r = await M({
        email: a,
        type: "email"
      });
      if (r.success)
        i("sent"), A(p), j(new Date(Date.now() + b * 1e3)), B?.();
      else
        throw new Error(r.message || "Failed to send verification code");
    } catch (r) {
      const s = r instanceof Error ? r : new Error("Failed to send verification code");
      i("error"), e(s.message), o?.(s);
    }
  }, [a, E, M, p, b, B, o]), Y = T(async () => {
    if (d >= C) {
      e("Maximum resend attempts reached");
      return;
    }
    try {
      R((r) => r + 1), await F(), D?.(d + 1);
    } catch (r) {
      const s = r instanceof Error ? r : new Error("Failed to resend verification code");
      o?.(s);
    }
  }, [d, C, F, D, o]), y = T(async () => {
    if (!n || n.length !== h) {
      e("Please enter a valid verification code");
      return;
    }
    if (!a || !E) {
      e("Missing required parameters for verification");
      return;
    }
    try {
      if (i("verifying"), e(null), (await I("email", {
        code: n,
        token: m ?? ""
      })).verified)
        i("verified"), k?.({
          email: a || "",
          verified: !0
        });
      else
        throw new Error("Invalid verification code");
    } catch (r) {
      const s = r instanceof Error ? r : new Error("Verification failed");
      i("error"), e(s.message), o?.(s);
    }
  }, [n, h, E, I, a, V, k, o]), X = T(() => {
    P(""), i("idle"), e(null), R(0), A(0), j(null);
  }, []);
  return {
    code: n,
    setCode: P,
    status: x,
    error: S,
    resendAttempts: d,
    timeRemaining: w,
    expiresAt: v,
    canResend: w === 0 && d < C,
    sendCode: F,
    resendCode: Y,
    handleVerify: y,
    reset: X
  };
}
const me = _(function({
  email: u,
  organizationId: N,
  autoSubmit: h = !0,
  codeLength: p = 6,
  resendDelay: C = 30,
  maxResendAttempts: b = 3,
  expirationTime: k = 300,
  onVerificationSuccess: o,
  onVerificationError: B,
  onCodeSent: D,
  onCodeResent: V,
  className: M,
  style: I,
  disabled: c = !1,
  size: l = "md",
  radius: n = "md"
}) {
  const { config: P, components: x } = Z(), {
    code: i,
    setCode: S,
    status: e,
    error: d,
    resendAttempts: R,
    timeRemaining: w,
    canResend: A,
    sendCode: v,
    resendCode: j,
    handleVerify: m
  } = re({
    email: u,
    organizationId: N,
    autoSubmit: h,
    codeLength: p,
    resendDelay: C,
    maxResendAttempts: b,
    expirationTime: k,
    onVerificationSuccess: o,
    onVerificationError: B,
    onCodeSent: D,
    onCodeResent: V
  }), a = x.Button ?? G, U = (y) => {
    y.length <= p && S(y);
  }, F = (y) => {
    y.preventDefault(), m();
  };
  return /* @__PURE__ */ t("div", { className: M, style: I, children: e === "verified" ? /* @__PURE__ */ f("div", { className: "text-center py-8", children: [
    /* @__PURE__ */ t(H, { className: "h-16 w-16 text-success mx-auto mb-4" }),
    /* @__PURE__ */ t("h3", { className: "text-lg font-semibold mb-2", children: "Email Verified!" }),
    /* @__PURE__ */ t("p", { className: "text-default-500", children: "Your email address has been successfully verified." })
  ] }) : e === "expired" ? /* @__PURE__ */ f("div", { className: "text-center py-8", children: [
    /* @__PURE__ */ t(J, { className: "h-16 w-16 text-warning mx-auto mb-4" }),
    /* @__PURE__ */ t("h3", { className: "text-lg font-semibold mb-2", children: "Code Expired" }),
    /* @__PURE__ */ t("p", { className: "text-default-500 mb-4", children: "The verification code has expired. Please request a new one." }),
    /* @__PURE__ */ t(
      a,
      {
        color: "primary",
        onClick: v,
        disabled: c,
        size: l,
        radius: n,
        startContent: /* @__PURE__ */ t(K, { className: "h-4 w-4" }),
        children: "Send New Code"
      }
    )
  ] }) : /* @__PURE__ */ f("form", { onSubmit: F, className: "space-y-4", children: [
    /* @__PURE__ */ f("div", { className: "text-center", children: [
      /* @__PURE__ */ t(O, { className: "h-12 w-12 text-primary mx-auto mb-4" }),
      /* @__PURE__ */ t("h3", { className: "text-lg font-semibold mb-2", children: "Verify Your Email" }),
      u && /* @__PURE__ */ f("p", { className: "text-default-500 mb-4", children: [
        "We've sent a verification code to ",
        u
      ] })
    ] }),
    d && /* @__PURE__ */ t(z, { error: d }),
    u && /* @__PURE__ */ f($, { children: [
      /* @__PURE__ */ f("div", { className: "space-y-4", children: [
        /* @__PURE__ */ t(
          ee,
          {
            value: i,
            onChange: U,
            length: p,
            disabled: c || e === "verifying",
            placeholder: "Enter verification code",
            size: l,
            radius: n
          }
        ),
        /* @__PURE__ */ t(
          a,
          {
            type: "submit",
            color: "primary",
            className: "w-full",
            size: l,
            radius: n,
            isLoading: e === "verifying",
            disabled: c || i.length !== p || e === "verifying",
            children: e === "verifying" ? "Verifying..." : "Verify Email"
          }
        ),
        /* @__PURE__ */ f("div", { className: "flex items-center justify-between", children: [
          /* @__PURE__ */ t("span", { className: "text-sm text-default-500", children: "Didn't receive the code?" }),
          /* @__PURE__ */ t(
            L,
            {
              onResend: j,
              disabled: c || !A,
              remainingTime: w,
              attempt: R,
              maxAttempts: b,
              size: l,
              radius: n
            }
          )
        ] })
      ] }),
      e === "idle" && /* @__PURE__ */ t(
        a,
        {
          color: "primary",
          variant: "flat",
          className: "w-full",
          size: l,
          radius: n,
          onClick: v,
          disabled: c,
          isLoading: e === "sending",
          children: e === "sending" ? "Sending..." : "Send Verification Code"
        }
      )
    ] })
  ] }) });
});
export {
  me as EmailVerification
};
//# sourceMappingURL=email-verification.js.map
