'use client';
import { jsx as e, jsxs as t, Fragment as G } from "react/jsx-runtime";
import ae, { useState as p, useCallback as D, useMemo as j, useEffect as ne } from "react";
import { Button as X, Divider as W, Link as q, Spinner as ie } from "@heroui/react";
import { motion as Z } from "framer-motion";
import { EnvelopeIcon as H, ArrowLeftIcon as U, ExclamationTriangleIcon as le, KeyIcon as J, CheckCircleIcon as oe } from "@heroicons/react/24/outline";
import { useAuth as V } from "../../../hooks/use-auth.js";
import { useConfig as z } from "../../../hooks/use-config.js";
import { useMagicLink as ee } from "../../../hooks/use-magic-link.js";
import R from "../../forms/form-wrapper.js";
import ce from "../../forms/email-field.js";
import Q from "../../forms/password-field.js";
const se = ae.memo(({
  password: n,
  requirements: i = {
    minLength: 8,
    requireUppercase: !0,
    requireLowercase: !0,
    requireNumbers: !0,
    requireSymbols: !1
  }
}) => {
  const d = j(() => {
    const s = [];
    return i.minLength && s.push({
      label: `At least ${i.minLength} characters`,
      passed: n.length >= i.minLength
    }), i.requireUppercase && s.push({
      label: "One uppercase letter",
      passed: /[A-Z]/.test(n)
    }), i.requireLowercase && s.push({
      label: "One lowercase letter",
      passed: /[a-z]/.test(n)
    }), i.requireNumbers && s.push({
      label: "One number",
      passed: /\d/.test(n)
    }), i.requireSymbols && s.push({
      label: "One special character",
      passed: /[!@#$%^&*(),.?":{}|<>]/.test(n)
    }), s;
  }, [n, i]), y = d.filter((s) => s.passed).length, o = Math.round(y / d.length * 100), N = () => o < 40 ? "bg-danger-500" : o < 70 ? "bg-warning-500" : "bg-success-500", b = () => o < 40 ? "Weak" : o < 70 ? "Medium" : "Strong";
  return /* @__PURE__ */ t("div", { className: "space-y-3", children: [
    /* @__PURE__ */ t("div", { className: "space-y-2", children: [
      /* @__PURE__ */ t("div", { className: "flex justify-between text-sm", children: [
        /* @__PURE__ */ e("span", { className: "text-default-600", children: "Password strength" }),
        /* @__PURE__ */ e("span", { className: `font-medium ${o < 40 ? "text-danger-600" : o < 70 ? "text-warning-600" : "text-success-600"}`, children: b() })
      ] }),
      /* @__PURE__ */ e("div", { className: "w-full bg-default-200 rounded-full h-2", children: /* @__PURE__ */ e(
        Z.div,
        {
          className: `h-2 rounded-full ${N()}`,
          initial: { width: 0 },
          animate: { width: `${o}%` },
          transition: { duration: 0.3 }
        }
      ) })
    ] }),
    /* @__PURE__ */ e("div", { className: "space-y-1", children: d.map((s, w) => /* @__PURE__ */ t("div", { className: "flex items-center gap-2 text-sm", children: [
      /* @__PURE__ */ e(
        oe,
        {
          className: `w-4 h-4 ${s.passed ? "text-success-600" : "text-default-300"}`
        }
      ),
      /* @__PURE__ */ e("span", { className: s.passed ? "text-success-700 dark:text-success-400" : "text-default-500", children: s.label })
    ] }, w)) })
  ] });
});
se.displayName = "PasswordStrengthIndicator";
function de({
  email: n = "",
  onSuccess: i,
  onError: d,
  title: y = "Forgot Password",
  subtitle: o = "Enter your email address and we'll send you a link to reset your password.",
  redirectUrl: N,
  variant: b = "default",
  size: s = "md",
  radius: w = "md",
  className: S = "",
  showBackLink: C = !0,
  organizationId: f
}) {
  const { isValidEmail: B } = ee(), { requestPasswordReset: A, isLoading: P } = V(), { components: L, linksPath: I } = z(), [m, r] = p(n), [E, x] = p(!1), [M, u] = p(null), F = L.Button ?? X, $ = D(async (k) => {
    if (k.preventDefault(), u(null), !m) {
      u("Please enter your email address");
      return;
    }
    if (!B(m)) {
      u("Please enter a valid email address");
      return;
    }
    try {
      const h = N || `${window.location.origin}/auth/reset-password`, v = await A({
        email: m,
        redirectUrl: h
        // organizationId,
      });
      v.success ? (x(!0), i?.(m)) : (u(v.error || "Failed to send reset link"), d?.(new Error(v.error || "Failed to send reset link")));
    } catch (h) {
      const v = h instanceof Error ? h.message : "Failed to send reset link";
      u(v), d?.(h instanceof Error ? h : new Error(v));
    }
  }, [m, B, A, N, f, i, d]), c = D(async () => {
    x(!1), u(null);
  }, []), g = j(() => ({
    size: s,
    variant: "flat",
    className: `space-y-6 ${S}`,
    title: y,
    subtitle: E ? void 0 : o,
    showCard: b === "card"
  }), [s, S, y, o, b, E]);
  return E ? /* @__PURE__ */ e(R, { ...g, children: /* @__PURE__ */ t("div", { className: "text-center space-y-4", children: [
    /* @__PURE__ */ e(
      Z.div,
      {
        initial: { scale: 0 },
        animate: { scale: 1 },
        className: "mx-auto w-16 h-16 rounded-full bg-success-100 dark:bg-success-900/30 flex items-center justify-center",
        children: /* @__PURE__ */ e(H, { className: "w-8 h-8 text-success-600" })
      }
    ),
    /* @__PURE__ */ t("div", { children: [
      /* @__PURE__ */ e("h3", { className: "text-xl font-semibold text-foreground mb-2", children: "Check Your Email" }),
      /* @__PURE__ */ t("p", { className: "text-default-500 text-sm", children: [
        "We've sent a password reset link to ",
        /* @__PURE__ */ e("strong", { children: m })
      ] })
    ] }),
    /* @__PURE__ */ t("div", { className: "space-y-3", children: [
      /* @__PURE__ */ e("p", { className: "text-sm text-default-400", children: "Didn't receive the email? Check your spam folder." }),
      /* @__PURE__ */ e(
        F,
        {
          variant: "light",
          size: "sm",
          onPress: c,
          children: "Send Another Email"
        }
      )
    ] }),
    C && /* @__PURE__ */ t(G, { children: [
      /* @__PURE__ */ e(W, { className: "my-4" }),
      /* @__PURE__ */ t(
        q,
        {
          href: I?.signIn || "/auth/sign-in",
          className: "flex items-center justify-center gap-2 text-sm",
          children: [
            /* @__PURE__ */ e(U, { className: "w-4 h-4" }),
            "Back to Sign In"
          ]
        }
      )
    ] })
  ] }) }) : /* @__PURE__ */ e(R, { ...g, onSubmit: $, children: /* @__PURE__ */ t("div", { className: "space-y-4", children: [
    /* @__PURE__ */ e(
      ce,
      {
        label: "Email Address",
        name: "email",
        placeholder: "Enter your email address",
        value: m,
        onChange: r,
        startContent: /* @__PURE__ */ e(H, { className: "w-4 h-4 text-default-400" }),
        size: s,
        radius: w,
        required: !0,
        disabled: P,
        variant: "bordered",
        autoFocus: !0
      }
    ),
    M && /* @__PURE__ */ e("div", { className: "text-danger-600 text-sm bg-danger-50 dark:bg-danger-900/20 rounded-lg p-3", children: M }),
    /* @__PURE__ */ e(
      F,
      {
        type: "submit",
        color: "primary",
        size: s,
        radius: w,
        className: "w-full",
        isLoading: P,
        isDisabled: !m || P,
        children: P ? "Sending..." : "Send Reset Link"
      }
    ),
    C && /* @__PURE__ */ t(G, { children: [
      /* @__PURE__ */ e(W, { className: "my-4" }),
      /* @__PURE__ */ e("div", { className: "text-center", children: /* @__PURE__ */ t(
        q,
        {
          href: I?.signIn || "/auth/sign-in",
          className: "flex items-center justify-center gap-2 text-sm",
          children: [
            /* @__PURE__ */ e(U, { className: "w-4 h-4" }),
            "Back to Sign In"
          ]
        }
      ) })
    ] })
  ] }) });
}
function ue({
  token: n,
  onSuccess: i,
  onError: d,
  title: y = "Reset Password",
  subtitle: o = "Enter your new password below.",
  redirectUrl: N = "/auth/sign-in",
  variant: b = "default",
  size: s = "md",
  radius: w = "md",
  className: S = "",
  autoVerify: C = !0,
  passwordRequirements: f = {
    minLength: 8,
    requireUppercase: !0,
    requireLowercase: !0,
    requireNumbers: !0,
    requireSymbols: !1
  }
}) {
  const { signIn: B, validateToken: A } = V(), { components: P, linksPath: L } = z(), { extractTokenFromUrl: I } = ee(), { resetPassword: m } = V(), [r, E] = p(""), [x, M] = p(""), [u, F] = p(!1), [$, c] = p(null), [g, k] = p("idle"), [h, v] = p(null), K = P.Button ?? X, T = j(() => n || I(), [n, I]);
  ne(() => {
    C && g === "idle" && te();
  }, [C, T, g]);
  const te = D(async () => {
    if (!T) {
      k("invalid"), c("No reset token found in URL");
      return;
    }
    try {
      k("verifying"), c(null);
      const a = await A({
        token: T,
        type: "password"
      });
      a.success ? (k("valid"), v(T)) : (k("invalid"), c(a.error || "Invalid or expired reset link"));
    } catch (a) {
      k("invalid");
      const l = a instanceof Error ? a.message : "Failed to verify reset token";
      c(l), d?.(a instanceof Error ? a : new Error(l));
    }
  }, [T, m, d]), _ = j(() => {
    const a = [];
    return f.minLength && a.push(r.length >= f.minLength), f.requireUppercase && a.push(/[A-Z]/.test(r)), f.requireLowercase && a.push(/[a-z]/.test(r)), f.requireNumbers && a.push(/\d/.test(r)), f.requireSymbols && a.push(/[!@#$%^&*(),.?":{}|<>]/.test(r)), a.every((l) => l);
  }, [r, f]), re = D(async (a) => {
    if (a.preventDefault(), c(null), !r || !x) {
      c("Please fill in all fields");
      return;
    }
    if (r !== x) {
      c("Passwords do not match");
      return;
    }
    if (!_) {
      c("Password does not meet requirements");
      return;
    }
    if (!h) {
      c("Invalid reset token");
      return;
    }
    try {
      F(!0);
      const l = await m({
        token: h,
        newPassword: r
      });
      l.status === "complete" && l.user ? (i?.(l), setTimeout(() => {
        window.location.href = N;
      }, 2e3)) : c(l.error?.message || "Failed to reset password");
    } catch (l) {
      const Y = l instanceof Error ? l.message : "Failed to reset password";
      c(Y), d?.(l instanceof Error ? l : new Error(Y));
    } finally {
      F(!1);
    }
  }, [r, x, _, h, B, i, d, N]), O = j(() => ({
    size: s,
    variant: "flat",
    className: `space-y-6 ${S}`,
    title: y,
    subtitle: g === "valid" ? o : void 0,
    showCard: b === "card"
  }), [s, S, y, o, b, g]);
  return g === "verifying" ? /* @__PURE__ */ e(R, { ...O, children: /* @__PURE__ */ t("div", { className: "text-center space-y-4", children: [
    /* @__PURE__ */ e(ie, { size: "lg" }),
    /* @__PURE__ */ t("div", { children: [
      /* @__PURE__ */ e("h3", { className: "text-xl font-semibold text-foreground mb-2", children: "Verifying Reset Link" }),
      /* @__PURE__ */ e("p", { className: "text-default-500 text-sm", children: "Please wait while we verify your reset link..." })
    ] })
  ] }) }) : g === "invalid" ? /* @__PURE__ */ e(R, { ...O, children: /* @__PURE__ */ t("div", { className: "text-center space-y-4", children: [
    /* @__PURE__ */ e(
      Z.div,
      {
        initial: { scale: 0 },
        animate: { scale: 1 },
        className: "mx-auto w-16 h-16 rounded-full bg-danger-100 dark:bg-danger-900/30 flex items-center justify-center",
        children: /* @__PURE__ */ e(le, { className: "w-8 h-8 text-danger-600" })
      }
    ),
    /* @__PURE__ */ t("div", { children: [
      /* @__PURE__ */ e("h3", { className: "text-xl font-semibold text-foreground mb-2", children: "Invalid Reset Link" }),
      /* @__PURE__ */ e("p", { className: "text-default-500 text-sm", children: $ || "This password reset link is invalid or has expired." })
    ] }),
    /* @__PURE__ */ t("div", { className: "space-y-3", children: [
      /* @__PURE__ */ e(
        K,
        {
          as: q,
          href: L?.forgotPassword || "/auth/forgot-password",
          color: "primary",
          children: "Request New Reset Link"
        }
      ),
      /* @__PURE__ */ e("div", { className: "text-center", children: /* @__PURE__ */ t(
        q,
        {
          href: L?.signIn || "/auth/sign-in",
          className: "text-sm flex items-center justify-center gap-2",
          children: [
            /* @__PURE__ */ e(U, { className: "w-4 h-4" }),
            "Back to Sign In"
          ]
        }
      ) })
    ] })
  ] }) }) : /* @__PURE__ */ e(R, { ...O, onSubmit: re, children: /* @__PURE__ */ t("div", { className: "space-y-4", children: [
    /* @__PURE__ */ e(
      Q,
      {
        label: "New Password",
        name: "password",
        placeholder: "Enter your new password",
        value: r,
        onChange: E,
        startContent: /* @__PURE__ */ e(J, { className: "w-4 h-4 text-default-400" }),
        size: s,
        radius: w,
        required: !0,
        disabled: u,
        variant: "bordered",
        autoFocus: !0
      }
    ),
    r && /* @__PURE__ */ e(
      se,
      {
        password: r,
        requirements: f
      }
    ),
    /* @__PURE__ */ e(
      Q,
      {
        label: "Confirm Password",
        name: "confirmPassword",
        placeholder: "Confirm your new password",
        value: x,
        onChange: M,
        startContent: /* @__PURE__ */ e(J, { className: "w-4 h-4 text-default-400" }),
        size: s,
        radius: w,
        required: !0,
        disabled: u,
        variant: "bordered"
      }
    ),
    $ && /* @__PURE__ */ e("div", { className: "text-danger-600 text-sm bg-danger-50 dark:bg-danger-900/20 rounded-lg p-3", children: $ }),
    /* @__PURE__ */ e(
      K,
      {
        type: "submit",
        color: "primary",
        size: s,
        radius: w,
        className: "w-full",
        isLoading: u,
        isDisabled: !r || !x || !_ || u,
        children: u ? "Resetting..." : "Reset Password"
      }
    ),
    /* @__PURE__ */ e(W, { className: "my-4" }),
    /* @__PURE__ */ e("div", { className: "text-center", children: /* @__PURE__ */ t(
      q,
      {
        href: L?.signIn || "/auth/sign-in",
        className: "text-sm flex items-center justify-center gap-2",
        children: [
          /* @__PURE__ */ e(U, { className: "w-4 h-4" }),
          "Back to Sign In"
        ]
      }
    ) })
  ] }) });
}
function ke(n) {
  return /* @__PURE__ */ e(de, { ...n, variant: "card" });
}
function Pe(n) {
  return /* @__PURE__ */ e(ue, { ...n, variant: "card" });
}
export {
  de as ForgotPassword,
  ke as ForgotPasswordCard,
  ue as ResetPassword,
  Pe as ResetPasswordCard,
  de as default
};
//# sourceMappingURL=forgot-password.js.map
