1import{u as D,b as U,r as n,j as e,d as h,a as Y,s as V}from"./index-DjvjYx8D.js";import{C as G,b as Z,c as $,a as z}from"./card-BoeYN9zz.js";import{T as H,a as O,b as _,c as q}from"./tabs-DP0G5vfD.js";import{B as C}from"./button-Cz5sYbhP.js";import{I as L}from"./input-vy9OFss5.js";import{L as T}from"./label-BVrYemgw.js";import{u as J}from"./useImpersonation-CWFUls8c.js";import{w as I}from"./queryTimeout-DjX5RWJs.js";import{c as K,g as Q}from"./roleCache-CPULw5mK.js";import{o as X,s as B,Z as ee}from"./types-CtWaBghH.js";import{A as te}from"./ApplicationForm-BJFqQpMH.js";import{P as se}from"./PublicNavBar-C-wZl_UG.js";import"./index-DAoShclu.js";import"./index-D8K3azz-.js";import"./select-CfoOE7i0.js";import"./index-BdQq_4o_.js";import"./Combination-Cmn8Mk01.js";import"./index-Dv7kNRC4.js";import"./chevron-down-BXueFYvF.js";import"./check-BUcFAsHw.js";import"./dropdown-menu-MtG1yKvd.js";import"./chevron-right-LW5lYHnM.js";import"./badge-Cl_SCNNJ.js";import"./textarea-BNnM3daC.js";import"./slider-79s94pcJ.js";import"./dialog-B9zYFgAh.js";import"./index-Dxopu1_s.js";import"./applicationDraftStorage-BRd3BC6p.js";import"./metaConversions-B0uTN9UF.js";import"./pricingCalculator-BphUV8v0.js";import"./circle-check-D_eqDBdz.js";import"./mail-eghF6GbV.js";import"./arrow-right-BQhxQtC5.js";import"./arrow-left-DXjop1Hu.js";import"./save-BOhb9Hg4.js";import"./melon-logo-DaaW67WJ.js";const ae=X({email:B().trim().email("Please enter a valid email address"),password:B().min(6,"Password must be at least 6 characters")}),ie=()=>{const s=D(),{toast:t}=U(),{stopImpersonation:x}=J(),[i,A]=n.useState(""),[b,a]=n.useState(""),[p,u]=n.useState(!1),[c,m]=n.useState({}),[v,o]=n.useState(!1),[j,N]=n.useState(""),[y,k]=n.useState(!1),S=async r=>{r.preventDefault();const l=(j||i).trim();if(!l||!/^\S+@\S+\.\S+$/.test(l)){t({title:"Enter a valid email",description:"We need a valid email to send the sign-in link.",variant:"destructive"});return}k(!0);try{await h.auth.signInWithOtp({email:l,options:{emailRedirectTo:`${window.location.origin}/get-started?tab=apply&resume=1`,shouldCreateUser:!1}}),t({title:"Check your inbox",description:"If an account exists for this email, we've sent a one-time sign-in link."}),o(!1)}catch{t({title:"Check your inbox",description:"If an account exists for this email, we've sent a one-time sign-in link."})}finally{k(!1)}},M=async r=>{r.preventDefault(),m({});try{ae.parse({email:i,password:b}),x(),u(!0);const{error:l}=await I(h.auth.signInWithPassword({email:i,password:b}),5e3,"Sign in timed out. Please try again.");if(l){t({title:"Login Failed",description:l.message,variant:"destructive"});return}const{data:{user:d}}=await I(h.auth.getUser(),5e3,"Authentication check timed out");if(d){const{data:f}=await I((async()=>await h.from("user_roles").select("role").eq("user_id",d.id))(),5e3,"Role check timed out"),g=(f==null?void 0:f.map(w=>w.role))||[];K(d.id,g);const P=g.includes("admin"),R=g.includes("staff"),W=g.includes("dealer"),F=g.includes("affiliate")||g.includes("super_affiliate"),E=g.includes("customer");if(!E&&!P&&!R&&!W&&!F){const{data:w}=await I((async()=>await h.from("applications").select("status").eq("user_id",d.id).order("created_at",{ascending:!1}).limit(1).maybeSingle())(),5e3,"Application check timed out");if(w){if(w.status==="pending"){t({title:"Application Under Review",description:"Your application is being reviewed. We'll notify you via email."}),s("/application-pending");return}else if(w.status==="rejected"){t({title:"Application Not Approved",description:"Your application was not approved. Please contact support for more information.",variant:"destructive"});return}}}P||R?(t({title:"Welcome back!",description:"Redirecting to admin dashboard..."}),s("/admin")):W?(t({title:"Welcome back!",description:"Redirecting to dealer portal..."}),s("/dealer")):F&&!E?(t({title:"Welcome back!",description:"Redirecting to affiliate portal..."}),s("/affiliate")):E?(t({title:"Welcome back!",description:"You have successfully logged in."}),s("/dashboard")):(t({title:"Welcome back!",description:"You have successfully logged in."}),s("/dashboard"))}}catch(l){if(l instanceof ee){const d={};
1l.errors.forEach(f=>{f.path[0]&&(d[f.path[0]]=f.message)}),m(d)}}finally{u(!1)}};return e.jsxs("form",{onSubmit:M,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(T,{htmlFor:"login-email",children:"Email"}),e.jsx(L,{id:"login-email",type:"email",placeholder:"[email protected]",value:i,onChange:r=>A(r.target.value),required:!0,className:"h-12"}),c.email&&e.jsx("p",{className:"text-sm text-destructive",children:c.email})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(T,{htmlFor:"login-password",children:"Password"}),e.jsx(L,{id:"login-password",type:"password",placeholder:"â¢â¢â¢â¢â¢â¢â¢â¢",value:b,onChange:r=>a(r.target.value),required:!0,className:"h-12"}),c.password&&e.jsx("p",{className:"text-sm text-destructive",children:c.password}),e.jsx("div",{className:"flex justify-end",children:e.jsx(C,{type:"button",variant:"link",className:"text-sm px-0 h-auto",onClick:()=>s("/reset-password"),children:"Forgot password?"})})]}),e.jsx(C,{type:"submit",variant:"gradient",className:"w-full",size:"lg",disabled:p,children:p?"Signing In...":"Sign In"}),e.jsx("div",{className:"pt-2 border-t border-border/50",children:v?e.jsxs("div",{className:"space-y-3 pt-1",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"We'll email you a one-time link to sign in â no password needed. Works even if you never set one."}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(T,{htmlFor:"magic-email",children:"Your email"}),e.jsx(L,{id:"magic-email",type:"email",placeholder:"[email protected]",value:j,onChange:r=>N(r.target.value),className:"h-12"})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(C,{type:"button",variant:"outline",className:"flex-1",onClick:()=>o(!1),disabled:y,children:"Cancel"}),e.jsx(C,{type:"button",className:"flex-1",onClick:S,disabled:y,children:y?"Sending...":"Send sign-in link"})]})]}):e.jsxs("button",{type:"button",onClick:()=>{N(i),o(!0)},className:"w-full text-sm text-muted-foreground hover:text-foreground transition-colors text-center",children:["Can't log in? ",e.jsx("span",{className:"underline",children:"Email me a sign-in link"})]})})]})},Ue=()=>{const s=D(),[t]=Y(),[x,i]=n.useState("apply");n.useEffect(()=>{const a=t.get("tab");a&&["login","apply"].includes(a)&&i(a);const p=t.get("ref");p&&V(p);const{data:{subscription:u}}=h.auth.onAuthStateChange((c,m)=>{if(c==="SIGNED_IN"&&m){if(t.get("resume")==="1"){sessionStorage.setItem("melon:resume-banner","1"),i("apply");return}if(sessionStorage.getItem("melon:application-in-progress")==="1")return;const o=Q(m.user.id);if(o){const j=o.includes("admin"),N=o.includes("staff"),y=o.includes("dealer"),k=o.includes("affiliate"),S=o.includes("customer");s(j||N?"/admin":y?"/dealer":k&&!S?"/affiliate":"/dashboard")}else s("/dashboard")}});return()=>u.unsubscribe()},[s,t]),n.useEffect(()=>{const a=t.get("tab"),p=t.get("bike");if(a!=="apply"||!p)return;let u=!1;const c=performance.now(),m=()=>{if(u)return;const v=document.getElementById("your-ride-summary");if(v){v.scrollIntoView({behavior:"smooth",block:"start"});return}performance.now()-c<4e3&&requestAnimationFrame(m)};return requestAnimationFrame(m),()=>{u=!0}},[t]);const A=()=>{i("login");const a=document.getElementById("login-section");a&&a.scrollIntoView({behavior:"smooth"})},b=()=>{i("apply");const a=document.getElementById("login-section");a&&a.scrollIntoView({behavior:"smooth"})};return e.jsxs("div",{className:"min-h-screen overflow-x-clip bg-gradient-to-b from-[hsl(174_45%_98%)] to-background",children:[e.jsx(se,{onLoginClick:A,onApplyClick:b}),e.jsx("main",{id:"login-section",className:"container mx-auto px-4 pt-24 pb-12",children:e.jsxs("div",{className:"max-w-4xl mx-auto",children:[e.jsx("h1",{className:"sr-only",children:"Get Started with Melon â Apply or Sign In"}),e.jsxs(H,{value:x,onValueChange:i,className:"w-full",children:[e.jsxs(O,{className:"grid w-full grid-cols-2 mb-8 h-14",children:[e.jsx(_,{value:"login",className:"text-base",children:"Login"}),e.jsx(_,{value:"apply",className:"text-base",children:"Apply Now"})]}),e.jsxs(G,{className:"shadow-card",children:[e.jsx(Z,{children:e.jsxs($,{className:"text-center text-base",children:[x==="login"&&"Welcome back! Sign in to your account",x==="apply"&&"Apply for your rent-to-buy vehicle"]})}),e.jsxs(z,{children:[e.jsx(q,{value:"login",className:"mt-0",children:e.jsx(ie,{})}),e.jsx(q,{value:"apply",className:"mt-0",children:e.jsx(te,{})})]})]})]})]})})]})};export{Ue as default};
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.