1import{m as J,J as re,u as Ce,f as X,r as a,p as H,ag as $e,s as P,ae as te,ah as Ee,ai as Ke,aj as Pe,j as e,I as ie,ak as se,al as We,Y as oe,a9 as Qe,W as fe,S as G,X as Fe,l as Ue,B as U,am as Ve,v as Ye,C as ve,P as De,an as Q,ao as je,ap as Ge,aq as He,af as Be,ar as Je,as as Xe,at as Ze,au as et,z as tt,av as st,g as nt,aw as ee,ax as ce,h as at,d as rt,ay as it,az as ot,aA as ye,aB as ct,aC as lt,aD as dt,aE as ut}from"./index-DL0d_lAK.js";import{a as mt,b as ft,c as pt,u as ze}from"./useBrandAssets-BgjUMr2B.js";import{s as qe,a as gt,i as xt}from"./compose-DOjjQ7ye.js";import{R as Ie}from"./ring-DOI_kVyQ.js";import{W as ht}from"./wizard-steps-CJsUeFyd.js";import{c as Te,C as xe,T as he,a as ae,S as bt,r as we,v as Ne,s as Ae,i as vt,p as le,d as jt,k as yt}from"./SetupShow-dJezh6pB.js";import{I as Oe}from"./input-BZVgYfh9.js";import{A as wt}from"./AssetManager-1guXkkVJ.js";import{A as Nt,P as St,C as _t}from"./Products-HN3OR0Ff.js";import{P as kt}from"./package-BxsBUoPB.js";import{S as Ct,d as Re,s as Et}from"./Competitors-BU_oaQVZ.js";import{T as Pt}from"./tooltip-DE3ztT2v.js";import{S as zt}from"./sparkle-C-SzCLEl.js";import{I as qt}from"./InstagramConnect-B0_E5wPE.js";import{s as It}from"./igOauth-BKYGYQLv.js";import{F as Tt,M as At}from"./FirstPosts-1qzqH7db.js";import{C as de}from"./chevron-right-Bnb0q334.js";import"./useMutation-D_ZYaUMO.js";import"./upload-DGmusEJv.js";import"./card-DnGeKDac.js";import"./select-DCy2S0v2.js";import"./file-upload-BLNZQkWD.js";import"./upload-CRc1gxYv.js";import"./star-wjux-bxy.js";import"./checkbox-CfDdTcFe.js";import"./filter-select-BrIOUJFK.js";import"./chip-BlU4hIu0.js";import"./SectionLayout-C49x_sxr.js";import"./list-filter-CGqFwn1t.js";import"./PageHeader-C6rx3uss.js";import"./EmptyState-BWePa1hl.js";import"./chevron-down-CIYFcfTQ.js";import"./search-BDZPLI7M.js";import"./list-BpKExd-V.js";import"./layout-grid-Bk1IDLDF.js";import"./pencil-C2hosQyU.js";import"./trash-2-DHhGh4SX.js";import"./rss-BTvWcJiO.js";import"./refresh-cw-D6LjrvwT.js";import"./download-fXEMRr1g.js";import"./store-BmDEDP4T.js";import"./external-link-Dcz4p2aP.js";import"./globe-EvdRHgaC.js";import"./facebook-BbOQPb3V.js";import"./renderImage-BWv-M-n1.js";import"./textTags-BjQI4wYd.js";let pe=null;const Ot=t=>{pe=t??null},Rt=()=>{const t=pe;return pe=null,t},ne=new Map;function Me(t,n,l){if(!t||!n||ne.has(t))return;const s=J("scan-site-products",{brand_id:t,url:n,limit:20}).catch(()=>{}).finally(()=>
1{ne.delete(t),l.invalidateQueries({queryKey:["products",t]})});ne.set(t,s)}const ge=t=>ne.get(t)??null,Se=["Reading your websiteâ¦","Finding your logo and imagesâ¦","Extracting your story and audienceâ¦","Picking your brand colorsâ¦","Almost there, saving what we foundâ¦"];function Mt({onCreated:t,brandId:n,registerPrimary:l,onReset:s,formFlow:r=null,heading:c}){var I,Y,Z;const{user:m}=re(),{activeBrand:i}=Ce(),d=X(),[h,b]=a.useState(((I=r==null?void 0:r.initial)==null?void 0:I.name)??""),[p,y]=a.useState((((Y=r==null?void 0:r.initial)==null?void 0:Y.url)??"").replace(/^https?:\/\//i,"")),[M,z]=a.useState((((Z=r==null?void 0:r.initial)==null?void 0:Z.instagram)??"").replace(/^@/,"")),[g,f]=a.useState(n&&!r?"done":"form"),[E,W]=a.useState(null),[N,S]=a.useState(""),[w,A]=a.useState(""),[v,o]=a.useState(0),L=H(p.trim()),[C,V]=a.useState(""),[O,$]=a.useState(!1),u=()=>`https://${p.trim().replace(/^https?:\/\//i,"")}`;a.useEffect(()=>{if($(!1),!L.includes(".")){V("");return}const j=setTimeout(()=>V(L),400);return()=>clearTimeout(j)},[L]);const _=a.useRef(!1),R=E??i,F=!!n&&!!p.trim()&&!!(R!=null&&R.domain)&&H(p.trim())===H(R.domain);a.useEffect(()=>{var T;if(r){l({label:"Next",chevron:!0,disabled:!h.trim(),run:()=>r.onSubmit({name:h.trim(),url:p.trim(),instagram:M.trim()})}),(T=r.onProgress)==null||T.call(r,$e(h,p,M));return}if(g==="done"){l(null);return}const j=!!p.trim()&&!F,k=g==="failed"&&!!w&&u()===w;l({label:g==="scanning"?"Scanningâ¦":k?"Change the address to scan again":g==="failed"?"Try again":j?"Scan my brand":"Next",disabled:g==="scanning"||!h.trim()||k,run:()=>K(j)})},[g,h,p,M,F,w]),a.useEffect(()=>()=>l(null),[]),a.useEffect(()=>{if(g!=="scanning")return;const j=setInterval(()=>o(k=>(k+1)%Se.length),4e3);return()=>clearInterval(j)},[g]);async function K(j){if(!_.current){_.current=!0,S("");try{let k={};if(j&&(f("scanning"),k=(await J("scan-brand",{url:u()})).fields??{}),n&&j){await Promise.all([P.from("products").delete().eq("brand_id",n),P.from("competitors").delete().eq("brand_id",n),P.from("brand_voices").delete().eq("brand_id",n),P.from("knowledge_items").delete().eq("brand_id",n)]);for(const q of["products","competitors","brand_voices","knowledge_items"])d.invalidateQueries({queryKey:[q,n]});s==null||s()}const T={...k,name:h.trim()||k.name||"My brand"},{data:D,error:x}=n?await P.from("brands").update(T).eq("id",n).select().single():await P.from("brands").insert({...T,user_id:m.id}).select().single();if(x)throw new Error(x.message);W(D),D.country&&Te(D.id).catch(()=>{}),j&&Me(D.id,u(),d),await d.invalidateQueries({queryKey:["brands"]}),n||t(D.id),f("done")}catch(k){S(te(k,{action:"OnboardingWizard.scan"})),j&&A(u()),f("failed")}finally{_.current=!1}}}if(g==="done"){const j=E??i,k=Ee(j?Pe(j).pct:0,0,Ke.length-1);return e.jsxs("div",{className:"flex flex-col items-center gap-4 py-6 text-center",children:[j!=null&&j.logo_url?e.jsx(ie,{src:j.logo_url,alt:"",className:"h-16 w-16 rounded-xl border border-border bg-white object-contain p-1"}):e.jsx(se,{size:46,gradient:!0}),e.jsxs("div",{ref:T=>Ot(T==null?void 0:T.getBoundingClientRect()),className:"relative grid h-24 w-24 place-items-center",children:[e.jsx(Ie,{pct:k,size:96,stroke:7}),e.jsxs("span",{className:"absolute inset-0 grid place-items-center font-display text-lg font-semibold tabular-nums",children:[k,"%"]})]}),e.jsx("p",{className:"text-lg font-bold tracking-tight",children:We((j==null?void 0:j.name)??"your brand",k)}),e.jsx("p",{className:"max-w-md text-sm text-muted-foreground",children:k>=50?"Everything we found feeds every caption, image and idea we write. You can fine-tune it any time in the Brand Book.":"A thin profile still works, and every field you add in the Brand Book later makes the writing sharper."})]})}return g==="scanning"?e.jsxs("div",{className:"flex flex-col items-center gap-5 py-10 text-center",children:[e.jsx("p",{className:"text-sm font-medium",children:Se[v]}),e.jsx("div",{className:"h-1.5 w-64 overflow-hidden rounded-full bg-secondary",children:e.jsx("div",{className:"h-full w-1/3 animate-[shimmer_1.4s_ease-in-out_infinite] rounded-full bg-primary"})}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"This usually takes under a minute. Everything is saved as it lands."})]}
1):e.jsxs("div",{className:"mx-auto max-w-md space-y-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:c??"Welcome. Tell us about your brand"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"We scan your website and fill your Brand Book for you."})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(oe,{children:"Brand name"}),e.jsx(Oe,{autoFocus:!0,value:h,onChange:j=>b(j.target.value),placeholder:"My brand name"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(oe,{children:"Website"}),e.jsxs("div",{className:"flex h-10 w-full items-center rounded-md border border-input bg-background ring-offset-background focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2",children:[e.jsx("span",{className:"ms-2.5 grid h-6 w-6 shrink-0 place-items-center text-muted-foreground",children:C&&!O?e.jsx("img",{src:`https://${C}/favicon.ico`,alt:"",className:"h-5 w-5 rounded",onError:()=>$(!0)},C):e.jsx(Qe,{size:15})}),e.jsx("span",{className:"select-none ps-1.5 text-sm text-muted-foreground",children:"https://"}),e.jsx("input",{value:p,onChange:j=>y(j.target.value.replace(/^https?:\/\//i,"")),placeholder:"mybrand.com",className:"h-full w-full bg-transparent pe-3 text-sm placeholder:text-muted-foreground focus:outline-none"})]})]}),r&&e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(oe,{children:"Instagram"}),e.jsxs("div",{className:"flex h-10 w-full items-center rounded-md border border-input bg-background ring-offset-background focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2",children:[e.jsx("span",{className:"ms-2.5 grid h-6 w-6 shrink-0 place-items-center text-muted-foreground",children:e.jsx(fe,{size:15})}),e.jsx("span",{className:"select-none ps-1.5 text-sm text-muted-foreground",children:"instagram.com/"}),e.jsx("input",{value:M,onChange:j=>z(j.target.value),placeholder:"mybrand",className:"h-full w-full bg-transparent pe-3 text-sm placeholder:text-muted-foreground focus:outline-none"})]})]}),g==="failed"&&e.jsx("p",{className:"text-sm text-red-600",children:N})]})}function Lt({brandId:t}){const n=X(),{data:l=[]}=mt(),[s,r]=a.useState(()=>!!ge(t));a.useEffect(()=>{const p=ge(t);if(!p)return;r(!0);let y=!0;return p.finally(()=>{y&&r(!1)}),()=>{y=!1}},[t]);const[c,m]=a.useState(!1),[i,d]=a.useState(!1),h=async p=>{const{error:y}=await P.from("products").delete().eq("id",p);if(y){Ue(y,{action:"OnboardingWizard.removeProduct"});return}n.invalidateQueries({queryKey:["products",t]})},b=[...Nt,{label:"Import from a product feed",menuLabel:"Product feed",onSelect:()=>m(!0)},{label:"Import from CSV",menuLabel:"CSV",onSelect:()=>d(!0)}];return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:"Import your products"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"Real products make real posts: names, photos and selling points feed the writer."})]}),e.jsx(wt,{label:"Import",actions:b,brandId:t,onDone:()=>n.invalidateQueries({queryKey:["products",t]})})]}),e.jsx(St,{adding:c,onAddingChange:m}),e.jsx(_t,{open:i,onOpenChange:d}),s&&e.jsxs("p",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(G,{})," Importing products we found on your siteâ¦"]}),l.length>0&&e.jsxs("p",{className:"text-sm text-green-600",children:[l.length," product",l.length===1?"":"s"," ready."]}),l.length===0&&!s?e.jsx("p",{className:"rounded-xl border border-dashed border-border p-6 text-center text-sm text-muted-foreground",children:"Nothing imported yet. Pick an import option above, or press Skip."}):e.jsxs(xe,{children:[l.map(p=>e.jsx(he,{title:p.name,media:p.primary_image_url?e.jsx(ie,{src:p.primary_image_url,alt:"",className:"h-full w-full object-cover"}):e.jsx(kt,{size:22,className:"text-muted-foreground"}),action:e.jsx("button",{type:"button",title:"Remove",onClick:()=>h(p.id),className:"grid h-6 w-6 place-items-center rounded-full bg-card/90 text-muted-foreground shadow-sm transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",children:e.jsx(Fe,{size:13})})},p.id)),s&&[0,1,2,3].map(p=>e.jsx(ae,{},`sk-${p}`))]})]})}const ue=new Map,$t=t=>{let n=ue.get(t);return n||(n=Re.invoke({brandId:t}),ue.set(t,n),n.catch(()=>{}).finally(()=>ue.delete(t))),n};function Kt({brandId:t,marker:n,onDone:l,onBusy:s}){const r=X(),{data:c=[]}
1=ft(),[m,i]=a.useState(!1),[d,h]=a.useState(""),[b,p]=a.useState(!1),y=a.useRef(!1);a.useEffect(()=>(s==null||s(m||b),()=>s==null?void 0:s(!1)),[m,b]);const M=()=>r.invalidateQueries({queryKey:["competitors",t]}),z=async({shared:f=!1}={})=>{i(!0);try{await(f?$t(t):Re.invoke({brandId:t}))}catch{}finally{i(!1),M()}};a.useEffect(()=>{var f;y.current||(y.current=!0,(f=n==null?void 0:n.done)!=null&&f.competitors||(l("competitors"),z({shared:!0})))},[]);const g=async()=>{if(d.trim()){p(!0);try{await Et.invoke({brandId:t,input:d.trim()}),h(""),M()}catch{}finally{p(!1)}}};return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:"Know your competitors"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"They become market context for the writer and profiles you can draw inspiration from."})]}),e.jsx(Pt,{label:m?"Finding competitorsâ¦":"Find competitors for me",align:"end",children:e.jsx("button",{type:"button",onClick:()=>z(),disabled:m,"aria-label":"Find competitors for me",className:"grid h-8 w-8 shrink-0 place-items-center rounded-full bg-card text-muted-foreground shadow-sm transition-colors hover:bg-background hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-40",children:m?e.jsx(G,{}):e.jsx(zt,{size:15,fill:"currentColor"})})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Oe,{className:"w-full sm:w-80",placeholder:"https://competitor.com",value:d,disabled:b,onChange:f=>h(f.target.value),onKeyDown:f=>f.key==="Enter"&&g()}),e.jsx(U,{variant:"outline",size:"sm",disabled:b||!d.trim(),onClick:g,children:b?e.jsxs(e.Fragment,{children:[e.jsx(G,{})," Addingâ¦"]}):"Add"})]}),m&&c.length===0&&e.jsxs("p",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(G,{})," Finding competitors for youâ¦"]}),(c.length>0||b||m)&&e.jsxs(xe,{children:[c.map(f=>e.jsx(he,{title:f.name,subtitle:f.website?H(f.website):null,media:f.website?e.jsx(Ve,{host:H(f.website),size:56,className:"h-14 w-14 object-contain"}):e.jsx(Ct,{size:22,className:"text-muted-foreground"})},f.id)),m&&c.length===0&&[0,1,2,3].map(f=>e.jsx(ae,{},`sk-${f}`)),b&&e.jsx(ae,{})]})]})}const me=new Map;async function Wt(t){let n=me.get(t);return n||(n=(async()=>{await J("generate-brand-voice",{brand_id:t,preset:"professional"}),await J("generate-brand-voice",{brand_id:t,preset:"friendly"})})(),me.set(t,n),n.catch(()=>{}).finally(()=>me.delete(t))),n}function Qt({brandId:t,marker:n,onDone:l,onBusy:s}){const r=X(),{data:c=[]}
1=pt(),[m,i]=a.useState(!1),[d,h]=a.useState(!1),b=a.useRef(!1);a.useEffect(()=>(s==null||s(d),()=>s==null?void 0:s(!1)),[d]);const p=async()=>{i(!1),h(!0),l("voices");try{await Wt(t)}catch{i(!0)}finally{h(!1),r.invalidateQueries({queryKey:["brand_voices",t]})}};return a.useEffect(()=>{var y;b.current||(b.current=!0,(y=n==null?void 0:n.done)!=null&&y.voices||p())},[]),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:"Your brand voices"}),e.jsx("p",{className:"mt-1 text-sm text-muted-foreground",children:"Two ways of sounding like you: a professional one and a friendly one. Captions are written in whichever you pick."})]}),d&&e.jsxs("p",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(G,{})," Writing your two voices, about a minuteâ¦"]}),m&&!d&&e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("p",{className:"text-sm text-red-600",children:"Voice creation hit a snag."}),e.jsx(U,{variant:"outline",size:"sm",onClick:p,children:"Try again"})]}),(c.length>0||d)&&e.jsxs(xe,{children:[c.map(y=>e.jsx(he,{title:y.name,subtitle:y.writing_style||null,media:e.jsx(ie,{src:Ye(y),alt:"",className:"h-16 w-16 rounded-full"})},y.id)),d&&[0,1].slice(c.length).map(y=>e.jsx(ae,{},`sk-${y}`))]})]})}const Ft=["Publish and schedule posts straight to your account.","Your real engagement tells us your best times to post.","View and reply to comments without leaving the app."];function Ut({brandId:t,marker:n,onDone:l,registerPrimary:s}){var h;const c=((h=ze(t).data)==null?void 0:h.status)==="connected",m=a.useRef(!1),[i,d]=a.useState(!1);return a.useEffect(()=>{if(s){if(c){s(null);return}s({label:e.jsxs(e.Fragment,{children:[e.jsx(fe,{size:15})," Connect Instagram"]}),disabled:i,run:async()=>{d(!0);try{await It(t)}catch{d(!1)}}})}},[c,i]),a.useEffect(()=>()=>s==null?void 0:s(null),[]),a.useEffect(()=>{var b;!c||m.current||(b=n==null?void 0:n.done)!=null&&b.scanProfile||(m.current=!0,l("scanProfile"),qe(t).catch(()=>{}))},[c]),e.jsxs("div",{className:"mx-auto max-w-md space-y-4 text-center",children:[e.jsxs("div",{className:"flex items-center justify-center gap-3",children:[e.jsx("span",{className:"grid h-12 w-12 place-items-center rounded-full bg-foreground text-background",children:e.jsx(se,{size:26})}),c?e.jsx(ve,{size:18,"aria-hidden":"true",className:"text-brand"}):e.jsx(De,{size:18,"aria-hidden":"true",className:"text-muted-foreground"}),e.jsx("span",{className:"grid h-12 w-12 place-items-center rounded-full ig-gradient text-white",children:e.jsx(fe,{size:22})})]}),e.jsx("h1",{className:"text-xl font-bold tracking-tight",children:c?"Instagram connected":"Connect your Instagram"}),!c&&e.jsx("ul",{className:"mx-auto w-fit space-y-2.5 text-left",children:Ft.map(b=>e.jsxs("li",{className:"flex gap-2.5 text-sm",children:[e.jsx(ve,{size:16,className:"mt-0.5 shrink-0 text-brand"}),e.jsx("span",{children:b})]},b))}),(!s||c)&&e.jsx("div",{className:"flex justify-center",children:e.jsx(qt,{brandId:t})}),c?e.jsx("p",{className:"text-sm text-muted-foreground",children:"All set. Press Finish and we will create your first posts."}):e.jsx("p",{className:"text-xs text-muted-foreground",children:"You might need a Professional (Business or Creator) account. If you do, Instagram will guide you through: three more clicks, and nothing about your content changes. Finish there and come back."})]})}function Vt({onDone:t,onBusy:n,onCreated:l,onReset:s,onGo:r}){const{user:c}=re(),m=X(),[i,d]=a.useState("running"),[h,b]=a.useState(""),[p,y]=a.useState(null),[M,z]=a.useState({brand:null,igTemplates:null,products:null,competitors:null,voices:null}),g=a.useRef(!1);a.useEffect(()=>(n==null||n(i!=="result"),()=>n==null?void 0:n(!1)),[i]);const f=async()=>{var N,S;try{const w=Q(),A=(w==null?void 0:w.pending)??{},v=(A.name??"").trim()||je(A.url??"",A.instagram??"")||"My brand";let o=w==null?void 0:w.brandId;if(!o){const{data:L,error:C}=await we(`create:${c.id}:bare:${v}`,()=>P.from("brands").insert({name:v,user_id:c.id}).select().single());if(C)throw new Error(C.message);
1o=L.id,m.invalidateQueries({queryKey:["brands"]}),l(o)}(S=(N=Q())==null?void 0:N.done)!=null&&S.voices||(t("voices"),Ne(o).catch(()=>{}).finally(()=>m.invalidateQueries({queryKey:["brand_voices",o]}))),r("instagram")}catch(w){b(te(w,{action:"OnboardingWizard.scan"})),d("failed")}},E=(N,S,{typedHandle:w="",igPosts:A=[]}={})=>{var $;const v=N.id,o=()=>{var u;return((u=Q())==null?void 0:u.done)??{}};(($=Q())==null?void 0:$.version)===4&&Ae(v).catch(()=>{});const L=(async()=>{S&&!o().siteImport&&(t("siteImport"),Me(v,S,m)),await(ge(v)??Promise.resolve());const{data:u}=await P.from("products").select("id,name,primary_image_url").eq("brand_id",v).order("created_at");return u??[]})(),C=(async()=>{const u=w||Be(N.social_links);if(!u)return{rows:[],note:null};let _=null;if(o().igScan)await(le(`ig:${v}`)??Promise.resolve());else{t("igScan");try{A.length?await Promise.allSettled(A.slice(0,4).map(K=>gt(v,xt(K)))):await vt(v,u,{hasLogo:!!N.logo_url})}catch(K){_=_e(String((K==null?void 0:K.message)??""))||null}}m.invalidateQueries({queryKey:["brands"]});const{data:R}=await P.from("templates").select("id,name,image_url").eq("brand_id",v).order("created_at"),F=R??[];return!F.length&&!_&&(_=`We found @${u} but could not read its posts yet.`),{rows:F,note:_}})(),V=(async()=>{o().competitors?await(le(`detect:${v}`)??Promise.resolve()):(t("competitors"),await jt(v).catch(()=>{})),m.invalidateQueries({queryKey:["competitors",v]});const{data:u}=await P.from("competitors").select("id,name,website").eq("brand_id",v).order("created_at");return o().bible||(t("bible"),yt(v).catch(()=>{})),u??[]})(),O=(async()=>{o().voices?await(le(`voices:${v}`)??Promise.resolve()):(t("voices"),await Ne(v).catch(()=>{})),m.invalidateQueries({queryKey:["brand_voices",v]});const{data:u}=await P.from("brand_voices").select("*").eq("brand_id",v).order("created_at");return u??[]})();L.then(u=>z(_=>({..._,products:u}))).catch(()=>z(u=>({...u,products:[]}
1))),C.then(({rows:u,note:_})=>z(R=>({...R,igTemplates:u,igNote:_}))).catch(()=>z(u=>({...u,igTemplates:[]}))),V.then(u=>z(_=>({..._,competitors:u}))).catch(()=>z(u=>({...u,competitors:[]}))),O.then(u=>z(_=>({..._,voices:u}))).catch(()=>z(u=>({...u,voices:[]})))},W=async()=>{var A,v;d("running"),b(""),z({brand:null,igTemplates:null,products:null,competitors:null,voices:null});const N=((A=Q())==null?void 0:A.pending)??{},S=N.url?`https://${String(N.url).trim().replace(/^https?:\/\//i,"")}`:"",w=Ge(N.instagram);try{let o=null,L=[];const C=Q();if(C!=null&&C.brandId&&((v=C==null?void 0:C.done)!=null&&v.scan)){const{data:O}=await P.from("brands").select("*").eq("id",C.brandId).maybeSingle();o=O??null}if(!S&&!w&&!(o!=null&&o.domain))return f();const V=!!o&&!!S&&H(S)!==H(o.domain??"");if(!o||V){const O=S?{url:S}:{instagram_url:w},$=I=>we(`scan:${c.id}:${S||w}:${I}`,()=>J("scan-brand",O,{quiet:!0}));let u;try{u=await $(1)}catch(I){if((I==null?void 0:I.code)==="out_of_credits")throw I;u=await $(2)}const _=(u==null?void 0:u.fields)??{};if(L=(u==null?void 0:u.ig_posts)??[],V){await Promise.all(["products","competitors","brand_voices","knowledge_items"].map(I=>
1P.from(I).delete().eq("brand_id",o.id)));for(const I of["products","competitors","brand_voices","knowledge_items"])m.invalidateQueries({queryKey:[I,o.id]});s==null||s()}const R={..._,name:(N.name??"").trim()||_.name||je(S,w)||"My brand"};w&&(R.social_links=He(_.social_links??(o==null?void 0:o.social_links),w));const{data:F,error:K}=o?await P.from("brands").update(R).eq("id",o.id).select().single():await P.from("brands").insert({...R,user_id:c.id}).select().single();if(K)throw new Error(K.message);o=F,m.invalidateQueries({queryKey:["brands"]}),C!=null&&C.brandId||l(o.id),o.country&&Te(o.id).catch(()=>{})}else if((N.name??"").trim()&&o.name!==N.name.trim()){const{data:O}=await P.from("brands").update({name:N.name.trim()}).eq("id",o.id).select().single();O&&(o=O,m.invalidateQueries({queryKey:["brands"]}))}z(O=>({...O,brand:o})),E(o,S||(o.domain?`https://${o.domain}`:""),{typedHandle:w,igPosts:L})}catch(o){y((o==null?void 0:o.code)??null),b(_e(te(o,{action:"OnboardingWizard.scan"}))),d("failed")}};if(a.useEffect(()=>{g.current||(g.current=!0,W())},[]),i==="failed"){const N=p==="out_of_credits";return e.jsxs("div",{className:"mx-auto flex max-w-md flex-col items-center gap-4 py-8 text-center",children:[e.jsx("p",{className:"text-base font-semibold tracking-tight",children:N?"Out of credits":"We could not read your site"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:h}),e.jsx("p",{className:"text-xs text-muted-foreground",children:N?"The scan needs credits to run. Once your balance is topped up, Try again picks up right here.":"That is usually the site's own bot protection, nothing on your side. You can try again, or continue and fill the Brand Book whenever you like."}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(U,{variant:"brand",onClick:()=>W(),children:"Try again"}),e.jsx(U,{variant:"outline",onClick:f,children:"Continue with just the name"})]})]})}return e.jsx(bt,{results:M,onComplete:()=>d("result"),onGiveUp:()=>r("instagram")})}function _e(t){return String(t??"").replace(/\s+\u2014\s+/g,": ").replace(/\s*Try the Instagram option instead\.?/i,"").trim()}function Yt({brandId:t,onDone:n,marker:l,onBusy:s,onGo:r}){const{user:c}=re(),m=!!(c!=null&&c.is_anonymous),[i]=a.useState(()=>{var o;return!!((o=l==null?void 0:l.done)!=null&&o.firstPosts)}
1),[d,h]=a.useState(i),[b,p]=a.useState(null),[y,M]=a.useState(0),z=Bt(y,!d),g=Jt(Ht(t),!d),f=a.useRef(n);a.useEffect(()=>{f.current=n});const E=a.useRef(!1),W=a.useCallback(o=>{E.current||(E.current=!0,f.current("firstPosts"),p(o),h(!0))},[]),N=i&&!m,S=a.useRef(!1);a.useEffect(()=>{S.current||!N||(S.current=!0,r==null||r("instagram"))},[N]),a.useEffect(()=>(s==null||s(!d),()=>s==null?void 0:s(!1)),[d]);const w=d&&b===0,A=d?w?"Your brand is set up":"Your first posts are ready":"Making your first postsâ¦",v=m?w?"Sign in to keep your brand and start creating.":"Sign in to keep them, and the brand we just built for you.":"Saved to your account. Next, connect the Instagram they will publish to.";return N?e.jsx("div",{className:"grid min-h-[300px] place-items-center",children:e.jsx(G,{})}):e.jsxs("div",{className:"flex flex-col gap-6",children:[e.jsxs("div",{className:"text-center",children:[e.jsx("h2",{className:"text-xl font-bold tracking-tight",children:A}),d?e.jsx("p",{className:"mx-auto mt-2 max-w-md text-sm text-muted-foreground",children:v}):e.jsxs("div",{className:"mx-auto mt-3 w-full max-w-sm",children:[e.jsx(Xt,{text:g}),e.jsx("div",{className:"mt-3 h-1.5 w-full overflow-hidden rounded-full bg-secondary",role:"progressbar","aria-valuenow":y,"aria-valuemin":0,"aria-valuemax":100,children:e.jsx("div",{className:"h-full rounded-full bg-foreground transition-[width] duration-150 ease-linear",style:{width:`${z}%`}})})]})]}),i?e.jsx(Gt,{brandId:t,onCount:p}):e.jsx(Tt,{brandId:t,compact:!0,onAllDone:W,onProgress:M}),m&&e.jsx(Dt,{show:d})]})}function Dt({show:t}){const[n,l]=a.useState(!1),[s,r]=a.useState(""),[c,m]=a.useState(!1);async function i(){l(!0),r("");const{error:h}=await P.auth.linkIdentity({provider:"google",options:{redirectTo:window.location.origin}});if(h){if(l(!1),h.code==="identity_already_exists"||/already (exists|linked|registered)/i.test(h.message??"")){m(!0);return}r(te(h,{action:"StepPreview.link"}))}}async function d(){l(!0);const{error:h}=await P.auth.signInWithOAuth({provider:"google",options:{redirectTo:window.location.origin}});h&&(r(te(h,{action:"StepPreview.signInInstead"})),l(!1))}return t?c?e.jsxs("div",{className:"mx-auto max-w-sm text-center",children:[e.jsx("p",{className:"text-sm font-semibold",children:"You already have an account with that Google"}),e.jsx("p",{className:"mt-2 text-sm text-muted-foreground",children:"Signing in opens the brands you already have. What we just set up here is not carried over."}),e.jsxs("div",{className:"mt-4 flex justify-center gap-2",children:[e.jsx(U,{variant:"brand",disabled:n,onClick:d,children:"Sign in to my account"}),e.jsx(U,{variant:"outline",disabled:n,onClick:()=>m(!1),children:"Use a different Google"})]}),s&&e.jsx("p",{className:"mt-3 text-sm text-muted-foreground",children:s})]}):e.jsxs("div",{className:"mx-auto w-full max-w-sm text-center",children:[e.jsxs("button",{type:"button",onClick:i,disabled:n,className:"flex h-12 w-full items-center justify-center gap-3 rounded-full border border-border bg-card text-[15px] font-semibold text-foreground shadow-[0_14px_30px_-12px_rgba(28,27,25,0.35),0_4px_10px_-6px_rgba(28,27,25,0.25)] transition-all hover:-translate-y-px hover:shadow-[0_18px_36px_-12px_rgba(28,27,25,0.4),0_6px_12px_-6px_rgba(28,27,25,0.28)] active:translate-y-0 active:shadow-[0_8px_18px_-10px_rgba(28,27,25,0.3)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-60 disabled:shadow-none",children:[e.jsx(st,{}),n?"Opening Googleâ¦":"Continue with Google"]}),s&&e.jsx("p",{className:"mt-3 text-sm text-muted-foreground",children:s}),e.jsxs("p",{className:"mt-4 text-xs leading-relaxed text-muted-foreground",children:["Google is the only sign-in: nothing to verify, no password to lose. By continuing you agree to the"," ",e.jsx("a",{href:"/terms",target:"_blank",rel:"noreferrer",className:"underline hover:text-foreground",children:"Terms"})," and"," ",e.jsx("a",{href:"/privacy",target:"_blank",rel:"noreferrer",className:"underline hover:text-foreground",children:"Privacy Policy"}),"."]})]}):null}function Gt({brandId:t,onCount:n}){const[l,s]=a.useState(null);return a.useEffect(()=>{let r=!0;return P.from("posts").select("id, caption, image_url").eq("brand_id",t).order("created_at",{ascending:!1}).limit(5).then(c=>{if(r){const m=tt(c);s(m),n==null||n(m.length)}}).catch(()=>{r&&(s([]),n==null||n(0))}),()=>{r=!1}},[t]),l===null?e.jsx("div",{className:"grid min-h-[180px] place-items-center",children:e.jsx(G,{})}):l.length?e.jsx("div",{className:"grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-5",children:l.map(r=>e.jsx(At,{imageUrl:r.image_url,caption:r.caption,children:e.jsx("span",{className:"text-slate-300","aria-hidden":"true",children:e.jsx(se,{size:20})})},r.id))}):null}function Ht(t){const[n,l]=a.useState(et);return a.useEffect(()=>{if(!t)return;let s=!0;return Ae(t).then(r=>{s&&(r!=null&&r.length)&&l(r)}
1).catch(()=>{}),()=>{s=!1}},[t]),n}function Bt(t,n){const[l,s]=a.useState(0);return a.useEffect(()=>{if(!n)return;const r=setInterval(()=>s(c=>Je(c,t)),Xe);return()=>clearInterval(r)},[t,n]),l}function Jt(t,n){const[l,s]=a.useState(0);return a.useEffect(()=>{if(!n||t.length<2)return;const r=setInterval(()=>s(c=>c+1),Ze);return()=>clearInterval(r)},[n,t]),t[l%t.length]??""}function Xt({text:t}){const n=a.useRef(null);return a.useEffect(()=>{var s,r;const l=n.current;!l||!t||(r=(s=window.matchMedia)==null?void 0:s.call(window,"(prefers-reduced-motion: reduce)"))!=null&&r.matches||l.animate([{opacity:0,transform:"translateY(5px)"},{opacity:1,transform:"translateY(0)"}],{duration:420,easing:"cubic-bezier(.3,.9,.3,1)"})},[t]),e.jsx("p",{ref:n,"aria-live":"polite",className:"h-5 text-sm text-muted-foreground",children:t})}const ke=()=>dt()?3:ut()?2:4;function Ys({onExit:t}){var D;const n=nt(),l=X(),{user:s}=re(),{brands:r,activeBrand:c,setActiveBrandId:m}=Ce(),[i,d]=a.useState(()=>Q()),[h,b]=a.useState(!1),[p,y]=a.useState(null),[M,z]=a.useState(0),g=(i==null?void 0:i.step)??"brand",f=(i==null?void 0:i.brandId)??null,E=(i==null?void 0:i.version)??ke(),W=it(E),N=ot(E),S=W.indexOf(g);a.useEffect(()=>{if(i)return;const x={brandId:null,userId:(s==null?void 0:s.id)??null,step:"brand",done:{},version:ke()};ee(x),d(x)},[]);const w=a.useRef(null);a.useEffect(()=>{if(!i)return;const x=i.userId&&s&&i.userId!==s.id,q=i.brandId&&i.brandId!==w.current&&r.length>0&&!r.some(B=>B.id===i.brandId);(x||q)&&(ce(),d(null))},[i,r,s]);const A=x=>d(q=>{const B={...q,...x};return ee(B),B}),v=x=>{ye(x),d(Q())},o=x=>{w.current=x,m(x),l.invalidateQueries({queryKey:["brands"]}).then(()=>m(x));const q={...Q()??{},brandId:x,userId:(s==null?void 0:s.id)??null,step:E>=3?"setup":"brand",done:{scan:!0}};ee(q),d(q)},L=()=>{const x={...Q()??{},brandId:f,userId:(s==null?void 0:s.id)??null,step:E>=3?"setup":"brand",done:{scan:!0}};ee(x),d(x)},C=a.useCallback(x=>z(x),[]),V=a.useCallback(({name:x,url:q,instagram:B})=>{d(Le=>{const be={...Le??{},step:"setup",pending:{name:x,url:q,instagram:B}};return ee(be),be})},[]),O=()=>{var q;const x=Q();x&&!((q=x.done)!=null&&q.bible)&&(ye("bible"),J("generate-knowledge",{brand_id:x.brandId}).catch(()=>{}))},$=x=>{g==="competitors"&&x!=="competitors"&&O(),A({step:x}),b(!1)},u=()=>{g==="competitors"&&O(),ce(),f&&ct(i)&<({brandId:f}),t(),n("/",{replace:!0})},_=()=>{const x=W[S+1];x?$(x):u()},R=async()=>{f&&(await l.invalidateQueries({queryKey:["brands"]}),m(f)),ce(),t(),n("/brand",{replace:!0})},F=ze(f),K=((D=F.data)==null?void 0:D.status)==="connected",I=a.useRef(!1);a.useEffect(()=>{var x,q;E<3||g!=="instagram"||!K||I.current||(I.current=!0,(q=(x=Q())==null?void 0:x.done)!=null&&q.scanProfile||qe(f).catch(()=>{}),at("Instagram connected. Creating your first postsâ¦"),u())},[g,K]);const Y={brandId:f,onDone:v,marker:i,onBusy:b},Z=S===W.length-1,j=E===4&&g==="preview"&&!!(s!=null&&s.is_anonymous),k=!!(i!=null&&i.inApp),T=k&&f!==(c==null?void 0:c.id)?null:c;return e.jsxs("div",{children:[!k&&e.jsx(ht,{steps:W,labels:N,current:g,onGo:$,activeFrac:E>=3&&g==="brand"?M:null}),e.jsxs("div",{className:rt("mx-auto",k?"max-w-none":`mt-6 ${g==="preview"?"max-w-5xl":"max-w-3xl"} rounded-3xl border border-border bg-card p-5 shadow-[0_28px_64px_-24px_rgba(28,27,25,0.35),0_8px_20px_-10px_rgba(28,27,25,0.2)] sm:p-8`),children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsx(se,{size:30,className:"text-foreground"}),E<3&&f&&g!=="brand"&&e.jsx(Zt,{brand:c,pct:Ee(c?Pe(c).pct:0,S,W.length-1)})]}),e.jsxs("div",{className:"mt-5 flex min-h-[380px] flex-col justify-center",children:[g==="brand"&&e.jsx(Mt,{onCreated:o,brandId:f,registerPrimary:y,onReset:L,heading:k?"Create a new brand":void 0,formFlow:E>=3?{initial:(i==null?void 0:i.pending)??{name:(T==null?void 0:T.name)??"",url:(T==null?void 0:T.domain)??"",instagram:""},onSubmit:V,onProgress:C}:null}),g==="preview"&&e.jsx(Yt,{...Y,onGo:$}),g==="setup"&&e.jsx(Vt,{onDone:v,onBusy:b,onCreated:o,onReset:L,onGo:$}),g==="products"&&e.jsx(Lt,{...Y}),g==="competitors"&&e.jsx(Kt,{...Y}),g==="voice"&&e.jsx(Qt,{...Y}),g==="instagram"&&(E>=3&&F.isPending?e.jsx("div",{className:"grid min-h-[300px] place-items-center text-muted-foreground",children:e.jsx(G,{})}):e.jsx(Ut,{...Y,registerPrimary:E>=3?y:void 0}))]}),e.jsxs("div",{className:"mt-6 flex items-center gap-1 border-t border-border pt-4",children:[k&&g==="setup"&&!h&&e.jsx(U,{variant:"ghost",size:"sm",className:"opacity-50 hover:opacity-100",onClick:R,children:"See Brand Book"}),e.jsx("div",{className:"ms-auto",children:p?e.jsxs(U,{variant:"brand",disabled:p.disabled,onClick:p.run,children:[p.label,p.chevron&&e.jsx(de,{size:15})]}):f&&!j?Z&&E>=3?e.jsx(U,{variant:"ghost",size:"sm",className:"opacity-50 hover:opacity-100",disabled:h,onClick:_,children:"Finish"}):e.jsx(U,{variant:"brand",disabled:h,onClick:_,children:Z?"Finish":k&&g==="setup"?e.jsxs(e.Fragment,{children:["Connect Instagram ",e.jsx(de,{size:15})]}):e.jsxs(e.Fragment,{children:["Next ",e.jsx(de,{size:15})]})}):null})]})]})]})}function Zt({brand:t,pct:n}){const l=a.useRef(null);return a.useLayoutEffect(()=>{var i,d;const s=Rt(),r=l.current;if(!s||!r||(d=(i=window.matchMedia)==null?void 0:i.call(window,"(prefers-reduced-motion: reduce)"))!=null&&d.matches)return;const c=r.getBoundingClientRect(),m=Math.max(.5,Math.min(4,s.height/c.height));r.animate([{transform:`translate(${s.left-c.left}px, ${s.top-c.top}px) scale(${m})`,opacity:.85},{transform:"translate(0, 0) scale(1)",opacity:1}],{duration:650,easing:"cubic-bezier(.3,.9,.3,1)"})},[]),e.jsxs("div",{ref:l,style:{transformOrigin:"top left"},className:"flex shrink-0 items-center gap-2 will-change-transform",children:[t!=null&&t.logo_url?e.jsx(ie,{src:t.logo_url,alt:"",className:"h-9 w-9 rounded-lg border border-border bg-white object-contain p-0.5"}):e.jsx("span",{className:"grid h-9 w-9 place-items-center rounded-lg bg-secondary text-foreground",children:e.jsx(se,{size:18})}),e.jsxs("span",{className:"relative grid h-10 w-10 place-items-center",children:[e.jsx(Ie,{pct:n,size:40,stroke:4}),e.jsxs("span",{className:"absolute inset-0 grid place-items-center text-[9px] font-semibold tabular-nums",children:[n,"%"]})]})]})}export{Ys 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.