import{i as e,t}from"./jsx-runtime-Cx0BB4qO.js";import{Q as n,d as r,l as i,m as a,n as o,q as s}from"./workspace-D4nk9hec.js";import{t as c}from"./useNavigate-CWOSRTQR.js";import{F as l,a as u,c as d,s as f}from"./common-C53Tlnzg.js";import{t as p}from"./arrow-right-BguE2c08.js";import{t as m}from"./house-B49vR_6k.js";var h=e(n(),1),g=`authorization_response`,_=`/~oauth/initiate`,v=[`https://oauth.lovable.app`,`https://lovable.dev`],y=`lovable://oauth-callback`,b=`http://127.0.0.1/iframe-oauth/callback`,x=500,S=12e4,C=3e5,w=/LovableAppScheme\/([a-z0-9.+-]+)/i;function T(e){let t=w.exec(e)?.[1];return t?`${t}://oauth-callback`:y}function E(e){let t=window.location.origin;if(t===`null`)return e;let n;try{n=new URL(e,window.location.href)}catch{return e}if(n.origin===t)return e;let r=`${t}${n.pathname}${n.search}${n.hash}`;return console.warn(`[cloud-auth-js] redirect_uri origin ${n.origin} differs from the page origin ${t}; using ${r} so the popup result can reach this window`),r}function D(e){let t,n=new Promise(e=>{t=e}),r=n=>{if(!e.some(e=>n.origin===e))return;let r=n.data;!r||typeof r!=`object`||r.type===g&&t(r.response)};return window.addEventListener(`message`,r),{cleanup:()=>{window.removeEventListener(`message`,r)},messagePromise:n}}function O(e){let t=window.screenX!==0||window.screenY!==0||!e,n=t?window.outerWidth*.5:window.screen.width*.5,r=t?window.outerHeight*.5:window.screen.height*.5;return{width:n,height:r,left:t?window.screenX+(window.outerWidth-n)/2:(window.screen.width-n)/2,top:t?window.screenY+(window.outerHeight-r)/2:(window.screen.height-r)/2}}function k(e,t){return e.state===t?e.error?e.error===`legacy_flow`?{error:Error(`This flow is not supported in Preview mode. Please open the app in a new tab to sign in.`)}:{error:Error(e.error_description??`Sign in failed`)}:!e.access_token||!e.refresh_token?{error:Error(`No tokens received`)}:{tokens:{access_token:e.access_token,refresh_token:e.refresh_token},error:null}:{error:Error(`State is invalid`)}}function A(){let e=navigator.userAgent;return!!(/iPhone|iPad|iPod|Android/i.test(e)||/Macintosh/i.test(e)&&navigator.maxTouchPoints>1)}function j(){return typeof crypto<`u`&&crypto.getRandomValues?[...crypto.getRandomValues(new Uint8Array(16))].map(e=>e.toString(16).padStart(2,`0`)).join(``):Math.random().toString(36).substring(2)+Date.now().toString(36)}function M(e={}){let t=e.oauthBrokerUrl??_,n=e.supportedOAuthOrigins??v;async function r(e,r={}){let i=!1;try{i=window.self!==window.top}catch{i=!0}let a=navigator.userAgent,o=/LovableApp\//.test(a),s=!o&&/lovable/i.test(a),c=j(),l=r.redirect_uri??window.location.origin;o&&i?l=T(a):s&&i&&(l=b);let u=i&&!o&&!s;u&&(l=E(l));let d=new URLSearchParams({...r.extraParams,provider:e,redirect_uri:l,state:c});if(!i)return window.location.href=`${t}?${d.toString()}`,{error:null,redirected:!0};u&&d.set(`response_mode`,`web_message`);let f=`${t}?${d.toString()}`,{messagePromise:p,cleanup:m}=D(s?[...n,window.location.origin]:n),h;if(A())h=window.open(f,`_blank`);else{let{width:e,height:t,left:n,top:r}=O(i);h=window.open(f,`oauth`,`width=${e},height=${t},left=${n},top=${r}`)}if(!h&&(o||s)){let e,t=new Promise((t,n)=>{e=setTimeout(()=>{n(Error(`OAuth timed out waiting for response`))},S)});try{return k(await Promise.race([p,t]),c)}catch(e){return{error:e instanceof Error?e:Error(String(e))}}finally{e&&clearTimeout(e),m()}}if(!h)return m(),{error:Error(`Popup was blocked`)};let g,_=new Promise((e,t)=>{g=setInterval(()=>{h.closed&&(clearInterval(g),t(Error(`Sign in was cancelled`)))},x)}),v,y=new Promise((e,t)=>{v=setTimeout(()=>{t(Error(`Sign-in timed out waiting for the sign-in window. If it is still open, finish there, or open the app in a new browser tab to sign in.`))},C)});try{return k(await Promise.race([p,_,y]),c)}catch(e){return{error:e instanceof Error?e:Error(String(e))}}finally{clearTimeout(v),clearInterval(g),m(),h?.close()}}return{signInWithOAuth:r}}function N(e={}){return M(e)}var P=N(),F={auth:{signInWithOAuth:async(e,t)=>{let n=await P.signInWithOAuth(e,{...t,extraParams:{...t?.extraParams}});if(n.redirected||n.error)return n;try{await s.auth.setSession(n.tokens)}catch(e){return{error:e instanceof Error?e:Error(String(e))}}return n}}},I=t();function L({invite:e}){r();let[t,n]=(0,h.useState)(!1),[a,g]=(0,h.useState)(!1),_=d(),v=c();async function y(){e&&o(await s.rpc(`join_collective`,{_code:e})),await v({to:`/hjem`})}return(0,I.jsxs)(`div`,{className:`auth-panel`,children:[(0,I.jsxs)(`div`,{className:`mb-6 flex items-center gap-3`,children:[(0,I.jsx)(`span`,{className:`brand-mark`,children:(0,I.jsx)(m,{})}),(0,I.jsx)(`h1`,{className:`text-2xl font-bold`,children:i(`Kollektivappen`)})]}),(0,I.jsx)(`h2`,{className:`mb-6 text-xl`,children:i(t?`Velkommen til ditt digitale hjem`:`Godt å ha deg hjemme.`)}),a?(0,I.jsx)(`p`,{className:`py-6`,children:i(`Sjekk e-posten din for å bekrefte kontoen. Logg inn når den er bekreftet.`)}):(0,I.jsxs)(`form`,{className:`grid gap-4`,onSubmit:e=>{e.preventDefault();let n=new FormData(e.currentTarget);_.mutate(async()=>{t?o(await s.auth.signUp({email:f(n,`email`),password:f(n,`password`),options:{data:{display_name:f(n,`name`)},emailRedirectTo:window.location.origin+`/auth`}})).session?await y():g(!0):(o(await s.auth.signInWithPassword({email:f(n,`email`),password:f(n,`password`)})),await y())})},children:[t&&(0,I.jsx)(u,{label:i(`Navnet ditt`),name:`name`,required:!0}),(0,I.jsx)(u,{label:i(`E-post`),name:`email`,type:`email`,required:!0}),(0,I.jsx)(u,{label:i(`Passord`),name:`password`,type:`password`,required:!0}),(0,I.jsxs)(l,{type:`submit`,disabled:_.isPending,children:[i(t?`Opprett konto`:`Logg inn`),(0,I.jsx)(p,{})]})]}),(0,I.jsxs)(`div`,{className:`my-5 flex items-center gap-3 text-xs text-muted-foreground`,children:[(0,I.jsx)(`span`,{className:`h-px flex-1 bg-border`}),i(`eller`),(0,I.jsx)(`span`,{className:`h-px flex-1 bg-border`})]}),(0,I.jsx)(`div`,{className:`grid gap-2`,children:[`google`,`lovable`].map(t=>(0,I.jsxs)(l,{variant:`outline`,onClick:()=>_.mutate(async()=>{e&&sessionStorage.setItem(`collective-invite`,e);let n=await F.auth.signInWithOAuth(t,{redirect_uri:window.location.origin+`/auth`});if(n.error)throw n.error;n.redirected||await y()}),children:[i(`Fortsett med`),` `,t===`google`?`Google`:`Lovable`]},t))}),(0,I.jsx)(l,{className:`mt-4 w-full`,variant:`link`,onClick:()=>{n(!t),g(!1)},children:i(t?`Har du konto? Logg inn`:`Ny her? Opprett konto`)})]})}function R(){r();let e=d(),t=a();return(0,I.jsxs)(`div`,{className:`auth-panel`,children:[(0,I.jsx)(`h1`,{className:`mb-2 text-3xl font-bold`,children:i(`Et hjem, sammen.`)}),(0,I.jsx)(`p`,{className:`mb-8 text-muted-foreground`,children:i(`Opprett kollektivet ditt eller bli med vennene dine.`)}),(0,I.jsxs)(`form`,{className:`grid gap-4`,onSubmit:n=>{n.preventDefault();let r=new FormData(n.currentTarget);e.mutate(async()=>{o(await s.rpc(`create_collective`,{_name:f(r,`name`)})),await t.invalidateQueries()})},children:[(0,I.jsx)(u,{label:i(`Hva heter kollektivet?`),name:`name`,required:!0}),(0,I.jsxs)(l,{disabled:e.isPending,children:[i(`Opprett kollektiv`),(0,I.jsx)(p,{})]})]}),(0,I.jsx)(`div`,{className:`my-8 h-px bg-border`}),(0,I.jsxs)(`form`,{className:`grid gap-4`,onSubmit:n=>{n.preventDefault();let r=new FormData(n.currentTarget);e.mutate(async()=>{let e=f(r,`code`).split(`/`).filter(Boolean).pop()||``;o(await s.rpc(`join_collective`,{_code:e})),await t.invalidateQueries()})},children:[(0,I.jsx)(u,{label:i(`Invitasjonslenke eller kode`),name:`code`,required:!0}),(0,I.jsx)(l,{variant:`outline`,disabled:e.isPending,children:i(`Bli med i kollektiv`)})]})]})}export{R as n,L as t};