import{i as e,n as t,t as n}from"./jsx-runtime-DG_AJzB_.js";import{a as r,o as i}from"./api-base-url-DIb6qukm.js";import{n as a,t as o}from"./createReactComponent-_L2mp4Ui.js";import{t as s}from"./IconArrowRight-DsOD7mDl.js";import{t as c}from"./auth-client-CVsUOxFj.js";import{d as l,p as u}from"./schemas-BwLRjo3Z.js";import{A as d,V as f}from"./index-BQ5WpFpw.js";import{t as p}from"./input-CZTmGabI.js";import{t as m}from"./label-daGr3DrB.js";import{a as h,n as g,o as _,r as v,t as y}from"./card-CZCl7QNy.js";import{n as b,t as x}from"./zod-CtCK2PFe.js";var S=o(`outline`,`building`,`Building`,[[`path`,{d:`M3 21l18 0`,key:`svg-0`}],[`path`,{d:`M9 8l1 0`,key:`svg-1`}],[`path`,{d:`M9 12l1 0`,key:`svg-2`}],[`path`,{d:`M9 16l1 0`,key:`svg-3`}],[`path`,{d:`M14 8l1 0`,key:`svg-4`}],[`path`,{d:`M14 12l1 0`,key:`svg-5`}],[`path`,{d:`M14 16l1 0`,key:`svg-6`}],[`path`,{d:`M5 21v-16a2 2 0 0 1 2 -2h10a2 2 0 0 1 2 2v16`,key:`svg-7`}]]),C=e(t(),1),w=n(),T=l({name:u().min(2,`Organization name must be at least 2 characters`),slug:u().min(2,`Slug must be at least 2 characters`).regex(/^[a-z0-9-]+$/,`Slug can only contain lowercase letters, numbers, and hyphens`)}),E=r(`/_authenticated/onboarding/create-org`);function D(e){return e.toLowerCase().replaceAll(/[^a-z0-9]+/g,`-`).replaceAll(/^-|-$/g,``)}function O(e){if(!(e instanceof Error))return`Failed to create organization`;let t=e.message.trim();return t.length>0?t:`Failed to create organization`}function k(){let e=i(),t=f(),{auth:n,refetchSession:r}=E.useRouteContext(),[o,l]=(0,C.useState)(!1),u=n.session?.user.id,k=typeof window>`u`?`your-host`:window.location.host,A=b({defaultValues:{name:``,slug:``},resolver:x(T)}),j=A.watch(`name`),M=A.watch(`slug`);async function N(n){l(!0);try{let i=await c.create({name:n.name,slug:n.slug});if(i.error){A.setError(`root`,{message:i.error.message??`Failed to create organization`}),l(!1);return}let a=i.data?.id;if(!a){A.setError(`root`,{message:`Failed to create organization`}),l(!1);return}await c.setActive({organizationId:a}),await r(),await t.invalidateQueries({queryKey:d(u).queryKey}),await e({search:{orgId:a},to:`/onboarding/connect-database`})}catch(e){A.setError(`root`,{message:O(e)}),l(!1)}}return(0,w.jsx)(`div`,{className:`flex min-h-screen flex-col items-center justify-center p-6`,children:(0,w.jsxs)(y,{className:`w-full max-w-md`,children:[(0,w.jsxs)(h,{className:`text-center`,children:[(0,w.jsx)(`div`,{className:`mx-auto mb-4 flex h-12 w-12 items-center justify-center rounded-full bg-muted`,children:(0,w.jsx)(S,{size:24,stroke:1.5})}),(0,w.jsx)(_,{className:`text-xl`,children:`Create your organization`}),(0,w.jsx)(v,{children:`Set up your workspace to get started with OneQuery`})]}),(0,w.jsx)(g,{children:(0,w.jsxs)(`form`,{onSubmit:e=>{A.handleSubmit(N)(e)},className:`space-y-4`,children:[(0,w.jsxs)(`div`,{className:`space-y-2`,children:[(0,w.jsx)(m,{htmlFor:`name`,children:`Organization name`}),(0,w.jsx)(p,{id:`name`,placeholder:`Acme Inc.`,...A.register(`name`,{onChange:e=>{let t=D(e.target.value);A.setValue(`slug`,t)}})}),A.formState.errors.name&&(0,w.jsx)(`p`,{className:`text-sm text-destructive`,children:A.formState.errors.name.message})]}),(0,w.jsxs)(`div`,{className:`space-y-2`,children:[(0,w.jsx)(m,{htmlFor:`slug`,children:`URL slug`}),(0,w.jsxs)(`div`,{className:`flex items-center gap-2`,children:[(0,w.jsxs)(`span`,{className:`text-sm text-muted-foreground`,children:[k,`/`]}),(0,w.jsx)(p,{id:`slug`,placeholder:`acme`,...A.register(`slug`),className:`flex-1`})]}),A.formState.errors.slug&&(0,w.jsx)(`p`,{className:`text-sm text-destructive`,children:A.formState.errors.slug.message})]}),A.formState.errors.root&&(0,w.jsx)(`p`,{className:`text-sm text-destructive`,children:A.formState.errors.root.message}),(0,w.jsxs)(a,{type:`submit`,className:`w-full`,disabled:o||!j||!M,children:[o?`Setting up...`:`Continue`,(0,w.jsx)(s,{size:16,stroke:2})]})]})})]})})}var A=k;export{A as component};