"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("react"),U=require("./use-auth.cjs"),p=require("../provider/config-provider.cjs"),g={DEFAULT_EXPIRES_IN:15*60,RESEND_COOLDOWN:60,EMAIL_REGEX:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,TEMPLATES:{SIGN_IN:"magic-link-sign-in",SIGN_UP:"magic-link-sign-up",VERIFY_EMAIL:"magic-link-verify-email",PASSWORD_RESET:"magic-link-password-reset"},URL_PARAMS:{TOKEN:"token",EMAIL:"email",TYPE:"type",REDIRECT:"redirect_to"}};function h(){const{activeOrganization:a,reload:t,userType:S,sdk:i}=U.useAuth(),{apiUrl:R,publishableKey:l,features:c,linksPath:u,frontendUrl:m}=p.useConfig(),[f,n]=r.useState(!1),[o,k]=r.useState(null),[w,E]=r.useState(null),[d,_]=r.useState(null),I=r.useMemo(()=>c.magicLink,[c.magicLink]),L=r.useMemo(()=>d?(Date.now()-d.getTime())/1e3>=g.RESEND_COOLDOWN:!1,[d]),y=r.useMemo(()=>{if(!d||L)return 0;const e=(Date.now()-d.getTime())/1e3;return Math.max(0,g.RESEND_COOLDOWN-e)},[d,L]);r.useEffect(()=>{if(!d||L)return;const e=setInterval(()=>{},1e3);return()=>clearInterval(e)},[d,L]),r.useCallback(e=>{const s={code:e.code||"UNKNOWN_ERROR",message:e.message||"An unknown error occurred",details:e.details,field:e.field};throw k(s),s},[]);const A=r.useCallback(e=>g.EMAIL_REGEX.test(e),[]),v=r.useCallback(e=>{const s=e||window.location.href;try{return new URL(s).searchParams.get(g.URL_PARAMS.TOKEN)}catch{return null}},[]),T=r.useCallback(async(e,s)=>{if(!I)throw new Error("Magic links not available");if(!A(e))throw new Error("Invalid email address");try{n(!0),k(null);const M={email:e,redirectUrl:s?.redirectUrl||`${m??window.location.origin}${u?.magicLink}`,expiresIn:s?.expiresIn||g.DEFAULT_EXPIRES_IN},b=await i.auth.sendMagicLink(M);return E(e),_(new Date),{success:b.success,email:e,message:b.message,expiresAt:new Date(Date.now()+M.expiresIn*1e3)}}catch(M){return{success:!1,email:e,expiresAt:new Date,error:M.message||"Failed to send magic link"}}finally{n(!1)}},[i.auth,I,A,a]),N=r.useCallback(async e=>{if(!I)throw new Error("Magic links not available");if(!e)throw new Error("Verification token is required");try{n(!0),k(null);const s=await i.auth.verifyMagicLink(e);return s.session?(await t(),{success:!0,user:s.user,session:s.session,requiresAdditionalVerification:s.mfaRequired,mfaToken:s.mfaToken}):{success:!1,error:s.error||"Magic link verification failed"}}catch(s){return{success:!1,error:s.message||"Magic link verification failed"}}finally{n(!1)}},[i.auth,I,t]),C=r.useCallback(async e=>{const s=v(e);return s?N(s):{success:!1,error:"No verification token found in URL"}},[v,N]),O=r.useCallback(async()=>{if(!w)throw new Error("No previous magic link to resend");if(!L)throw new Error(`Please wait ${Math.ceil(y)} seconds before resending`);return T(w)},[w,L,y,T]),P=r.useCallback(()=>{k(null),E(null),_(null)},[]);return{isLoading:f,error:o,lastSentEmail:w,lastSentAt:d,canResend:L,timeUntilResend:y,sendMagicLink:T,verifyMagicLink:N,resendMagicLink:O,verifyFromUrl:C,extractTokenFromUrl:v,isValidEmail:A,clearState:P}}function D(){const{sendMagicLink:a,isLoading:t,error:S,lastSentEmail:i,canResend:R,resendMagicLink:l,isValidEmail:c,clearState:u}=h(),[m,f]=r.useState("idle"),n=r.useCallback(async(k,w)=>{if(!c(k))throw new Error("Please enter a valid email address");try{const E=await a(k,{redirectUrl:w||"/dashboard",template:g.TEMPLATES.SIGN_IN});return E.success&&f("email_sent"),E}catch(E){throw f("idle"),E}},[a,c]),o=r.useCallback(()=>{f("idle"),u()},[u]);return{signIn:n,resend:l,reset:o,state:m,sentTo:i,canResend:R,isLoading:t,error:S,isValidEmail:c}}function V(){const{verifyFromUrl:a,verifyMagicLink:t,extractTokenFromUrl:S,isLoading:i,error:R}=h(),[l,c]=r.useState("idle"),[u,m]=r.useState(null);r.useEffect(()=>{const n=S();n&&l==="idle"&&f(n)},[S,l]);const f=r.useCallback(async n=>{try{c("verifying");const o=n?await t(n):await a();return m(o),c(o.success?"success":"error"),o}catch(o){throw c("error"),m({success:!1,error:o.message}),o}},[t,a]);return{verify:f,state:l,result:u,isVerifying:l==="verifying"||i,isSuccess:l==="success",isError:l==="error",error:R||u?.error,requiresMFA:u?.requiresAdditionalVerification,mfaToken:u?.mfaToken}}function F(){const{sendMagicLink:a,isValidEmail:t}=h();return{sendResetLink:r.useCallback(async i=>{if(!t(i))throw new Error("Please enter a valid email address");return a(i,{template:g.TEMPLATES.PASSWORD_RESET,redirectUrl:`${window.location.origin}/auth/reset-password`})},[a,t]),isValidEmail:t}}exports.MAGIC_LINK_CONFIG=g;exports.useMagicLink=h;exports.useMagicLinkPasswordReset=F;exports.useMagicLinkSignIn=D;exports.useMagicLinkVerification=V;
//# sourceMappingURL=use-magic-link.cjs.map
