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