PageSourceSearch

https://trybrain.io/assets/_authenticated.matrix-Bsg9T_AP.js

js trybrain.io collected 2026-09-24 15:05:23 UTC 55,908 bytes, 8 lines download raw bytes

1const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/matrix-cell-funnel-panel-BDHis3gY.js","assets/index-DHwBXtP2.js","assets/cell-funnel.functions-Dbhhzye-.js","assets/video-Dk6kj1q-.js"])))=>i.map(i=>d[i]);
2import{y as As,r as d,e as h,au as ks,f as z,s as at,t as c,K as S,u as Es,j as e,B as u,ar as he,bD as pe,N as nt,at as fe,a0 as $,aN as Xe,S as B,L as lt,ak as Ps,p as Ye,am as $t,an as Dt,a5 as Fs,ao as qt,ap as Rt,k as re,aq as Lt,h as X,I as Pe,aE as ie,ab as rt,ac as it,ad as ot,ae as dt,af as ct,$ as Je,i as Ze,cg as Fe,cU as Ms,as as Ot,bv as $s,bw as Ds,bx as qs,by as Rs,bz as Ls,bA as Os,bB as Ts,bC as zs,ay as Ks,_ as Vs,P as Qs}from"./index-DHwBXtP2.js";import{l as Gs}from"./offers.functions-Dc3UOd2k.js";import{l as Us,a as Is,r as Bs,t as Ws,m as Hs,b as Xs,g as Ys,p as Js,d as Zs,c as ea,u as ta,e as sa,f as aa,h as na,s as la,i as ra,j as ia}from"./ad-copy-matrix.functions-DhewWPu6.js";import{l as oa,g as da,s as ca,p as ua,a as ma,b as xa,D as ha}from"./delivery-mode-toggle-y8ecANON.js";import{l as pa}from"./meta-lead-forms.functions-CxK6jyFh.js";import{s as fa}from"./cell-funnel.functions-Dbhhzye-.js";import{p as ga,l as ja}from"./matrix-production.functions-CN_hUEY4.js";import{l as ba,r as va}from"./audiences.functions-CPh7aYFW.js";import{W as Me}from"./wand-sparkles-D5Yfc3-O.js";import{P as ya}from"./pause-CmDG-0pY.js";import{P as Na}from"./play-JDOE0zXN.js";import{S as _a}from"./square-1XZSgepy.js";import{L as Tt}from"./lock-DvLJ09EX.js";import{T as $e}from"./trophy-DxeX-OQt.js";import{C as wa}from"./chevron-left-B7lTa6YE.js";import{L as Ca}from"./lock-open-DX37SBx9.js";import{R as zt}from"./rocket-fOKNrQg0.js";import"./layers-BTSX6eIt.js";const Sa=[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["path",{d:"m16 12-4-4-4 4",key:"177agl"}],["path",{d:"M12 16V8",key:"1sbj14"}]],Aa=As("circle-arrow-up",Sa);function ka(n=2){const[j,A]=d.useState({}),p=d.useRef([]),P=d.useRef(new Set),k=d.useRef(0),L=d.useCallback(()=>{for(;k.current<n&&p.current.length>0;){const w=p.current.shift();k.current++,A(m=>({...m,[w.id]:"running"})),Promise.resolve().then(()=>w.run()).catch(()=>{}).finally(()=>{k.current--,P.current.delete(w.id),A(m=>{if(!(w.id in m))return m;const C={...m};return delete C[w.id],C}),L()})}},[n]);return{enqueue:d.useCallback((w,m)=>{P.current.has(w)||(P.current.add(w),p.current.push({id:w,run:m}),A(C=>({...C,[w]:"queued"})),L())},[L]),statuses:j}}const oe=[{value:"LEARN_MORE",label:"Learn more"},{value:"SHOP_NOW",label:"Shop now"},{value:"SIGN_UP",label:"Sign up"},{value:"SUBSCRIBE",label:"Subscribe"},{value:"BOOK_TRAVEL",label:"Book now"},{value:"GET_QUOTE",label:"Get quote"},{value:"CONTACT_US",label:"Contact us"},{value:"DOWNLOAD",label:"Download"},{value:"APPLY_NOW",label:"Apply now"},{value:"GET_OFFER",label:"Get offer"},{value:"ORDER_NOW",label:"Order now"},{value:"WATCH_MORE",label:"Watch more"},{value:"SEE_MENU",label:"See menu"}],De="LEARN_MORE",Ea=d.lazy(()=>Vs(()=>import("./matrix-cell-funnel-panel-BDHis3gY.js"),__vite__mapDeps([0,1,2,3])).then(n=>({default:n.MatrixCellFunnelPanel}))),qe=["unaware","problem_aware","solution_aware","product_aware","most_aware"],Y=["problem_agitation","curiosity","contrarian","pattern_interrupt","social_proof","transformation","mechanism","big_promise"],et={unaware:"Unaware",problem_aware:"Problem",solution_aware:"Solution",product_aware:"Product",most_aware:"Most"},tt={problem_agitation:"Problem/Agitation",curiosity:"Curiosity",contrarian:"Contrarian",pattern_interrupt:"Pattern Interrupt",social_proof:"Social Proof",transformation:"Transformation",mechanism:"Mechanism",big_promise:"Big Promise"},st=/\/architect(\/|$|\?)/i;function Pa({label:n,hint:j,checked:A,onCheckedChange:p}){return e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"text-xs font-medium",children:n}),j&&e.jsx("p",{className:"text-[11px] text-muted-foreground leading-snug",children:j})]}),e.jsx(Ks,{checked:A,onCheckedChange:p})]})}function Fa({workspaceId:n,offerId:j}){const A=h(Us);h(Is);const p=h(Bs),P=h(Ws),k=h(Hs),L=h(Xs),V=h(Ys),w=h(Js),m=h(Zs),C=h(ea),x=ks(),O=ka(2),de=(t,
2s)=>`regen::${t}::${s}`,E=z({queryKey:["matrix-rebuild-status",n,j],queryFn:async()=>{const{data:t}=await at.auth.getSession(),s=t.session?.access_token??"",a=j?`&offerId=${j}`:"";return(await(await fetch(`/api/public/hooks/matrix-rebuild-status?workspaceId=${n}${a}`,{headers:s?{authorization:`Bearer ${s}`}:{}})).json().catch(()=>({}))).run??{status:"idle"}},enabled:!!n,refetchInterval:t=>t.state.data?.status==="running"?3e3:!1}),N=E.data?.status==="running",Le=E.data?.status==="paused",ge=E.data?.status==="failed"?E.data.error?.trim()||"Generation failed":null,ut=ge?[`Workspace: ${n}`,j?`Offer: ${j}`:null,E.data?.completed_at?`Completed: ${E.data.completed_at}`:null,"Status: failed",`Error: ${ge}`].filter(Boolean).join(`
3`):null,Oe=d.useRef(0);d.useEffect(()=>{const t=E.data;if(!t||t.status!=="running"){Oe.current=0;return}const s=t.cells??0;s>Oe.current&&(Oe.current=s,n&&x.invalidateQueries({queryKey:["ad-copy-matrix",n]}))},[E.data?.cells,E.data?.status,n,x]);const Te=d.useRef(null),mt=d.useRef(!1);d.useEffect(()=>{const t=E.data;if(!(!t||!t.completed_at)){if(!mt.current){mt.current=!0,Te.current=t.completed_at;return}Te.current!==t.completed_at&&(Te.current=t.completed_at,t.status==="succeeded"?(t.has_saved_rows&&!t.cells?c.success(`Generation skipped — keeping ${t.saved_rows??g.length} saved rows`):c.success(`Generated ${t.cells??40} matrix cells`),n&&x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),It(s=>s+1)):t.status==="failed"&&c.error(t.error||"Generation failed"))}},[E.data?.completed_at,E.data?.status]);const y=S({mutationFn:async({data:t})=>{const{data:s}=await at.auth.getSession(),a=s.session?.access_token??"",i=await fetch("/api/public/hooks/run-copy-matrix",{method:"POST",headers:{"content-type":"application/json",...a?{authorization:`Bearer ${a}`}:{}},body:JSON.stringify({workspaceId:t.workspaceId,offerId:j,action:t.action??"start"})}),o=await i.json().catch(()=>({}));if(!i.ok&&i.status!==202)throw new Error(o.error||`Matrix regen failed (${i.status})`);return{...o,_action:t.action??"start"}},onSuccess:t=>{const s=t._action??"start";s==="pause"?c.info("Generation paused"):s==="resume"?c.info("Generation resumed"):s==="stop"&&c.info("Generation stopped"),E.refetch()},onError:t=>{c.error(t.message||"Generation failed to start")}}),{data:ze,isLoading:Kt,error:xt}=z({queryKey:["ad-copy-matrix",n,j],queryFn:()=>A({data:{workspaceId:n,offerId:j}}),enabled:!!n,retry:3,retryDelay:t=>Math.min(1e3*2**t,4e3)}),Vt=h(oa),ht=z({queryKey:["matrix-failed-cells",n],queryFn:()=>Vt({data:{workspaceId:n}}),enabled:!!n,refetchInterval:15e3}),Qt=d.useMemo(()=>new Set(ht.data?.cellIds??[]),[ht.data?.cellIds]),g=ze?.rows??[],Gt=ze?.hasEvidence??!1,[Ut,It]=d.useState(0),pt=d.useMemo(()=>{const t=new Map;for(const s of g)t.set(`${s.awareness_level}::${s.format_type}`,s);return t},[g]),[R,je]=d.useState(null),l=R?pt.get(`${R.awareness}::${R.format}`)??null:null,[ft,gt]=d.useState(new Set),[Bt,be]=d.useState(!1),ve=t=>{if(!R)return;const s=qe.indexOf(R.awareness),a=Y.indexOf(R.format);if(s<0||a<0)return;const i=qe.length*Y.length,f=(s*Y.length+a+t+i)%i,F=qe[Math.floor(f/Y.length)],q=Y[f%Y.length];je({awareness:F,format:q})},[b,ce]=d.useState(new Set),Wt=d.useMemo(()=>g.map(t=>t.id).sort().join("|"),[g]);d.useEffect(()=>{ce(t=>{if(t.size===0)return t;const s=new Set;for(const a of g)t.has(a.id)&&s.add(a.id);return s.size===t.size?t:s})},[Wt]);const Ht=t=>{ce(s=>{const a=new Set(s);return a.has(t)?a.delete(t):a.add(t),a})},Xt=t=>b.has(t),ye=S({mutationFn:t=>p({data:{workspaceId:n,awarenessLevel:t.awareness,formatType:t.format,offerId:j}}),onMutate:t=>{gt(s=>{const a=new Set(s);return a.add(`${t.awareness}::${t.format}`),a})},onSuccess:()=>{c.success("Cell generated"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})},onError:t=>c.error(t.message),onSettled:(t,s,a)=>{gt(i=>{const o=new Set(i);return o.delete(`${a.awareness}::${a.format}`),o})}}),Yt=S({mutationFn:t=>P({data:t}),onSuccess:()=>x.invalidateQueries({queryKey:["ad-copy-matrix",n]})}),Jt=S({mutationFn:t=>k({data:t}),onSuccess:()=>{c.success("Marked as winner — cell locked"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})}}),se=S({mutationFn:t=>C({data:{workspaceId:n,cellIds:t}}),onSuccess:t=>{c.success(`Deleted ${t?.deleted??0} cell${(t?.deleted??0)===1?"":"s"}`),ce(new Set),je(null),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})},onError:t=>c.error(t.message)}),ue=z({queryKey:["ad-copy-matrix-variations",l?.id],queryFn:()=>L({data:{cellId:l.id}}),enabled:!!l?.id}),[J,jt]=d.useState(()=>new Set);J.size>0&&J.values().next().value;const Ke=S({mutationFn:t=>as({data:{workspaceId:n,cellId:t.cellId,...t.actorId?{actorId:t.actorId}:{}}
3}),onMutate:t=>{const s=`${t.cellId}::${t.actorId??t.slotKey??"_solo"}`;return jt(a=>{const i=new Set(a);return i.add(s),i}),{key:s}},onSuccess:()=>{c.success("Video script generated"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})},onError:t=>c.error(t.message),onSettled:(t,s,a)=>{const i=`${a.cellId}::${a.actorId??a.slotKey??"_solo"}`;jt(o=>{if(!o.has(i))return o;const f=new Set(o);return f.delete(i),f})}}),Z=S({mutationFn:t=>V({data:{workspaceId:n,cellId:t.cellId,count:4,doubleDown:!!t.doubleDown}}),onSuccess:(t,s)=>{c.success(s.doubleDown?`Generated ${t.generated} double-down iterations`:`Generated ${t.generated} variations`),x.invalidateQueries({queryKey:["ad-copy-matrix-variations",l?.id]})},onError:t=>c.error(t.message)}),bt=S({mutationFn:t=>w({data:{variationId:t}}),onSuccess:()=>{c.success("Variation promoted — now the primary copy"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),x.invalidateQueries({queryKey:["ad-copy-matrix-variations",l?.id]})},onError:t=>c.error(t.message)}),Zt=S({mutationFn:t=>m({data:{variationId:t}}),onSuccess:()=>{x.invalidateQueries({queryKey:["ad-copy-matrix-variations",l?.id]})}}),es=h(ta),ts=h(sa),ss=h(aa),as=h(na),[ns,Ne]=d.useState(!1),[_,Q]=d.useState(null),[ls,_e]=d.useState(null),[D,W]=d.useState(null),[rs,me]=d.useState(!1),[U,ee]=d.useState(null),is=()=>{if(!l)return;const t=l.cta_label??"",s=oe.some(a=>a.value===t)?t:De;Q({headline:l.headline??"",primary_text:l.primary_text??"",description:l.description??"",cta_label:s,image_sub_text:l.image_brief?.sub_text??"",source_phrase:l.source_phrase??""}),Ne(!0)},os=()=>{Ne(!1),Q(null)};d.useEffect(()=>{Ne(!1),Q(null),_e(null),W(null),me(!1),ee(null)},[l?.id]);const Ve=S({mutationFn:t=>es({data:{cellId:t.cellId,headline:t.headline,primary_text:t.primary_text,description:t.description||null,cta_label:t.cta_label,image_sub_text:t.image_sub_text,source_phrase:t.source_phrase}}),onSuccess:()=>{c.success("Ad copy saved — cell locked"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),Ne(!1),Q(null)},onError:t=>c.error(t.message)}),vt=S({mutationFn:t=>ts({data:{variationId:t.variationId,headline:t.headline,primary_text:t.primary_text,description:t.description||null,cta_label:t.cta_label}}),onSuccess:()=>{c.success("Variation saved"),x.invalidateQueries({queryKey:["ad-copy-matrix-variations",l?.id]}),_e(null),W(null)},onError:t=>c.error(t.message)}),Qe=S({mutationFn:t=>ss({data:{cellId:t.cellId,hook:t.hook,promise:t.promise,proof:t.proof,cta:t.cta,full_script:t.full_script,...t.actorId?{actorId:t.actorId}:{}}}),onSuccess:()=>{c.success("Video script saved — cell locked"),x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),me(!1),ee(null)},onError:t=>c.error(t.message)}),ds=()=>{if(!l)return;const t=l.video_script??{};ee({hook:t.hook??"",promise:t.promise??"",proof:t.proof??"",cta:t.cta??"",full_script:t.full_script??""}),me(!0)},cs=t=>{ee({hook:t.hook??"",promise:t.promise??"",proof:t.proof??"",cta:t.cta??"",full_script:t.full_script??"",actorId:t.actor_id,actorName:t.actor_name??null}),me(!0)},us=()=>{me(!1),ee(null)},ms=h(la);S({mutationFn:t=>ms({data:t}),onMutate:t=>{x.setQueriesData({queryKey:["ad-copy-matrix",n]},s=>s&&{...s,rows:s.rows.map(a=>a.id===t.cellId?{...a,produce_video:t.produceVideo,produce_image:t.produceImage}:a)})},onSuccess:()=>{x.invalidateQueries({queryKey:["ad-copy-matrix",n]})},onError:t=>{c.error(t.message),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})}});const xs=h(ra),hs=h(ia),ps=S({mutationFn:t=>hs({data:t}),onMutate:t=>{x.setQueriesData({queryKey:["ad-copy-matrix",n]},s=>s&&{...s,rows:s.rows.map(a=>a.id===t.cellId?{...a,realism_style:t.realism}:a)})},onSuccess:()=>x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),onError:t=>{c.error(t.message),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})}});S({mutationFn:t=>xs({data:t}),onMutate:t=>{const s=["9:16","1:1","16:9"].filter(a=>t.aspects.includes(a));x.setQueriesData({queryKey:["ad-copy-matrix",n]},a=>a&&{...a,rows:a.rows.map(i=>i.id===t.cellId?{...i,aspects:s}:i)})},onSuccess:()=>{x.invalidateQueries({queryKey:["ad-copy-matrix",n]})},onError:t=>{c.error(t.message),x.invalidateQueries({queryKey:["ad-copy-matrix",n]})}});const yt=g.filter(t=>t.is_locked).length,Nt=g.filter(t=>t.linked_winner_ad_id).length,_t=g.filter(t=>t.confidence==="evidence-backed").length,we=ze?.performance??{},wt=xt instanceof Error?xt.message:null,fs=h(da);h(ca),h(ua);const Ct=h(ga),gs=h(ja),js=h(ma),bs=Es(),Ge=S({mutationFn:async t=>(await Ct({data:{workspaceId:n,cellIds:[t]}}),js({data:{workspaceId:n,cellId:t}})),onSuccess:(t,s)=>{c.success("Arcads jobs planned — opening Assembler"),bs({to:"/assembler",search:{cellId:s}})},onError:t=>c.error(t.message)}),St=z({queryKey:["matrix-production-status",n],queryFn:()=>gs({data:{workspaceId:n}}),enabled:!!n,refetchInterval:15e3}),At=St.data?.byCell??{};St.data?.metaAdAccount;const[I,Ce]=d.useState(!1),[Da,qa]=d.useState(null),[Se,Ue]=d.useState({campaignName:"",adAccountId:"",landingUrl:"",cta:"LEARN_MORE",countries:"US",budget:10,includeVariations:!0,saveDefaults:!0,ageMin:18,ageMax:65,gender:"all",savedAudienceColdIds:[],savedAudienceWarmIds:[],savedAudienceHotIds:[],excludedAudienceIds:[],splitByAwareness:!0,advantageAudience:!0,advantagePlacements:!0,manualPlacements:["facebook/feed","instagram/stream","instagram/explore_home"],useCbo:!0,objective:"OUTCOME_LEADS",leadCaptureMethod:"appointment",leadGenFormId:""}),[Ra,kt]=d.useState(null),ae=z({queryKey:["matrix-push-defaults",n],queryFn:()=>fs({data:{workspaceId:n}}),enabled:!!n&&I,staleTime:0,refetchOnMount:"always",refetchOnWindowFocus:!0});I&&(ae.isLoading||ae.isFetching);const vs=h(pa);z({queryKey:["meta-lead-forms",n],queryFn:()=>vs({data:{workspaceId:n}}),enabled:!!n&&I&&Se.objective==="OUTCOME_LEADS"&&Se.leadCaptureMethod==="leadform",staleTime:6e4});const ys=h(fa),K=d.useMemo(()=>(b.size===0?g.map(t=>
3t.id):Array.from(b)).slice().sort(),[b,g]),Et=K.join("|"),Ae=z({queryKey:["matrix-push-funnel-urls",n,Et],queryFn:()=>ys({data:{workspaceId:n,cellIds:K}}),enabled:!!n&&I&&K.length>0}).data??{},Ie=ae.data?.defaultLandingUrl??"",Pt=K.length===1?Ae[K[0]]?.url??null:null,Ft=d.useMemo(()=>{for(const t of K){const s=Ae[t]?.url;if(s)return s}return null},[K,Ae]),Mt=d.useMemo(()=>{for(const t of g){const s=t.landing_url??"";if(s&&s.startsWith("https://trybrain.io/lp/"))try{const a=new URL(s);return a.searchParams.delete("preview"),a.toString()}catch{return s}}return null},[g]);K.length>0&&K.every(t=>!!Ae[t]?.url),d.useEffect(()=>{if(!I)return;const t=Pt??Ft??Mt;t&&Ue(s=>{const a=s.landingUrl.trim(),i=!!Ie&&a===Ie,o=st.test(a);return a===t||a&&!i&&!o?s:{...s,landingUrl:t}})},[Ie,I,Pt,Ft,Mt]);const Ns=h(ba),_s=h(va),ws=z({queryKey:["meta-audiences",n],queryFn:()=>Ns({data:{workspaceId:n}}),enabled:!!n&&I}),Cs=h(xa);z({queryKey:["matrix-push-precheck",n,Et,Se.leadCaptureMethod],queryFn:()=>Cs({data:{workspaceId:n,cellIds:K,leadCaptureMethod:Se.leadCaptureMethod}}),enabled:!!n&&I&&K.length>0,staleTime:1e4}),(ws.data??[]).map(t=>({id:t.audience_meta_id,label:`${t.name} · ${t.audience_type}${t.approximate_count?` · ${Number(t.approximate_count).toLocaleString()}`:""}`})),S({mutationFn:()=>_s({data:{workspaceId:n}}),onSuccess:t=>{c.success(`Synced ${t.synced} audiences from Meta`),x.invalidateQueries({queryKey:["meta-audiences",n]})},onError:t=>c.error(t.message)}),d.useEffect(()=>{const t=ae.data;if(!t)return;const s=(t.accounts??[]).map(i=>i.id),a=i=>i&&s.includes(i)?i:t.adAccountId&&s.includes(t.adAccountId)?t.adAccountId:s.length>0?s[0]:t.adAccountId??"";Ue(i=>({...i,adAccountId:a(i.adAccountId),landingUrl:i.landingUrl||(st.test(t.defaultLandingUrl??"")?"":t.defaultLandingUrl||""),cta:t.defaultCta||i.cta,countries:(t.defaultCountries??[]).join(",")||i.countries,campaignName:i.campaignName||`Matrix push — ${new Date().toISOString().slice(0,10)}`,ageMin:t.defaultAgeMin??i.ageMin,ageMax:t.defaultAgeMax??i.ageMax,gender:t.defaultGenders&&t.defaultGenders.length===1?t.defaultGenders[0]:"all",savedAudienceColdIds:i.savedAudienceColdIds,savedAudienceWarmIds:i.savedAudienceWarmIds,savedAudienceHotIds:i.savedAudienceHotIds}))},[ae.data]);const ne=S({mutationFn:async()=>{const t=b.size===0?g.map(a=>a.id):Array.from(b);if(t.length===0)throw new Error("No cells selected");return{queued:(await Ct({data:{workspaceId:n,cellIds:t}})).queued,cellCount:t.length}},onSuccess:t=>{Ce(!1),kt(null),c.success(`Planned ${t.queued} cell${t.cellCount===1?"":"s"}. Open Assembler and click "Order video", "Generate funnel" or "Order VSL" on each row you want produced — nothing is rendered until you do.`),x.invalidateQueries({queryKey:["ad-copy-matrix",n]}),x.invalidateQueries({queryKey:["matrix-production-status",n]}),x.invalidateQueries({queryKey:["pipeline-rows",n]}),x.invalidateQueries({queryKey:["pipeline-queues",n]})},onError:t=>c.error(t.message)}),xe=(t,s=2)=>t==null||!Number.isFinite(Number(t))?null:Number(t).toFixed(s),Be=t=>{const s=xe(t==null?null:Number(t)*100,2);return s==null?null:`${s}%`},We=t=>{if(t==null)return null;const s=Number(t);return Number.isFinite(s)?s>=1e3?`$${(s/1e3).toFixed(1)}k`:`$${s.toFixed(0)}`:null};return n?e.jsxs("div",{className:"space-y-4",children:[ge&&e.jsx("div",{className:"rounded-md border border-destructive/30 bg-destructive/5 p-3 text-sm",children:e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"font-medium text-destructive",children:"Last matrix rebuild failed"}),e.jsx("p",{className:"mt-1 whitespace-pre-wrap break-words font-mono text-xs text-foreground",children:ge}),E.data?.completed_at&&e.jsx("p",{className:"mt-2 text-[11px] text-muted-foreground",children:new Date(E.data.completed_at).toLocaleString()})]}),e.jsxs(u,{size:"sm",variant:"outline",className:"gap-1.5",onClick:async()=>{if(ut)try{await pe(ut),c.success("Exact rebuild error copied")}catch{c.error("Couldn't copy the rebuild error")}},children:[e.jsx(he,{className:"h-3.5 w-3.5"}),"Copy error"]})]})}),Kt?e.jsx(nt,{className:"h-64 w-full"}):wt?e.jsxs("div",{className:"rounded-md border border-dashed bg-muted/30 p-6 text-sm",children:[e.jsx("p",{className:"font-medium",children:"Matrix couldn’t be loaded."}),e.jsx("p",{className:"mt-1 text-muted-foreground",children:wt})]}):g.length===0&&!Gt?e.jsxs("div",{className:"rounded-md border border-dashed bg-muted/30 p-6 text-sm",children:[e.jsx("p",{className:"font-medium",children:"Matrix needs evidence to generate ad copies."}),e.jsx("p",{className:"mt-1 text-muted-foreground",children:"Connect a Meta ad account so we can pull your winning ads, and upload a VOC file (customer reviews, support tickets, sales-call transcripts). Then re-run the Brain."}),e.jsxs(u,{size:"sm",variant:"outline",className:"mt-3 gap-1.5",disabled:!n||N||y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n,action:"start"}})},children:[e.jsx(fe,{className:$("h-3.5 w-3.5",(N||y.isPending)&&"animate-spin")}),N?"Generating…":"Generate matrix"]})]}):g.length===0&&!N&&!y.isPending?e.jsxs("div",{className:"rounded-md border border-dashed bg-muted/30 p-6 text-sm",children:[e.jsx("p",{className:"font-medium",children:"Brain hasn't generated the matrix yet."}),e.jsx("p",{className:"mt-1 text-muted-foreground",children:"If you've already run the Brain, the 40-cell matrix builds in the background after the brain finishes. You can also kick it off now."}),e.jsxs(u,{size:"sm",variant:"outline",className:"mt-3 gap-1.5",disabled:!n||N||y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n}})},children:[y.isPending||N?e.jsx(fe,{className:"h-3.5 w-3.5 animate-spin"}):e.jsx(Me,{className:"h-3.5 w-3.5"}),y.isPending?"Starting…":N?"Generating…":"Generate matrix now"]})]}):e.jsxs(e.Fragment,{children:[(N||y.isPending)&&e.jsxs("div",{className:"rounded-md border border-primary/30 bg-primary/5 p-3",children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs font-medium text-primary",children:[e.jsx(fe,{className:"h-3.5 w-3.5 animate-spin"}),y.isPending?"Starting matrix generation…":`Generating matrix… ${E.data?.cells??0}/40 cells`]}),(()=>{const t=E.data?.cells??0,s=Math.min(100,Math.round(t/40*100)),a=t===0;return e.jsx("div",{className:$("mt-2 h-1 w-full overflow-hidden rounded-full bg-primary/10",a&&"matrix-progress-indeterminate"),children:!a&&e.jsx("div",{className:"h-full rounded-full bg-primary/60 transition-all duration-500",style:{width:`${s}%`}})})})()]}),e.jsxs("div",{className:"mb-3 flex flex-wrap items-center gap-3 text-xs text-muted-foreground",children:[e.jsxs("span",{children:[g.length,"/40 cells filled"]}),e.jsx("span",{children:"·"}),e.jsxs("span",{className:"text-emerald-600",children:[_t," evidence-backed"]}),e.jsx("span",{children:"·"}),e.jsxs("span",{className:"text-amber-600",children:[g.length-_t," improvised"]}),yt>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"·"}),e.jsxs("span",{children:[yt," locked"]})]}),Nt>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"·"}),e.jsxs("span",{className:"text-emerald-600",children:[Nt," winners"]})]}),e.jsxs("div",{className:"ml-auto flex items-center gap-2",children:[e.jsx("span",{className:"text-[11px]",children:b.size===0?`All ${g.length} ads selected`:`${b.size} selected`}),e.jsx(u,{size:"sm",variant:"ghost",className:"h-7 px-2 text-[11px]",onClick:()=>ce(b.size===g.length?new Set:new Set(g.map(t=>
3t.id))),children:b.size===g.length?"Clear":"Select all"}),b.size>0&&e.jsxs(u,{size:"sm",variant:"outline",className:"h-7 gap-1.5 text-destructive hover:text-destructive",disabled:se.isPending,onClick:()=>be(!0),children:[e.jsx(Xe,{className:"h-3.5 w-3.5"}),"Delete ",b.size]}),e.jsxs(u,{size:"sm",variant:"outline",className:"h-7 gap-1.5",disabled:!n||N||y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n,action:"start"}})},children:[e.jsx(fe,{className:$("h-3.5 w-3.5",(N||y.isPending)&&"animate-spin")}),N?"Generating…":Le?"Restart":"Regenerate all"]}),N&&e.jsxs(u,{size:"sm",variant:"outline",className:"h-7 gap-1.5",disabled:y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n,action:"pause"}})},children:[e.jsx(ya,{className:"h-3.5 w-3.5"}),"Pause"]}),Le&&e.jsxs(u,{size:"sm",variant:"outline",className:"h-7 gap-1.5",disabled:y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n,action:"resume"}})},children:[e.jsx(Na,{className:"h-3.5 w-3.5"}),"Resume"]}),(N||Le)&&e.jsxs(u,{size:"sm",variant:"outline",className:"h-7 gap-1.5 text-destructive hover:text-destructive",disabled:y.isPending,onClick:()=>{n&&y.mutate({data:{workspaceId:n,action:"stop"}})},children:[e.jsx(_a,{className:"h-3.5 w-3.5"}),"Stop"]})]})]}),(()=>{const t=Object.values(At);let s=0,a=0,i=0,o=0,f=0;const F=3600*1e3,q=Date.now(),H=v=>{if(!v||v.status!=="completed")return!1;const r=v.meta_attach_status;if(r!==null&&r!=="uploading"&&r!=="pending")return!1;const G=v.created_at?Date.parse(v.created_at):NaN;return Number.isFinite(G)?q-G<F:!1};for(const v of t){const r=v.video?.status??v.image?.status;r&&(r==="queued"?s++:r==="processing"?a++:r==="failed"&&i++),(v.video?.meta_attach_status??v.image?.meta_attach_status??null)==="failed"&&f++,(H(v.video)||H(v.image))&&o++}const M=s+a,le=i>0||f>0;return M===0&&o===0&&!ne.isPending?null:e.jsxs("div",{className:"mb-2 flex items-center justify-between gap-2 rounded-md border border-border/60 bg-muted/30 px-3 py-1.5 text-[11px]",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(B,{className:$("h-3.5 w-3.5",(ne.isPending||M>0||o>0)&&"animate-pulse text-primary")}),e.jsx("span",{className:"text-muted-foreground",children:ne.isPending?"Handing off to Assembler…":"Cells in Assembler:"}),M>0&&e.jsxs("span",{children:[M," rendering"]}),o>0&&e.jsxs("span",{children:[o," uploading to Meta"]}),le&&e.jsxs("span",{className:"text-destructive",children:[i+f," failed"]})]}),e.jsx(lt,{to:"/assembler",className:"text-primary underline-offset-2 hover:underline",children:"View Assembler →"})]})})(),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full border-collapse text-xs",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-2 text-left font-medium text-muted-foreground",children:"Awareness ↓ / Format →"}),Y.map(t=>e.jsx("th",{className:"p-2 text-left font-medium text-muted-foreground whitespace-nowrap",children:tt[t]??t},t))]})}),e.jsx("tbody",{children:qe.map((t,s)=>e.jsxs("tr",{className:"border-t","data-no-hover":!0,children:[e.jsx("td",{className:"p-2 font-medium whitespace-nowrap",children:et[t]??t}),Y.map((a,i)=>{const o=pt.get(`${t}::${a}`),f=!o,F=!!o?.linked_winner_ad_id,q=!!o?.is_locked,H=o?.confidence==="evidence-backed",M=o?we[o.id]:void 0,le=o?At[o.id]:void 0,v=le?.video?.status,r=le?.image?.status,G=v==="completed"||r==="completed"?"Ready":v==="processing"||r==="processing"?"Rendering":v==="queued"||r==="queued"?"Queued":v==="failed"||r==="failed"?"Failed":null;return e.jsx("td",{className:"p-1 align-top",children:e.jsxs("div",{className:$("relative",N&&"matrix-tile-pending",!N&&"matrix-tile-intro"),style:y.isPending?void 0:{animationDelay:`${(s+i)*60}ms`},children:[!f&&e.jsx("div",{className:"absolute top-1 right-1 z-10",onClick:T=>T.stopPropagation(),children:e.jsx(Ps,{checked:Xt(o.id),onCheckedChange:()=>Ht(o.id),className:"h-3.5 w-3.5 bg-background","aria-label":"Include in CSV export"})}),!f&&Qt.has(o.id)&&e.jsx(lt,{to:"/assembler",onClick:T=>T.stopPropagation(),title:"Production failed — open Assembler to retry",className:"absolute top-1 left-1 z-10 flex h-2.5 w-2.5 items-center justify-center",children:e.jsxs("span",{className:"relative inline-flex h-2.5 w-2.5",children:[e.jsx("span",{className:"absolute inline-flex h-full w-full animate-ping rounded-full bg-destructive opacity-60"}),e.jsx("span",{className:"relative inline-flex h-2.5 w-2.5 rounded-full bg-destructive ring-2 ring-background"})]})}),!f&&Array.isArray(o.rule_violations)&&o.rule_violations.length>
30&&e.jsx("span",{className:"absolute bottom-1 left-1 z-10 inline-flex items-center gap-0.5 rounded bg-amber-500/90 px-1 py-0.5 text-[9px] font-semibold text-white",title:o.rule_violations.map(T=>`${T.kind}: ${T.detail}`).join(`
4`),children:"⚠ rule"}),e.jsx("button",{type:"button",onClick:()=>{if(f&&n){const T=de(t,a);O.enqueue(T,()=>ye.mutateAsync({awareness:t,format:a}))}else je({awareness:t,format:a})},disabled:f&&(ft.has(`${t}::${a}`)||!!O.statuses[de(t,a)]),className:$("flex h-28 w-full flex-col items-start justify-start gap-1 rounded-md border p-2 text-left text-[11px] transition",f&&!N&&"border-dashed border-border/60 bg-gradient-to-br from-muted/20 to-transparent text-muted-foreground hover:border-primary/40 hover:bg-muted/40",f&&N&&"border-dashed border-primary/30 bg-muted/20 text-muted-foreground overflow-hidden relative",!f&&!F&&!q&&H&&"border-emerald-500/30 bg-emerald-500/5 hover:bg-emerald-500/10",!f&&!F&&!q&&!H&&"border-amber-500/30 bg-amber-500/5 hover:bg-amber-500/10",!f&&!F&&q&&"border-slate-500/50 bg-slate-500/15 hover:bg-slate-500/25 ring-1 ring-slate-500/30",F&&"border-emerald-600/60 bg-emerald-500/15 hover:bg-emerald-500/25"),children:f?(()=>{const T=de(t,a),ke=O.statuses[T],Ee=N||!!ke||ft.has(`${t}::${a}`),He=`${et[t]??t} × ${tt[a]??a}`;if(Ee){const Ss=ke==="queued"?"Queued":N?He:"Generating…";return e.jsxs("div",{className:"flex h-full w-full flex-col justify-between gap-1.5 py-0.5",children:[e.jsx("div",{className:"matrix-skeleton-block h-2 w-3/4"}),e.jsxs("div",{className:"space-y-1",children:[e.jsx("div",{className:"matrix-skeleton-block h-1.5 w-full"}),e.jsx("div",{className:"matrix-skeleton-block h-1.5 w-5/6"})]}),e.jsx("div",{className:"truncate text-[9px] text-muted-foreground/80",children:Ss})]})}return e.jsxs("div",{className:"m-auto flex flex-col items-center gap-1 opacity-70",children:[e.jsx(B,{className:"h-3.5 w-3.5"}),e.jsx("span",{className:"text-[10px] font-medium",children:"Generate"})]})})():e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex w-full items-center gap-1",children:[H?e.jsx(Ye,{className:"h-3 w-3 text-emerald-600 shrink-0"}):e.jsx(B,{className:"h-3 w-3 text-amber-600 shrink-0"}),q&&e.jsx(Tt,{className:"h-3 w-3 shrink-0"}),F&&e.jsx($e,{className:"h-3 w-3 text-emerald-700 shrink-0"})]}),e.jsx("p",{className:"line-clamp-2 font-semibold leading-snug",children:o.headline}),e.jsx("p",{className:"line-clamp-2 text-muted-foreground",children:o.primary_text}),e.jsxs("div",{className:"mt-auto flex w-full items-center justify-between gap-1",children:[e.jsx("span",{className:"inline-flex rounded bg-background/60 px-1 py-0.5 text-[10px] font-medium",children:o.cta_label}),M?e.jsxs("span",{className:"flex items-center gap-1 text-[10px] font-medium text-emerald-700",children:[Be(M.ctr)&&e.jsx("span",{title:"CTR",children:Be(M.ctr)}),xe(M.roas)&&e.jsxs("span",{title:"ROAS",children:["·",xe(M.roas),"x"]}),We(M.spend_usd)&&e.jsxs("span",{title:"Spend",className:"text-muted-foreground",children:["·",We(M.spend_usd)]})]}):null]}),G?e.jsx("span",{className:"text-[9px] font-medium text-muted-foreground",title:"Creative production status",children:G}):null]})})]},`tile-${t}-${a}-${Ut}`)},a)})]},t))})]})})]}),e.jsx($t,{open:!!R,onOpenChange:t=>!t&&je(null),children:e.jsxs(Dt,{className:"max-w-[1400px] w-[96vw] sm:!max-w-[min(1400px,96vw)] flex flex-col max-h-[92vh] p-0",onKeyDown:t=>{const s=t.target,a=s?.tagName;a==="INPUT"||a==="TEXTAREA"||a==="SELECT"||s&&s.isContentEditable||(t.key==="ArrowLeft"?(t.preventDefault(),ve(-1)):t.key==="ArrowRight"&&(t.preventDefault(),ve(1)))},children:[e.jsx(u,{size:"icon",variant:"outline",className:"absolute -left-12 top-1/2 -translate-y-1/2 h-8 w-8 rounded-full border-2 border-border bg-background text-foreground shadow-md ring-1 ring-black/10 hover:bg-accent hover:text-accent-foreground",title:"Previous cell (←)",onClick:()=>ve(-1),children:e.jsx(wa,{className:"h-4 w-4"})}),e.jsx(u,{size:"icon",variant:"outline",className:"absolute -right-12 top-1/2 -translate-y-1/2 h-8 w-8 rounded-full border-2 border-border bg-background text-foreground shadow-md ring-1 ring-black/10 hover:bg-accent hover:text-accent-foreground",title:"Next cell (→)",onClick:()=>ve(1),children:e.jsx(Fs,{className:"h-4 w-4"})}),l&&e.jsx(u,{size:"icon",variant:"ghost",className:"absolute right-10 top-3 h-7 w-7",t
4itle:"Copy ad copy",onClick:()=>{const t=[`Headline: ${l.headline}`,`Primary text: ${l.primary_text}`,l.description?`Description: ${l.description}`:"",`CTA: ${l.cta_label}`].filter(Boolean).join(`
5`);pe(t),c.success("Copied to clipboard")},children:e.jsx(he,{className:"h-3.5 w-3.5"})}),e.jsxs(qt,{className:"px-6 pt-6 pb-2 shrink-0",children:[e.jsx(Rt,{className:"flex items-center gap-2",children:R&&e.jsxs(e.Fragment,{children:[et[R.awareness]," × ",tt[R.format],l?.confidence==="evidence-backed"?e.jsxs(re,{variant:"secondary",className:"ml-2 bg-emerald-500/10 text-emerald-700",children:[e.jsx(Ye,{className:"mr-1 h-3 w-3"})," Evidence-backed"]}):l?e.jsxs(re,{variant:"secondary",className:"ml-2 bg-amber-500/10 text-amber-700",children:[e.jsx(B,{className:"mr-1 h-3 w-3"})," Improvised"]}):null]})}),e.jsx(Lt,{children:l?.framework??"No copy yet — generate one for this cell."})]}),e.jsx("div",{className:"flex-1 overflow-y-auto px-6 py-2 min-h-0",children:l?e.jsxs("div",{className:"grid gap-5 text-sm xl:grid-cols-12 xl:items-start pb-2",children:[e.jsxs("div",{className:"space-y-3 xl:col-span-7 min-w-0",children:[ns&&_?e.jsxs("div",{className:"space-y-3 rounded-md border bg-muted/20 p-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:"Editing ad copy"}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Saving locks the cell so Brain regens won't overwrite."})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Hook (Call Out)"}),e.jsx(Pe,{value:_.headline,maxLength:400,onChange:t=>Q({..._,headline:t.target.value})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Meat (Value · What-Who-When)"}),e.jsx(ie,{value:_.primary_text,rows:6,maxLength:4e3,onChange:t=>Q({..._,primary_text:t.target.value})}),(()=>{const t=_.primary_text.length,s=t>=600&&t<=700,a=!s&&t>=550&&t<=750;return e.jsxs("p",{className:$("text-[10px] tabular-nums",s?"text-emerald-500":a?"text-amber-500":"text-destructive"),children:[t," / 600–700 chars",s?" ✓":a?" (close)":" (out of range)"]})})()]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Description (optional)"}),e.jsx(Pe,{value:_.description,maxLength:400,onChange:t=>Q({..._,description:t.target.value})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Image subtext (overlay line)"}),e.jsx(ie,{value:_.image_sub_text,rows:2,maxLength:200,onChange:t=>Q({..._,image_sub_text:t.target.value})}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Shows as the supporting paragraph on generated ad images. Leave blank to fall back to the Meat copy."})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Source phrase (VOC anchor)"}),e.jsx(ie,{value:_.source_phrase,rows:2,maxLength:400,onChange:t=>Q({..._,source_phrase:t.target.value})}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Verbatim customer line this cell is anchored on. Used as a softer secondary signal during image generation."})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"CTA label"}),e.jsxs(rt,{value:oe.some(t=>t.value===_.cta_label)?_.cta_label:De,onValueChange:t=>Q({..._,cta_label:t}),children:[e.jsx(it,{className:"h-9 text-xs",children:e.jsx(ot,{})}),e.jsx(dt,{children:oe.map(t=>e.jsx(ct,{value:t.value,className:"text-xs",children:t.label},t.value))})]}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:"Meta's button text. Only these values render on the ad."})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-1",children:[e.jsxs(u,{size:"sm",variant:"ghost",onClick:os,children:[e.jsx(Je,{className:"mr-1 h-3 w-3"})," Cancel"]}),e.jsxs(u,{size:"sm",onClick:()=>Ve.mutate({cellId:l.id,..._}),disabled:Ve.isPending||!_.headline.trim()||!_.primary_text.trim()||!_.cta_label.trim(),children:[e.jsx(Ze,{className:"mr-1 h-3 w-3"}),Ve.isPending?"Saving…":"Save"]})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"flex items-center justify-end",children:e.jsxs(u,{size:"sm",variant:"ghost",className:"h-7 gap-1 text-xs",onClick:is,children:[e.jsx(Fe,{className:"h-3 w-3"})," Edit copy"]})}),e.jsx(te,{label:"Hook (Call Out)",value:l.headline}),e.jsx(te,{label:"Meat (Value · What-Who-When)",value:l.primary_text,multiline:!0,targetRange:[600,700]}),l.description?e.jsx(te,{label:"Description",value:l.description}):null,l.image_brief?.sub_text?e.jsx(te,{label:"Image subtext",value:l.image_brief.sub_text,multiline:!0}):null,e.jsx(te,{label:"CTA",value:l.cta_label})]}),l.source_phrase?e.jsx(te,{label:"Source phrase",value:l.source_phrase,multiline:!0}):null,l.rationale?e.jsx(te,{label:"Rationale",value:l.rationale,multiline:!0}):null,l.references&&l.references.length>0?e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground mb-1",children:"References"}),e.jsx("ul",{className:"space-y-1",children:l.references.map((t,s)=>e.jsxs("li",{className:"text-xs",children:[e.jsx(re,{variant:"outline",className:"mr-2 text-[10px]",children:t.type}),t.snippet||t.id]},s))})]}):null]}),e.jsxs("div",{className:"space-y-3 xl:col-span-5 min-w-0",children:[e.jsx("div",{className:"rounded-md border bg-muted/20 p-3",children:e.jsx(Pa,{label:"Realistic look (no AI polish)",hint:"Adds a realism directive to image/video prompts: lower contrast, muted colour, phone-camera imperfection, handheld drift. Turn off for a polished cinematic look.",checked:l.realism_style!==!1,onCheckedChange:t=>ps.mutate({cellId:l.id,realism:t})})}),rs&&U?e.jsxs("div",{className:"rounded-md border border-violet-500/30 bg-violet-500/5 p-3 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("p",{className:"text-xs font-medium text-violet-700 flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3"})," Editing video script",U.actorId?e.jsxs("span",{className:"ml-1 text-[10px] text-muted-foreground",children:["· ",U.actorName??U.actorId]}):null]}),e.jsx("span",{className:"text-[10px] text-muted-foreground",children:"Saving locks the cell so Brain regens won't overwrite."})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Hook"}),e.jsx(ie,{value:U.hook,rows:2,maxLength:2e3,onChange:t=>ee({...U,hook:t.target.value})})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(X,{className:"text-xs",children:"Full script (sent to Arcads)"}),e.jsx(ie,{value:U.full_script,rows:8,maxLength:2e4,onChange:t=>ee({...U,full_script:t.target.value})})]}),e.jsxs("div",{className:"flex justify-end gap-2 pt-1",children:[e.jsxs(u,{size:"sm",variant:"ghost",onClick:us,children:[e.jsx(Je,{className:"mr-1 h-3 w-3"})," Cancel"]}),e.jsxs(u,{size:"sm",onClick:()=>Qe.mutate({cellId:l.id,...U}),disabled:Qe.isPending,children:[e.jsx(Ze,{className:"mr-1 h-3 w-3"}),Qe.isPending?"Saving…":"Save script"]})]})]}):(()=>{const s=(l.video_scripts??[]).filter(r=>!!r&&!!r.actor_id&&!!(r.hook||r.promise||r.proof||r.cta||r.full_script)),a=l.video_script,i=!!(a?.hook||a?.promise||a?.proof||a?.cta||a?.full_script),o=`add-slot-${s.length}`,f=`${l.id}::${o}`,F=J.has(f),q=s.length<3,H=s.length===0&&!i,M="slot-0",le=`${l.id}::${M}`,v=J.has(le);return e.jsxs("div",{className:"space-y-2",children:[H?e.jsx("div",{className:"rounded-md border border-dashed border-violet-500/30 bg-violet-500/5 p-4",children:e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsxs("div",{children:[e.jsxs("p",{className:"text-xs font-medium text-violet-700 flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3"})," Video script"]}),e.jsxs("p",{className:"text-[11px] text-muted-foreground mt-0.5",children:["Generate a script for this cell. You can add up to ",3," scripts across different actors."]})]}),e.jsxs(u,{size:"sm",variant:"outline",className:"h-8 gap-1",disabled:v,onClick:()=>Ke.mutate({cellId:l.id,slotKey:M}),children:[e.jsx(Me,{className:$("h-3 w-3",v&&"animate-pulse")}),v?"Generating…":"Generate script"]})]})}):e.jsxs("div",{className:"space-y-2",children:[s.map((r,G)=>{const T=`slot-${G}`,ke=`${l.id}::${T}`;J.has(ke);const Ee=r?.actor_name??r?.actor_id??null;return e.jsxs("div",{className:$("rounded-md border p-3 space-y-2 min-h-[148px]",r?"border-violet-500/30 bg-violet-500/5":"border-dashed border-violet-500/30 bg-violet-500/5"),children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("p",{className:"text-xs font-medium text-violet-700 flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3 shrink-0"})," Script ",G+1]}),Ee?e.jsx("p",{className:"truncate text-[10px] text-muted-foreground",children:Ee}):null]}),e.jsxs("div",{className:"flex items-center gap-1",children:[r?.hook_type?e.jsx(re,{variant:"outline",className:"text-[10px]",children:r.hook_type}):null,r?.duration_sec?e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:["~",r.duration_sec,"s"]}):null]})]}),r?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-1 text-xs",children:[r.hook?e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-muted-foreground",children:"Hook · "}),e.jsx("span",{children:r.hook})]}):null,r.cta?e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-muted-foreground",children:"CTA · "}),e.jsx("span",{children:r.cta})]}):null]}),e.jsxs("div",{className:"flex flex-wrap gap-1 pt-1",children:[e.jsxs(u,{size:"sm",variant:"ghost",className:"h-6 gap-1 px-2 text-[11px]",title:"Edit script",onClick:()=>cs(r),children:[e.jsx(Fe,{className:"h-3 w-3"})," Edit"]}),e.jsxs(u,{size:"sm",variant:"outline",className:"h-6 gap-1 px-2 text-[11px]",title:"Regenerate script",disabled:J.has(`${l.id}::${r.actor_id}`),onClick:()=>Ke.mutate({cellId:l.id,actorId:r.actor_id}),children:[e.jsx(Me,{className:"h-3 w-3"}),J.has(`${l.id}::${r.actor_id}`)?"Generating…":"Regenerate"]}),e.jsx(u,{size:"icon",variant:"ghost",className:"h-6 w-6",title:"Copy full script",onClick:()=>{const He=r.full_script||[r.hook&&`Hook: ${r.hook}`,r.promise&&`Promise: ${r.promise}`,r.proof&&`Proof: ${r.proof}`,r.cta&&`CTA: ${r.cta}
5`].filter(Boolean).join(`
6`);pe(He),c.success("Script copied")},children:e.jsx(he,{className:"h-3 w-3"})})]})]}):null]},r?.actor_id??`slot-${G}`)}),q&&e.jsx("div",{className:"flex justify-start",children:e.jsxs(u,{size:"sm",variant:"outline",className:"h-8 gap-1 border-dashed",disabled:F,onClick:()=>Ke.mutate({cellId:l.id,slotKey:o}),title:"Test the same ad across another Arc Ads actor",children:[e.jsx(Ms,{className:$("h-3 w-3",F&&"animate-pulse")}),F?"Generating…":`+ Add script for another actor (${s.length}/3)`]})})]}),i&&s.length===0&&a?e.jsxs("div",{className:"rounded-md border border-violet-500/30 bg-violet-500/5 p-3 space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("p",{className:"text-xs font-medium text-violet-700 flex items-center gap-1",children:[e.jsx(B,{className:"h-3 w-3"})," Existing script"]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(u,{size:"sm",variant:"ghost",className:"h-6 gap-1 px-2 text-[11px]",title:"Edit script",onClick:ds,children:[e.jsx(Fe,{className:"h-3 w-3"})," Edit"]}),e.jsx(u,{size:"icon",variant:"ghost",className:"h-6 w-6",title:"Copy full script",onClick:()=>{const r=a.full_script||[a.hook&&`Hook: ${a.hook}`,a.promise&&`Promise: ${a.promise}`,a.proof&&`Proof: ${a.proof}`,a.cta&&`CTA: ${a.cta}`].filter(Boolean).join(`
7`);pe(r),c.success("Script copied")},children:e.jsx(he,{className:"h-3 w-3"})})]})]}),a.hook?e.jsxs("div",{className:"text-xs",children:[e.jsx("span",{className:"font-medium text-muted-foreground",children:"Hook · "}),e.jsx("span",{children:a.hook})]}):null]}):null]})})(),l&&we[l.id]?e.jsxs("div",{className:"rounded-md border border-emerald-500/30 bg-emerald-500/5 p-3",children:[e.jsxs("p",{className:"text-xs font-medium text-emerald-700 mb-2 flex items-center gap-1",children:[e.jsx($e,{className:"h-3 w-3"})," Live performance (Meta)"]}),e.jsx("div",{className:"grid grid-cols-4 gap-2 text-xs",children:(()=>{const t=we[l.id];return e.jsxs(e.Fragment,{children:[e.jsx(Re,{label:"CTR",value:Be(t.ctr)}),e.jsx(Re,{label:"ROAS",value:xe(t.roas)?`${xe(t.roas)}x`:null}),e.jsx(Re,{label:"Spend",value:We(t.spend_usd)}),e.jsx(Re,{label:"Conv.",value:t.conversions!=null?String(t.conversions):null})]})})()}),e.jsxs("p",{className:"text-[10px] text-muted-foreground mt-2",children:["Auto-linked via ",e.jsxs("code",{className:"font-mono",children:["ACM-",l.id.replace(/-/g,"").slice(0,8)]}),' in the ad name. Use "Double down" to iterate on this winner.']})]}):null,e.jsxs("div",{className:"pt-3 border-t",children:[e.jsxs("p",{className:"text-xs font-medium text-muted-foreground mb-2",children:["Variations ",ue.data?.variations.length?`(${ue.data.variations.length})`:""]}),ue.isLoading?e.jsx(nt,{className:"h-16 w-full"}):ue.data?.variations.length?e.jsx("ul",{className:"space-y-2",children:ue.data.variations.map(t=>e.jsx("li",{className:"rounded-md border bg-card p-2 space-y-1",children:ls===t.id&&D?e.jsxs("div",{className:"space-y-2",children:[e.jsx(Pe,{value:D.headline,maxLength:400,placeholder:"Headline",onChange:s=>W({...D,headline:s.target.value})}),e.jsx(ie,{value:D.primary_text,rows:4,maxLength:4e3,placeholder:"Primary text",onChange:s=>W({...D,primary_text:s.target.value})}),e.jsx(Pe,{value:D.description,maxLength:400,placeholder:"Description (optional)",onChange:s=>W({...D,description:s.target.value})}),e.jsxs(rt,{value:oe.some(s=>s.value===D.cta_label)?D.cta_label:De,onValueChange:s=>W({...D,cta_label:s}),children:[e.jsx(it,{className:"h-8 text-xs",children:e.jsx(ot,{placeholder:"CTA"})}),e.jsx(dt,{children:oe.map(s=>e.jsx(ct,{value:s.value,className:"text-xs",children:s.label},s.value))})]}),e.jsxs("div",{className:"flex justify-end gap-1",children:[e.jsx(u,{size:"sm",variant:"ghost",className:"h-6 px-2 text-[10px]",onClick:()=>{_e(null),W(null)},children:e.jsx(Je,{className:"h-3 w-3"})}),e.jsx(u,{size:"sm",className:"h-6 px-2 text-[10px]",onClick:()=>vt.mutate({variationId:t.id,...D}),disabled:vt.isPending||!D.headline.trim()||!D.primary_text.trim()||!D.cta_label.trim(),children:e.jsx(Ze,{className:"h-3 w-3"})})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-1",children:[t.confidence==="evidence-backed"?e.jsxs(re,{variant:"secondary",className:"bg-emerald-500/10 text-emerald-700 text-[10px]",children:[e.jsx(Ye,{className:"mr-1 h-3 w-3"})," Evidence-backed"]}):e.jsxs(re,{variant:"secondary",className:"bg-amber-500/10 text-amber-700 text-[10px]",children:[e.jsx(B,{className:"mr-1 h-3 w-3"})," Improvised"]}),e.jsxs("span",{className:"ml-auto flex gap-1",children:[e.jsx(u,{size:"sm",variant:"ghost",className:"h-6 px-2 text-[10px]",onClick:()=>{pe([`Headline: ${t.headline}`,`Primary text: ${t.primary_text}`,`CTA: ${t.cta_label}`].join(`
8`)),c.success("Copied")},children:e.jsx(he,{className:"h-3 w-3"})}),e.jsx(u,{size:"sm",variant:"ghost",className:"h-6 px-2 text-[10px]",onClick:()=>{_e(t.id),W({headline:t.headline,primary_text:t.primary_text,description:t.description??"",cta_label:oe.some(s=>s.value===t.cta_label)?t.cta_label:De})},title:"Edit",children:e.jsx(Fe,{className:"h-3 w-3"})}),e.jsx(u,{size:"sm",variant:"ghost",className:"h-6 px-2 text-[10px]",onClick:()=>bt.mutate(t.id),disabled:bt.isPending,title:"Promote to primary",children:e.jsx(Aa,{className:"h-3 w-3"})}),e.jsx(u,{size:"sm",variant:"ghost",className:"h-6 px-2 text-[10px]",onClick:()=>Zt.mutate(t.id),children:e.jsx(Xe,{className:"h-3 w-3"})})]})]}),e.jsx("p",{className:"text-xs font-semibold",children:t.headline}),e.jsx("p",{className:"text-xs text-muted-foreground whitespace-pre-wrap",children:t.primary_text}),e.jsx("span",{className:"inline-flex rounded bg-background/60 px-1 py-0.5 text-[10px] font-medium",children:t.cta_label})]})},t.id))}):e.jsx("p",{className:"text-xs text-muted-foreground",children:"No variations yet. Generate alternates that pull from your VOC, winners, and conversion data."})]}),n?e.jsx(d.Suspense,{fallback:e.jsx("div",{className:"rounded-md border border-dashed bg-muted/20 p-3 text-xs text-muted-foreground",children:"Loading funnel…"}),children:e.jsx(Ea,{workspaceId:n,cellId:l.id,headlineHint:l.headline??void 0,ctaHint:l.cta_label??void 0,scriptHint:l.primary_text??void 0})}):null]})]}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"No copy exists for this cell yet."})}),e.jsxs(Ot,{className:"flex flex-wrap gap-2 sm:justify-between shrink-0 border-t px-6 py-4",children:[e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs(u,{size:"sm",variant:"outline",onClick:()=>R&&ye.mutate({awareness:R.awareness,format:R.format}),disabled:ye.isPending,children:[e.jsx(fe,{className:$("mr-2 h-3 w-3",ye.isPending&&"animate-spin")}),l?"Regenerate":"Generate"]}),l&&e.jsxs(u,{size:"sm",variant:"outline",onClick:()=>Yt.mutate({cellId:l.id,locked:!l.is_locked}),children:[l.is_locked?e.jsx(Ca,{className:"mr-2 h-3 w-3"}):e.jsx(Tt,{className:"mr-2 h-3 w-3"}),l.is_locked?"Unlock":"Lock"]}),l&&e.jsxs(u,{size:"sm",variant:"outline",onClick:()=>Jt.mutate({cellId:l.id,metaAdId:l.linked_winner_ad_id?null:"manual"}),children:[e.jsx($e,{className:"mr-2 h-3 w-3"}),l.linked_winner_ad_id?"Unmark winner":"Mark as winner"]}),l&&e.jsxs(u,{size:"sm",variant:"outline",onClick:()=>Z.mutate({cellId:l.id}),disabled:Z.isPending,children:[e.jsx(Me,{className:$("mr-2 h-3 w-3",Z.isPending&&"animate-pulse")}),Z.isPending?"Generating…":"Generate 4 variations"]}),l&&we[l.id]?e.jsxs(u,{size:"sm",variant:"default",onClick:()=>Z.mutate({cellId:l.id,doubleDown:!0}),disabled:Z.isPending,title:"Tight iterations on this winning ad — preserves the hook, varies the proof",children:[e.jsx($e,{className:$("mr-2 h-3 w-3",Z.isPending&&"animate-pulse")}),"Double down"]}):null,l&&e.jsxs(u,{size:"sm",variant:"outline",className:"text-destructive hover:text-destructive",disabled:se.isPending,onClick:()=>{ce(new Set([l.id])),be(!0)},title:"Delete this cell (and its variations + scripts)",children:[e.jsx(Xe,{className:"mr-2 h-3 w-3"}),"Delete cell"]})]}),l&&e.jsx("div",{className:"flex sm:justify-end",children:e.jsxs(u,{size:"sm",onClick:()=>Ge.mutate(l.id),disabled:Ge.isPending,title:"Plan an Advantage+ bundle for this cell and open the Assembler to order assets",children:[e.jsx(zt,{className:"mr-2 h-3 w-3"}),Ge.isPending?"Planning…":"Plan in Assembler"]})})]})]})}),e.jsx($t,{open:I,onOpenChange:t=>{t&&Ue(s=>st.test(s.landingUrl)?{...s,landingUrl:""}:s),Ce(t),t||kt(null)},children:e.jsxs(Dt,{className:"max-w-md",children:[e.jsxs(qt,{children:[e.jsx(Rt,{children:"Send to Assembler"}),e.jsx(Lt,{children:`Queue the selected cells in the Assembler. Nothing is rendered automatically — for each cell you'll click "Order video", "Generate funnel" or "Order VSL" individually. When the build is ready, configure audience & budget and launch to Meta from Assembler.`})]}),e.jsx("div",{className:"rounded-md border bg-muted/40 p-3 text-sm",children:(()=>
8{const t=b.size===0?g.length:b.size,s=ae.data?.videosPerAdSet??2,o=(b.size===0?g:g.filter(f=>b.has(f.id))).reduce((f,F)=>{const q=F.aspects;return f+(Array.isArray(q)&&q.length>0?q.length:3)},0)*s;return e.jsxs("div",{className:"space-y-1.5",children:[e.jsxs("div",{children:[e.jsx("strong",{children:t})," cell",t===1?"":"s"," will be queued."]}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Production plan: ",e.jsxs("strong",{children:["~",o]})," Arcads video job",o===1?"":"s"," ","(",s," video",s===1?"":"s"," per aspect ratio) + 1 VSL per cell."]}),e.jsx("div",{className:"text-[11px] text-muted-foreground/80",children:"Jobs already planned for these cells are skipped automatically — clicking twice will not double-create."}),e.jsx("div",{className:"text-[11px] text-muted-foreground/80",children:"Tip: change videos per ad set in Production Studio → Production settings."})]})})()}),e.jsxs(Ot,{children:[e.jsx(u,{variant:"outline",onClick:()=>Ce(!1),children:"Cancel"}),e.jsxs(u,{onClick:()=>{Ce(!1);const t=b.size===0?g.length:b.size;c.info(`Queuing ${t} cell${t===1?"":"s"} — progress shows above the matrix and in Assembler.`),ne.mutate()},disabled:ne.isPending,children:[e.jsx(zt,{className:$("mr-2 h-4 w-4",ne.isPending&&"animate-pulse")}),"Send to Assembler"]})]})]})}),e.jsx($s,{open:Bt,onOpenChange:be,children:e.jsxs(Ds,{children:[e.jsxs(qs,{children:[e.jsxs(Rs,{children:["Delete ",b.size," cell",b.size===1?"":"s","?"]}),e.jsx(Ls,{children:"Their variations and scripts will also be deleted. The tiles will return to an empty “Generate” state so you can rebuild individual coordinates on demand."})]}),e.jsxs(Os,{children:[e.jsx(Ts,{disabled:se.isPending,children:"Cancel"}),e.jsx(zs,{disabled:se.isPending||b.size===0,onClick:t=>{t.preventDefault();const s=Array.from(b);s.length!==0&&se.mutate(s,{onSettled:()=>be(!1)})},children:se.isPending?"Deleting…":"Delete"})]})]})})]}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"Connect a workspace to view the matrix."})}function te({label:n,value:j,multiline:A,targetRange:p}){if(!j)return null;const P=A?j.split(/\n{2,}/).map(w=>w.trim()).filter(Boolean):null,k=j.length,L=p?k>=p[0]&&k<=p[1]:!0,V=p?!L&&k>=p[0]-50&&k<=p[1]+50:!1;return e.jsxs("div",{children:[e.jsxs("div",{className:"mb-1 flex items-center justify-between gap-2",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground",children:n}),p?e.jsxs("span",{className:$("text-[10px] tabular-nums",L?"text-emerald-500":V?"text-amber-500":"text-destructive"),children:[k," / ",p[0],"–",p[1]]}):null]}),A?e.jsx("div",{className:"rounded-md border bg-card p-2 space-y-1",children:P.map((w,m)=>e.jsx("p",{className:"whitespace-pre-wrap",children:w},m))}):e.jsx("p",{className:"rounded-md border bg-card p-2",children:j})]})}function Re({label:n,value:j}){return e.jsxs("div",{className:"rounded-md border bg-background/60 p-2",children:[e.jsx("p",{className:"text-[10px] text-muted-foreground",children:n}),e.jsx("p",{className:"font-semibold",children:j??"—"})]})}const Ma="active_workspace_id",$a="active_offer_id::";function sn(){const n=h(Qs),j=h(Gs),{data:A}=z({queryKey:["workspaces"],queryFn:async()=>{try{return await n({})}catch(m){if(!(m instanceof Error?m.message:String(m)).includes("No authorization header provided"))throw m;const{data:x,error:O}=await at.from("workspaces").select("id, name").order("created_at",{ascending:!0});if(O)throw new Error(O.message);return x??[]}}}),p=(()=>
8{if(!A?.length)return null;if(typeof window>"u")return A[0];try{const m=window.localStorage.getItem(Ma);return m?A.find(C=>C.id===m)??A[0]:A[0]}catch{return A[0]}})(),{data:P}=z({queryKey:["matrix-offers",p?.id],queryFn:()=>j({data:{workspaceId:p.id}}),enabled:!!p?.id}),k=p?`${$a}${p.id}`:null,[L,V]=d.useState(null);d.useEffect(()=>{if(!P||P.length===0||!k){V(null);return}const m=P.filter(O=>O.status==="active");if(m.length===0){V(null);return}let C=null;try{C=window.localStorage.getItem(k)}catch{C=null}if(C&&m.find(O=>O.id===C))V(C);else{const O=m.find(de=>de.is_default)??m[0];V(O?.id??null)}},[P,k]);const w=m=>{if(V(m),k)try{window.localStorage.setItem(k,m)}catch{}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-start justify-between gap-4 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-semibold tracking-tight",children:"Matrix"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Angle × hook × format cells. Send to Assembler to queue the build, then configure & launch to Meta from Assembler when you're ready."})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[p&&e.jsx(ha,{workspaceId:p.id}),P&&P.length>0&&L&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"Offer"}),e.jsxs(rt,{value:L,onValueChange:w,children:[e.jsx(it,{className:"w-[260px] h-9",children:e.jsx(ot,{placeholder:"Select an offer"})}),e.jsx(dt,{children:P.map(m=>{const C=m.status!=="active";return e.jsxs(ct,{value:m.id,disabled:C,children:[m.name??"Untitled offer",m.is_default?" · default":"",C?" · draft (activate to use)":""]},m.id)})})]}),e.jsx(lt,{to:"/offer-lab",className:"text-xs text-muted-foreground underline-offset-4 hover:underline",children:"Edit in Offer Lab →"})]})]})]}),p?e.jsx(Fa,{workspaceId:p.id,offerId:L??void 0}):e.jsx(nt,{className:"h-64 w-full"})]})}export{sn as component};

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.