1import{j as e,r as t,bM as ue}from"./vendor-DG5Dz7Ik.js";import{b as K,a as z,z as U}from"./view-analytics-BkPD6vG9.js";import"./pdf-vendor-BQyayL6K.js";import"./socket-vendor-DENoLIvz.js";import"./auth-vendor-CeReGQmr.js";const Q="/v1/v19-cohorts",J={createCohort(a){return K(Q,a)},listCohorts(a={}){const l=new URLSearchParams;a.status&&l.set("status",a.status),a.cursor&&l.set("cursor",a.cursor),typeof a.limit=="number"&&l.set("limit",String(a.limit));const n=l.toString()?`?${l.toString()}`:"";return z(`${Q}${n}`)},getCohort(a){return z(`${Q}/${encodeURIComponent(a)}`)},startSimulation(a){return K(`${Q}/${encodeURIComponent(a)}/start`,{})}},me={sm:"px-1.5 py-0.5 text-[10px]",md:"px-2 py-0.5 text-xs",lg:"px-3 py-1 text-sm"},xe="Simulated: data not derived from real users (NF-03 / COHORT-07)";function oe({size:a="md",tooltip:l=xe,className:n=""}){const d=me[a];return e.jsxs("span",{"data-testid":"simulated-badge",role:"status","aria-label":l,title:l,className:`inline-flex items-center gap-1 rounded-full border border-amber-300 bg-amber-100 font-semibold text-amber-900 ${d} ${n}`.trim(),children:[e.jsx("span",{"aria-hidden":"true",children:"â "}),e.jsx("span",{children:"Simulated"})]})}const re=1e4,ne=3,ie=120,Y=2e3,pe=1,he=365,be=90,le={v15_panel:"Persona panels",v9_twin:"Customer twins",v13_consumer_sim:"Prior simulations"},fe=async a=>{const l=await Promise.allSettled([z("/v1/persona-panels/panels",{signal:a}),z("/v1/digital-twins/cohorts",{signal:a}),z("/v1/simulation/runs",{signal:a})]),[n,d,j]=l,b=n.status==="fulfilled"?n.value:[],S=d.status==="fulfilled"?d.value:[],g=j.status==="fulfilled"?j.value:[];return{errors:l.flatMap((c,h)=>c.status==="rejected"?[`${["Persona panels","Customer twins","Prior simulations"][h]} could not be loaded.`]:[]),panels:b.filter(c=>Array.isArray(c.personas)&&c.personas.length>0).map(c=>({kind:"v15_panel",id:c.id,label:c.name,capacity:c.personas.length})),twins:S.filter(c=>c.twinCount>0).map(c=>({kind:"v9_twin",id:c.id,label:`Twin audience ${c.id.slice(0,8)}`,capacity:c.twinCount})),consumerSims:g.filter(c=>typeof c.panel_size=="number"&&c.panel_size>0).map(c=>{var h;return{kind:"v13_consumer_sim",id:c.id,label:`Sim run ${c.id.slice(0,8)} (${((h=c.created_at)==null?void 0:h.slice(0,10))??"undated"})`,capacity:c.panel_size}})}};function ge({simulationQueueEnabled:a,loadSources:l=fe,onCreated:n}){const[d,j]=t.useState(""),[b,S]=t.useState(""),[g,c]=t.useState(be),[h,w]=t.useState([]),[E,k]=t.useState({panels:[],twins:[],consumerSims:[]}),[F,i]=t.useState(!0),[u,y]=t.useState(!1),m=t.useRef(!1),R=t.useRef(a);R.current=a;const[$,L]=t.useState(!1),[B,I]=t.useState(null),[O,V]=t.useState(null),[X,N]=t.useState([]),[M,W]=t.useState(0),[v,T]=t.useState(null),A=t.useRef(!0);t.useEffect(()=>{A.current=!0,i(!0),N([]);const r=new AbortController;return(async()=>{try{const p=await l(r.signal);if(!A.current)return;k(p),N(p.errors??[])}catch{if(!A.current)return;I("Failed to load source pools â refresh to retry.")}finally{A.current&&i(!1)}})(),()=>{A.current=!1,r.abort()}},[l,M]);const H=t.useMemo(()=>h.reduce((r,p)=>r+p.size,0),[h]),q=H>re,ee=H<1,s=d.trim().length>=ne&&d.length<=ie,o=b.length<=Y,f=!u&&!v&&!q&&!ee&&s&&o,D=t.useCallback(r=>{w(p=>{const x=p.findIndex(C=>C.kind===r.kind&&C.id===r.id);return x>=0?p.filter((C,_)=>_!==x):[...p,{kind:r.kind,id:r.id,label:r.label,size:Math.min(r.capacity,100),capacity:r.capacity}]})},[]),P=t.useCallback((r,p,x)=>{w(C=>C.map(_=>_.kind===r&&_.id===p?{..._,size:Math.max(1,Math.min(_.capacity,Math.floor(x)))}:_))},[]),se=t.useCallback(()=>({name:d.trim(),description:b.trim().length>0?b.trim():void 0,horizonDays:g,sources:h.map(r=>({kind:r.kind,id:r.id,size:r.size}))}),[d,b,g,h]),ae=t.useCallback(async(r=!1)=>{if(m.current||r&&(a!==!0||$)||!v&&!f||v&&!r)return;m.current=!0,y(!0),I(null);let p=v;try{if(!p){if(p=await J.createCohort(se()),!A.current)return;T(p),V({kind:"success",text:`Cohort saved (${p.personaCount} personas).`})}if(r){if(R.current!==!0){I("The audience was saved, but simulation is unavailable on this deployment. Open the saved audience below.");return}if(await J.startSimulation(p.cohortId),!A.current)return;L(!0),V({kind:"success",text:"Simulation queued."})}n==null||n(p)}
1catch(x){if(A.current){const C=U(x,p?"Failed to start simulation.":"Failed to save cohort.");I(p?`The audience was saved, but starting simulation was not confirmed: ${C} Retry starting or open the saved audience below.`:C)}}finally{m.current=!1,A.current&&y(!1)}},[f,se,v,a,$,n]),de=[...E.panels,...E.twins,...E.consumerSims];return e.jsxs("div",{className:"cohort-composer-v19 max-w-5xl mx-auto p-6 space-y-6 text-gray-900",children:[e.jsx("header",{className:"flex items-start justify-between gap-4 border-b pb-4",children:e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-semibold flex items-center gap-3",children:["Audience simulation",e.jsx(oe,{})]}),e.jsx("p",{"data-testid":"ai-act-disclosure",className:"text-sm text-gray-700 mt-2 max-w-2xl",children:"Simulated results are not derived from real user data. Intended for pre-launch forecasting only â outputs must not be used to make consequential automated decisions about identifiable individuals (EU AI Act, NF-03)."})]})}),B&&e.jsx("div",{role:"alert","data-testid":"error-banner",className:"bg-red-50 border border-red-300 text-red-800 px-3 py-2 rounded text-sm",children:B}),X.length>0&&e.jsxs("div",{role:"alert",className:"rounded border border-amber-300 bg-amber-50 p-3 text-sm text-amber-900",children:[X.join(" ")," ",e.jsx("button",{className:"underline",disabled:u||!!v,onClick:()=>W(r=>r+1),children:"Retry sources"})]}),v&&e.jsxs("p",{className:"text-sm",children:["Audience saved. ",e.jsx("button",{className:"underline",onClick:()=>n==null?void 0:n(v),children:"Open saved audience"}),". Its settings and sources are locked."]}),a!==!0&&e.jsx("p",{className:"text-sm text-amber-900",children:"Simulation is unavailable on this deployment. You can still compose and save an audience."}),O&&e.jsx("div",{role:"status","data-testid":O.kind==="success"?"toast-success":"toast-error",className:O.kind==="success"?"bg-green-50 border border-green-300 text-green-800 px-3 py-2 rounded text-sm":"bg-red-50 border border-red-300 text-red-800 px-3 py-2 rounded text-sm",children:O.text}),e.jsxs("fieldset",{disabled:u||!!v,className:"space-y-4",children:[e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"text-sm font-medium",children:"Cohort name"}),e.jsx("input",{type:"text",value:d,onChange:r=>j(r.target.value),minLength:ne,maxLength:ie,required:!0,"data-testid":"cohort-name",className:"mt-1 block w-full border rounded px-3 py-2",placeholder:"e.g. Q1 launch â early adopters"})]}),e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"text-sm font-medium",children:"Description (optional)"}),e.jsx("textarea",{value:b,onChange:r=>S(r.target.value.slice(0,Y)),maxLength:Y,"data-testid":"cohort-description",className:"mt-1 block w-full border rounded px-3 py-2",rows:3}),e.jsxs("span",{className:"text-xs text-gray-500",children:[b.length," / ",Y]})]}),e.jsxs("label",{className:"block",children:[e.jsxs("span",{className:"text-sm font-medium",children:["Simulation horizon: ",g," day",g===1?"":"s"]}),e.jsx("input",{type:"range",min:pe,max:he,value:g,onChange:r=>c(Number(r.target.value)),"data-testid":"horizon-slider",className:"mt-1 block w-full"})]})]}),e.jsx("fieldset",{disabled:u||!!v,className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:["v15_panel","v9_twin","v13_consumer_sim"].map(r=>{const p=de.filter(x=>x.kind===r);return e.jsxs("div",{"data-testid":`source-column-${r}`,className:"border rounded p-3 bg-gray-50",children:[e.jsx("h2",{className:"text-sm font-semibold mb-2",children:le[r]}),F&&e.jsx("div",{className:"text-xs text-gray-500",children:"Loadingâ¦"}),!F&&p.length===0&&e.jsxs("div",{className:"text-xs text-gray-500",children:["No ",le[r]," available",e.jsx("a",{className:"block underline mt-2",href:r==="v9_twin"?"/digital-twins":"/tools",children:"Find audience research tools"})]}),e.jsx("ul",{className:"space-y-2",children:p.map(x=>{const C=h.find(Z=>Z.kind===x.kind&&Z.id===x.id),_=!!C;return e.jsxs("li",{className:"text-sm",children:[e.jsxs("label",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",checked:_,onChange:()=>D(x),"data-testid":`source-toggle-${x.kind}-${x.id}`}),e.jsx("span",{className:"truncate",title:x.label,children:x.label}),e.jsxs("span",{className:"ml-auto text-xs text-gray-500",children:["/",x.capacity]})]}),_&&C&&e.jsx("input",{type:"number","aria-label":`Audience size from ${x.label}`,min:1,max:x.capacity,value:C.size,onChange:Z=>P(x.kind,x.id,Number(Z.target.value)),"data-testid":`source-size-${x.kind}-${x.id}`,className:"mt-1 block w-24 border rounded px-2 py-1 text-xs"})]},x.id)})})]},r)})}),e.jsxs("section",{"data-testid":"size-counter",className:"text-sm font-medium "+(q?"text-red-700 font-bold":"text-gray-700"),children:["Selected: ",H.toLocaleStr
1ing()," / ",re.toLocaleString(),q&&e.jsx("span",{className:"ml-2","data-testid":"over-limit-warning",children:"â exceeds 10,000 cap, trim sources to save"})]}),e.jsxs("section",{className:"flex gap-3 border-t pt-4",children:[e.jsx("button",{type:"button",onClick:()=>void ae(),disabled:!f,"data-testid":"save-button",className:"px-4 py-2 rounded bg-blue-600 text-white disabled:bg-gray-300",children:"Save Cohort"}),e.jsx("button",{type:"button",onClick:()=>void ae(!0),disabled:a!==!0||$||u||!v&&!f,"data-testid":"save-and-start-button",className:"px-4 py-2 rounded bg-blue-700 text-white disabled:bg-gray-300",children:$?"Simulation queued":v?"Retry starting saved simulation":"Save & Start Simulation"})]})]})}const G="/v1/v19-cohorts",ye={forecast(a,l){return K(`${G}/${encodeURIComponent(a)}/forecast`,l)},recalibrate(a,l){return K(`${G}/${encodeURIComponent(a)}/recalibrate`,l)},listRecalibrations(a){return z(`${G}/${encodeURIComponent(a)}/recalibrations`)},fairnessAudit(a,l){const n=new URLSearchParams({dimension:l}).toString();return z(`${G}/${encodeURIComponent(a)}/fairness?${n}`)}},te=20,je=["email","sms","push","ad"];function ce(a){return{stepIndex:a,dayOffset:a*3,brandId:"",kind:"email",payloadJson:""}}function Ne({cohortId:a,service:l=ye}){const[n,d]=t.useState([ce(0)]),[j,b]=t.useState(null),[S,g]=t.useState(!1),[c,h]=t.useState(null),[w,E]=t.useState("gender"),[k,F]=t.useState(null),[i,u]=t.useState(!1),[y,m]=t.useState(null),[R,$]=t.useState([]),[L,B]=t.useState(!0),[I,O]=t.useState(""),[V,X]=t.useState(0),N=t.useRef(!0);t.useEffect(()=>{N.current=!0,B(!0),O(""),$([]);const s=new AbortController;return(async()=>{try{const o=await l.listRecalibrations(a);if(!N.current)return;$(o.recalibrations)}catch{N.current&&O("Recalibration history is unavailable.")}finally{N.current&&B(!1)}})(),()=>{N.current=!1,s.abort()}},[a,l,V]);const M=t.useMemo(()=>j?j.totals:null,[j]),W=t.useCallback(()=>{d(s=>s.length>=te?s:[...s,ce(s.length)])},[]),v=t.useCallback(s=>{d(o=>o.length<=1?o:o.filter((D,P)=>P!==s).map((D,P)=>({...D,stepIndex:P})))},[]),T=t.useCallback((s,o)=>{d(f=>f.map((D,P)=>P===s?{...D,...o}:D))},[]),A=t.useCallback(async()=>{h(null),g(!0);try{const s=await l.forecast(a,{drip:n.map(o=>({stepIndex:o.stepIndex,dayOffset:o.dayOffset,brandId:o.brandId,kind:o.kind,payloadJson:o.payloadJson||void 0})),dryRun:!0});if(!N.current)return;b(s)}catch(s){if(N.current){const o=U(s,"Forecast failed.");h(o)}}finally{N.current&&g(!1)}},[a,n,l]),H=t.useCallback(async()=>{m(null),u(!0);try{const s=await l.fairnessAudit(a,w);if(!N.current)return;F(s)}catch(s){if(N.current){const o=U(s,"Fairness audit failed.");m(o)}}finally{N.current&&u(!1)}},[a,w,l]),q=n.length<te,ee=n.length>1;return e.jsxs("div",{className:"cohort-forecast-v19 max-w-5xl mx-auto p-6 space-y-6 text-gray-900",children:[e.jsx("header",{className:"flex items-start justify-between gap-4 border-b pb-4",children:e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-semibold flex items-center gap-3",children:["Audience forecast (simulated)",e.jsx(oe,{})]}),e.jsx("p",{"data-testid":"ai-act-disclosure",className:"text-sm text-gray-700 mt-2 max-w-2xl",children:"Forecast is simulated; not derived from real user outcomes. Outputs must not be used to make consequential automated decisions about identifiable individuals (EU AI Act, NF-03)."})]})}),c&&e.jsx("div",{role:"alert","data-testid":"error-banner",className:"bg-red-50 border border-red-300 text-red-800 px-3 py-2 rounded text-sm",children:c}),e.jsxs("section",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h2",{className:"text-lg font-medium",children:"Drip sequence"}),e.jsxs("span",{className:"text-xs text-gray-500",children:[n.length," / ",te," steps"]})]}),e.jsx("ul",{className:"space-y-2","data-testid":"drip-list",children:n.map((s,o)=>e.jsxs("li",{"data-testid":`drip-step-${o}`,className:"grid grid-cols-1 md:grid-cols-12 gap-2 border rounded p-2 bg-gray-50",children:[e.jsxs("span",{className:"md:col-span-1 text-xs text-gray-700 self-center",children:["#",s.stepIndex]}),e.jsxs("label",{className:"md:col-span-2 text-xs",children:["Day",e.jsx("input",{type:"number",min:0,max:365,value:s.dayOffset,onChange:f=>T(o,{dayOffset:Number(f.target.value)}),"data-testid":`drip-day-${o}`,className:"mt-1 block w-full border rounded px-2 py-1"})]}),e.jsxs("label",{className:"md:col-span-3 text-xs",children:["Brand ID",e.jsx("input",{type:"text",value:s.brandId,onChange:f=>T(o,{brandId:f.target.value}),"data-testid":`drip-brand-${o}`,placeholder:"UUID",className:"mt-1 block w-full border rounded px-2 py-1"})]}),e.jsxs("label",{className:"md:col-span-2 text-xs",children:["Kind",e.jsx("select",{value:s.kind,onChange:f=>T(o,{kind:f.target.value}),"data-testid":`drip-kind-${o}`,className:"mt-1 block w-full border rounded px-2 py-1",children:je.map(f=>e.jsx("option",{value:f,children:f},f))})]}),e.jsxs("label",{className:"md:col-span-3 text-xs",children:["Payload (JSON)",e.jsx("input",{type:"text",value:s.payloadJson??"",onChange:f=>T(o,{payloadJson:f.target.value}),"data-testid":`drip-payload-${o}`,placeholder:"{}",className:"mt-1 block w-full border rounded px-2 py-1"})]}),e.jsx("div",{className:"md:col-span-1 self-end",children:e.jsx("button",{type:"button",onClick:()=>v(o),disabled:!ee,"data-testid":`drip-remove-${o}`,className:"text-xs text-red-600 disabled:text-gray-400",children:"Remove"})})]},`step-${o}`))}),e.jsxs("div",{className:"flex gap-3",children:[e.jsx("button",{type:"button",onClick:W,disabled:!q,"data-testid":"drip-add",className:"px-3 py-1 rounded bg-gray-200 text-gray-800 disabled:bg-gray-100 disabled:text-gray-400 text-sm",children:"+ Add step"}),e.jsx("button",{type:"button",onClick:()=>void A(),disabled:S,"data-testid":"run-forecast",className:"px-4 py-1 rounded bg-blue-600 text-white disabled:bg-gray-300 text-sm",children:S?"Runningâ¦":"Run Forec
1ast"})]})]}),j&&e.jsxs("section",{"data-testid":"forecast-results",className:"space-y-3 border rounded p-3",children:[e.jsx("h2",{className:"text-lg font-medium",children:"Forecast results"}),M&&e.jsxs("div",{className:"text-sm text-gray-700",children:["Totals â impressions: ",M.impressions.toLocaleString(),", clicks: ",M.clicks.toLocaleString(),", conversions:"," ",M.conversions.toLocaleString()]}),e.jsx("div",{"data-testid":"per-day-table",className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full text-xs",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left px-2 py-1",children:"Day"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Impressions"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Clicks"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Conversions"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Avg P(response)"})]})}),e.jsx("tbody",{children:j.perDay.filter(s=>s.impressions>0||s.clicks>0||s.conversions>0).map(s=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"px-2 py-1",children:s.day}),e.jsx("td",{className:"text-right px-2 py-1",children:s.impressions.toLocaleString()}),e.jsx("td",{className:"text-right px-2 py-1",children:s.clicks.toLocaleString()}),e.jsx("td",{className:"text-right px-2 py-1",children:s.conversions.toLocaleString()}),e.jsx("td",{className:"text-right px-2 py-1",children:s.avgResponseProb.toFixed(3)})]},s.day))})]})}),e.jsx("div",{"data-testid":"per-step-table",className:"overflow-x-auto",children:e.jsxs("table",{className:"min-w-full text-xs",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left px-2 py-1",children:"Step"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Touched personas"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Expected clicks"}),e.jsx("th",{className:"text-right px-2 py-1",children:"Expected conversions"})]})}),e.jsx("tbody",{children:j.perStep.map(s=>e.jsxs("tr",{className:"border-t",children:[e.jsxs("td",{className:"px-2 py-1",children:["#",s.stepIndex]}),e.jsx("td",{className:"text-right px-2 py-1",children:s.touchedPersonas.toLocaleString()}),e.jsx("td",{className:"text-right px-2 py-1",children:s.expectedClicks.toLocaleString()}),e.jsx("td",{className:"text-right px-2 py-1",children:s.expectedConversions.toLocaleString()})]},s.stepIndex))})]})})]}),e.jsxs("section",{className:"space-y-3 border rounded p-3",children:[e.jsx("h2",{className:"text-lg font-medium",children:"Longitudinal fairness audit"}),e.jsxs("div",{className:"flex items-end gap-3",children:[e.jsxs("label",{className:"text-xs flex-1 max-w-xs",children:["Demographic dimension",e.jsxs("select",{value:w,onChange:s=>E(s.target.value),"data-testid":"fairness-dimension",className:"mt-1 block w-full border rounded px-2 py-1 text-sm",children:[e.jsx("option",{value:"age_bracket",children:"Age bracket"}),e.jsx("option",{value:"gender",children:"Gender"}),e.jsx("option",{value:"income_bracket",children:"Income bracket"})]})]}),e.jsx("button",{type:"button",onClick:()=>void H(),disabled:i,"data-testid":"run-fairness",className:"px-3 py-1 rounded bg-blue-600 text-white disabled:bg-gray-300 text-sm",children:i?"Auditingâ¦":"Run Audit"})]}),y&&e.jsx("div",{role:"alert","data-testid":"fairness-error",className:"bg-red-50 border border-red-300 text-red-800 px-2 py-1 rounded text-xs",children:y}),k&&e.jsxs("div",{"data-testid":"fairness-result",className:"text-sm space-y-1",children:[e.jsxs("div",{children:["Four-fifths rule:"," ",e.jsx("span",{"data-testid":"four-fifths-badge",className:k.passedFourFifthsRule?"inline-block px-2 py-0.5 rounded text-xs bg-green-100 text-green-800":"inline-block px-2 py-0.5 rounded text-xs bg-red-100 text-red-800",children:k.passedFourFifthsRule?"PASS":"FAIL"})]}),!k.passedFourFifthsRule&&e.jsxs("div",{className:"text-xs text-gray-700",children:["Worst day: ",k.worstDay]}),e.jsxs("div",{className:"text-xs text-gray-600",children:["Audited ",k.perDay.length," days"]})]})]}),e.jsxs("section",{className:"space-y-2 border rounded p-3",children:[e.jsx("h2",{className:"text-lg font-medium",children:"Recalibration history"}),L&&e.jsx("div",{className:"text-xs text-gray-500",children:"Loadingâ¦"}),I&&e.jsxs("p",{role:"alert",className:"text-sm text-red-700",children:[I," ",e.jsx("button",{className:"underline",onClick:()=>X(s=>s+1),children:"Retry"})]}),!L&&!I&&R.length===0&&e.jsx("div",{className:"text-xs text-gray-500",children:"No recalibrations recorded for this cohort."}),R.length>0&&e.jsx("ul",{className:"space-y-1",children:R.map(s=>e.jsxs("li",{"data-testid":`recal-${s.id}`,className:"text-xs flex items-center gap-3",children:[e.jsx("span",{className:s.status==="applied"?"inline-block px-2 py-0.5 rounded bg-green-100 text-green-800":"inline-block px-2 py-0.5 rounded bg-gray-100 text-gray-800",children:s.status}),e.jsxs("span",{children:["divergence: ",s.divergenceScore.toFixed(3)]}),e.jsx("span",{className:"text-gray-500",children:new Date(s.createdAt).toLocaleStr
1ing()})]},s.id))})]})]})}function ve({cohortId:a,simulationQueueEnabled:l,onObserved:n}){const[d,j]=t.useState(null),[b,S]=t.useState(!0),[g,c]=t.useState(!1),[h,w]=t.useState(""),[E,k]=t.useState(""),[F,i]=t.useState(0),u=t.useRef(!1);t.useEffect(()=>(u.current=!0,()=>{u.current=!1}),[]),t.useEffect(()=>{let m=!0,R;S(!0),w("");const $=async()=>{try{const L=await J.getCohort(a);if(!m)return;j(L),n(L),L.status==="simulating"&&(R=window.setTimeout($,3e3))}catch(L){m&&w(U(L))}finally{m&&S(!1)}};return $(),()=>{m=!1,clearTimeout(R)}},[a,F,n]);const y=async()=>{if(!(!l||(d==null?void 0:d.status)!=="ready"||b||g||h)){c(!0),k("");try{await J.startSimulation(a)}catch(m){u.current&&k(U(m))}finally{u.current&&(c(!1),S(!0),i(m=>m+1))}}};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"max-w-5xl mx-auto p-6 pb-0 space-y-3",children:[d&&e.jsxs(e.Fragment,{children:[e.jsx("h2",{className:"text-xl font-semibold",children:d.name}),e.jsxs("p",{role:"status",children:["Audience status: ",d.status]})]}),b&&e.jsx("p",{role:"status",children:"Loading audience statusâ¦"}),h&&e.jsxs("p",{role:"alert",className:"text-red-700",children:[h," ",e.jsx("button",{className:"underline",onClick:()=>i(m=>m+1),children:"Retry audience status"})]}),(d==null?void 0:d.status)==="ready"&&e.jsx("button",{className:"border rounded px-4 py-2 disabled:opacity-50",disabled:!l||b||g||!!h,onClick:()=>void y(),children:g?"Starting simulationâ¦":"Start simulation"}),(d==null?void 0:d.status)==="simulating"&&e.jsxs("p",{children:["Simulation is in progress. ",h?"Status updates are paused; retry the audience status above.":"Status updates automatically; no further start is needed."]}),E&&e.jsxs("p",{role:"alert",className:"text-red-700",children:[E," Your audience is still saved. Check its current status before retrying."]})]}),e.jsx(Ne,{cohortId:a})]})}function Ae(){const[a,l]=ue(),n=a.get("cohort"),[d,j]=t.useState([]),[b,S]=t.useState(null),[g,c]=t.useState(""),[h,w]=t.useState(!0),[E,k]=t.useState(0);t.useEffect(()=>{let i=!0;return w(!0),c(""),S(null),J.listCohorts({limit:100}).then(u=>{i&&(j(y=>
1[...u.cohorts.filter(m=>!y.some(R=>R.id===m.id)),...y]),S(typeof u.simulationQueueEnabled=="boolean"?u.simulationQueueEnabled:null))}).catch(u=>{i&&c(U(u))}).finally(()=>{i&&w(!1)}),()=>{i=!1}},[E]);const F=t.useCallback(i=>{j(u=>{const y=u.find(m=>m.id===i.id);return(y==null?void 0:y.updatedAt)===i.updatedAt&&y.status===i.status&&y.name===i.name?u:y?u.map(m=>m.id===i.id?i:m):[...u,i]})},[]);return e.jsxs("section",{className:"cohort-studio-page bg-white min-h-full text-gray-900",style:{colorScheme:"light"},children:[e.jsxs("header",{className:"max-w-5xl mx-auto p-6 pb-0 space-y-3",children:[e.jsx("p",{className:"text-sm font-medium",children:"Workspace-wide research · All businesses. These synthetic audiences are not customer contacts or measured campaign results."}),e.jsxs("div",{className:"flex flex-wrap gap-3 items-end",children:[e.jsxs("label",{className:"text-sm flex-1",children:["Saved simulated audience",e.jsxs("select",{className:"block mt-1 border rounded p-2 w-full bg-white",value:n??"",onChange:i=>l(i.target.value?{cohort:i.target.value}:{}),children:[e.jsx("option",{value:"",children:"Compose a new audience"}),n&&!d.some(i=>i.id===n)&&e.jsx("option",{value:n,children:"Selected audience"}),d.map(i=>e.jsxs("option",{value:i.id,children:[i.name," · ",i.status]},i.id))]})]}),n&&e.jsx("button",{className:"text-sm underline p-2",onClick:()=>l({}),children:"Compose another audience"})]}),h&&e.jsx("p",{role:"status",className:"text-sm",children:"Loading saved audiencesâ¦"}),g&&e.jsxs("p",{role:"alert",className:"text-red-700 text-sm",children:[g," ",e.jsx("button",{className:"underline",onClick:()=>k(i=>i+1),children:"Retry"})]}),b!==!0&&e.jsxs("p",{className:"text-sm",children:[b===!1?"Simulation execution is disabled in this deployment.":"Simulation availability is not confirmed; starting is unavailable."," You can still compose, save, reopen, and review audiences."]})]}),n?e.jsx(ve,{cohortId:n,simulationQueueEnabled:b===!0,onObserved:F},n):e.jsx(ge,{simulationQueueEnabled:b===!0,onCreated:i=>l({cohort:i.cohortId})})]})}export{Ae as CohortStudioPage,Ae 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.