1import{r as g,f as q,q as O,a as V,s as m,j as e,C as Y,h as J,i as G,k as K,l as Q,aA as R,T as W,B as X,J as t}from"./index-P0N1Je5T.js";import{u as Z,t as ee}from"./zod-B5uM1xf9.js";import{o as ae,s as x,l as se}from"./index-nOzG9PIh.js";import{I as L}from"./input-1tKBwIjz.js";import{F as re,a as f,b as y,c as j,d as S,f as T,e as b}from"./form-DLWQtRX4.js";import{d as te}from"./dh-logo-square-black-BwR7p7IZ.js";import{L as ne}from"./loader-circle-BQs4OzSl.js";import"./label-CoHN_xq2.js";const oe={Ã:"ss","áº":"ss",Å:"l",Å:"l",ø:"o",Ã:"o",æ:"ae",Ã:"ae",Å:"oe",Å:"oe",Ä:"d",Ä:"d",ð:"d",Ã:"d",þ:"th",Ã:"th",ı:"i",İ:"i"},N=n=>{if(!n)return"";let c=n;for(const[v,o]of Object.entries(oe))c=c.split(v).join(o);return c.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g,"").replace(/[^a-z0-9\s-]/g,"").replace(/\s+/g,"-").replace(/-+/g,"-").replace(/^-+|-+$/g,"").trim()},ie=n=>ae({name:x().min(2,n("admin.companySetup.validation.nameMin")),slug:x().min(2,n("admin.companySetup.validation.slugMin")).regex(/^[a-z0-9-]+$/,n("admin.companySetup.validation.slugFormat")),description:x().max(450,n("admin.companySetup.validation.descriptionMax")).optional(),website:x().url(n("admin.companySetup.validation.urlInvalid")).optional().or(se(""))}),fe=()=>{const[n,c]=g.useState(!1),v=q(),{profile:o,refetchProfile:z}=O(),{t:s}=V(),D=ie(s),i=Z({resolver:ee(D),defaultValues:{name:(o==null?void 0:o.display_name)||"",slug:N((o==null?void 0:o.display_name)||""),description:"",website:""}}),w=i.watch("name"),l=i.watch("slug"),A=!!w&&w.trim().length>=2&&N(w).length===0,[C,_]=g.useState(null),[d,F]=g.useState(!1);g.useEffect(()=>{if(!l||l.length<2||!/^[a-z0-9-]+$/.test(l)){_(null);return}F(!0);const a=setTimeout(async()=>{const{data:r,error:u}=await m.rpc("check_slug_available",{p_slug:l});u||_(!!r),F(!1)},400);return()=>clearTimeout(a)},[l]);const B=a=>{const r=N(a);i.setValue("slug",r,{shouldValidate:!0})},H=async a=>{if(!o){t.error(s("admin.companySetup.messages.profileNotFound"));return}c(!0);try{const{data:{user:r},error:u}=await m.auth.getUser();if(u||!r){console.error("Authentication error:",u),t.error("User not authenticated. Please log in again.");return}const{data:{session:$},error:U}=await m.auth.getSession();if(U||!$){console.error("Session error:",U),t.error("Session expired. Please log in again.");return}const{data:le,error:k}=await m.from("profiles").select("id").eq("user_id",r.id).limit(1);if(k){console.error("Auth context test failed:",k),t.error("Authentication context invalid. Please refresh and try again.");return}const{data:E,error:p}=await m.rpc("create_company_secure",{p_name:a.name,p_slug:a.slug,p_landing_page_name:a.slug,p_description:a.description||null,p_website:a.website||null});if(p){console.error("Erro ao criar empresa:",p);const h=p.message||"";if(h.includes("SLUG_TAKEN")||h.includes("Company slug already exists")||h.includes("Landing page name already exists")){t.error(s("admin.companySetup.messages.slugTaken")),_(!1);return}if(h.includes("User not authenticated")){t.error("User not authenticated. Please log in again.");return}t.error(s("admin.companySetup.messages.createError")+p.message);return}const I=Array.isArray(E)?E:[E];if(!I||I.length===0){t.error("No company data returned after creation");return}const{data:P,error:M}=await m.from("profiles").select("company_id").eq("user_id",r.id).single();if(M){console.error("[COMPANY SETUP] Erro ao verificar profile:",M),t.error("Erro ao verificar criação da empresa. Por favor, recarregue a página.");return}if(!(P!=null&&P.company_id)){console.error("[COMPANY SETUP] Profile não foi atualizado com company_id após criação"),t.error("Empresa criada mas houve erro na vinculação. Por favor, recarregue a página e tente novamente.");return}t.success(s("admin.companySetup.messages.success")),await z(),v("/admin")}catch(r){console.error("Erro inesperado:",r),t.error(s("admin.companySetup.messages.unexpectedError"))}finally{c(!1)}};return e.jsx("div",{className:"min-h-screen bg-gradient-to-br from-background to-muted/20 flex items-center justify-center p-4",children:e.jsxs(Y,{className:"w-full max-w-2xl",children:[e.jsxs(J,{className:"text-center space-y-4",children:[e.jsx("div",{className:"mx-auto w-16 h-16 flex items-center justify-center",children:e.jsx("img",{src:te,alt:"Drop Highlight",className:"w-16 h-16"})}),e.jsx(G,{className:"text-2xl font-bold",children:s("admin.companySetup.title")}),e.jsx(K,{children:s("admin.companySetup.subtitle")})]}),e.jsx(Q,{children:e.jsx(re,{...i,children:e.jsxs("form",{onSubmit:i.handleSubmit(H),className:"space-y-6",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm font-medium text-muted-foreground",children:[e.jsx(R,{className:"w-4 h-4"}),s("admin.companySetup.sections.companyInfo")]}),e.jsx(f,{control:i.control,name:"name",render:({field:a})=>e.jsxs(y,{children:[e.jsxs(j,{children:[s("admin.companySetup.fields.companyName")," *"]}),e.jsx(S,{children:e.jsx(L,{placeholder:s("admin.companySetup.fields.companyNamePlaceholder"),...a,onChange:r=>{a.onChange(r),B(r.target.value)}})}),l&&!A&&e.jsxs(T,{children:["Your URL will be:"," ",e.jsxs("span",{className:"font-mono text-foreground",children:["drophighlight.com/",l]}),d&&e.jsx("span",{className:"ml-2 text-xs text-muted-foreground",children:"checkingâ¦"}),!d&&C===!0&&e.jsx("span",{className:"ml-2 text-xs text-emerald-600",children:"available"}),!d&&C===!1&&e.jsx("span",{className:"ml-2 text-xs text-destructive",children:"already taken"})]}),A&&e.jsx("p",{className:"text-sm text-destructive",children:"This name contains only characters that can't be used in a URL (e.g. emojis or non-Latin scripts). Please add Latin letters, or edit the URL name field below manually."}),e.jsx(b,{})]})}),e.jsx(f,{control:i.control,name:"slug",render:({field:a})=>e.jsxs(y,{children:[e.jsx(j,{children:"Public URL"}),e.jsx(S,{children:e.jsx(L,{...a,onChange:r=>a.onChange(N(r.target.value)),placeholder:"your-company"})}),e.jsx(T,{children:"Adjust manually if the auto-generated URL is already taken."}),e.jsx(b,{})]})}),e.jsx(f,{control:i.control,name:"description",render:({field:a})=>{var r;return e.jsxs(y,{children:[e.jsx(j,{children:s("admin.companySetup.fields.description")}),e.jsx(S,{children:e.jsx(W,{placeholder:s("admin.companySetup.fields.descriptionPlaceholder"),className:"min-h-[80px]",maxLength:450,...a})}),e.jsxs(T,{className:"flex justify-between items-center",children:[e.jsx("span",{children:s("admin.companySetup.fields.descriptionHelper")}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[((r=a.value)==null?void 0:r.length)||0,"/450"]})]}),e.jsx(b,{})]})}})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm font-medium text-muted-foreground",children:[e.jsx(R,{className:"w-4 h-4"}),s("admin.companySetup.sections.webSettings")]}),e.jsx(f,{control:i.control,name:"website",render:({field:a})=>e.jsxs(y,{children:[e.jsx(j,{children:s("admin.companySetup.fields.website")}),e.jsx(S,{children:e.jsx(L,{placeholder:s("admin.companySetup.fields.websitePlaceholder"),...a})}),e.jsx(b,{})]})})]}),e.jsx(X,{type:"submit",className:"w-full",disabled:n||C===!1||d,children:n?e.jsxs(e.Fragment,{children:[e.jsx(ne,{className:"w-4 h-4 mr-2 animate-spin"}),s("admin.companySetup.buttons.settingUp")]}):s("admin.companySetup.buttons.setupCompany")})]})})})]})})};export{fe as CompanySetup,N as sanitizeToSlug};
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.