1import{E,b as C,b3 as L,r as d,w as p,y as P,W as _,B as U,b4 as R,b5 as T,s as m,a3 as q}from"./main-DX7XzkoU.js";import{s as S}from"./subscriberService-DJIuIlbz.js";const f=(i,a)=>{const l=i==null?void 0:i.code,h=(i==null?void 0:i.message)||"",t=i==null?void 0:i.status;switch(l){case"invalid_credentials":case"invalid_login_credentials":return"auth.errors.invalid_credentials";case"user_already_registered":return"auth.errors.user_already_registered";case"weak_password":return"auth.errors.weak_password";case"invalid_email":return"auth.errors.invalid_email";case"signup_disabled":return"auth.errors.signup_disabled";case"email_not_confirmed":return"auth.errors.email_not_confirmed";case"too_many_requests":return"auth.errors.too_many_requests";case"oauth_provider_error":case"oauth_error":return"auth.errors.oauth_error";case"popup_closed_by_user":return"auth.errors.popup_blocked";case"user_banned":return"auth.errors.user_banned";case"over_email_send_rate_limit":return"auth.errors.too_many_requests";case"session_not_found":case"invalid_session":return"auth.errors.invalid_credentials";default:{const e=h.toLowerCase();if(e.includes("invalid login credentials")||e.includes("invalid credentials"))return"auth.errors.invalid_credentials";if(e.includes("user is banned"))return"auth.errors.user_banned";if(e.includes("user already registered")||e.includes("email address is already registered"))return"auth.errors.user_already_registered";if(e.includes("password should be at least")||e.includes("password is too short"))return"auth.errors.weak_password";if(e.includes("invalid email")||e.includes("email is invalid"))return"auth.errors.invalid_email";if(e.includes("signup is disabled")||e.includes("registration is disabled"))return"auth.errors.signup_disabled";if(e.includes("email not confirmed")||e.includes("email address not confirmed"))return"auth.errors.email_not_confirmed";if(e.includes("too many requests")||e.includes("rate limit")||t===429)return"auth.errors.too_many_requests";if(e.includes("oauth")||e.includes("authorization"))return"auth.errors.oauth_error";if(e.includes("popup")||e.includes("window closed"))return"auth.errors.popup_blocked";if(e.includes("network")||e.includes("fetch")||t===0)return"errors.network_error";if(t&&t>=500)return"errors.server_error";switch(a){case"signup":return"auth.errors.generic_signup";case"signin":return"auth.errors.generic_signin";case"google":return"auth.errors.generic_google";default:return"errors.something_wrong"}}}},D=()=>{const{toast:i}=E(),{t:a}=C(),{redirectAfterLogin:l}=L(),[h,t]=d.useState({isLoading:!1,isAddingInstagram:!1,pendingInstagramHandle:p()}),e=d.useCallback((r,s)=>a(f(r,s)),[a]),c=d.useCallback((r,s,o)=>{i({title:r,description:e(s,o),variant:"destructive"})},[i,e]),b=d.useCallback(async(r,s)=>{const{redirect:o=!0,showErrorToast:g=!0}=s??{};t(n=>({...n,isLoading:!0})),r.instagramUsername.trim()&&(P(r.instagramUsername),t(n=>({...n,pendingInstagramHandle:r.instagramUsername.trim().replace(/^@/,"")})));try{const{data:n,error:u}=await _.auth.signUp({email:r.email,password:r.password,options:{emailRedirectTo:`${window.location.origin}/dashboard`,data:{full_name:r.name,brand:U.id,...r.signupFlow?{signup_flow:r.signupFlow}:{}}}});if(u)throw u;if(!n.user)throw new Error(a("auth.errors.signup_failed"));const{error:w}=await _.auth.signInWithPassword({email:r.email,password:r.password});if(w)throw new Error(a("auth.errors.auto_login_failed"));return await Promise.all([R(),S(r.phone)]),o?await l():t(A=>({...A,isLoading:!1})),{ok:!0}}catch(n){g&&c(a("auth.errors.sign_up_error"),n,"signup"),t(w=>({...w,isLoading:!1,isAddingInstagram:!1}));const u=f(n,"signup");return{ok:!1,emailInUse:u==="auth.errors.user_already_registered",errorKey:u}}},[a,c,l]),v=d.useCallback(async(r,s)=>{t(o=>({...o,isLoading:!0}));try{const{data:o,error:g}=await _.auth.signInWithPassword({email:r.email,password:r.password});if(g)throw g;const n=T(),u=m(s)||m(n)||"/dashboard";console.log("[Auth] Sign in successful, navigating to:",u),window.location.href=u}catch(o){c(a("auth.errors.sign_in_error"),o,"signin"),t(g=>({...g,isLoading:!1,isAddingInstagram:!1}))}},[a,c]),y=d.useCallback(async r=>{try{r&&q(r);const{error:s}=await _.auth.signInWithOAuth({provider:"google",options:{redirectTo:`${window.location.origin}/auth`}});if(s)throw s}catch(s){c(a("auth.errors.google_sign_in_error"),s,"google")}},[a,c]),k=d.useCallback(async r=>{t(s=>({...s,isLoading:!0}));try{const{error:s}=await _.auth.resetPasswordForEmail(r,{redirectTo:`${window.location.origin}/update-password`});if(s)throw s;return i({title:a("auth.password_reset_success_title"),description:a("auth.password_reset_success_desc")}),!0}catch(s){return i({title:a("auth.errors.password_reset_error"),description:s instanceof Error?s.message:a("errors.something_wrong"),variant:"destructive"}),!1}finally{t(s=>({...s,isLoading:!1}))}},[i,a]),I=d.useCallback(async()=>{t(r=>({...r,isAddingInstagram:!!p()})),await l()},[l]);return{...h,signUp:b,signIn:v,signInWithGoogle:y,resetPassword:k,handleOAuthRedirect:I}};export{D as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.