1import{j as e}from"./vendor-markdown-C-I3nZWo.js";import{r as n}from"./vendor-charts-DphG3eWF.js";import{n as V,r as b,N as W,L as $,B as x,C as I,a as q,ac as E,b as F,K as ee,M as se,Q as te,S as ae,s as o,a3 as l}from"./index-DFoQxP2Z.js";import{u as re}from"./useIsAdmin-Cz3smzBt.js";import{B as z}from"./badge-DvZAazPQ.js";import{T as ie}from"./textarea-DyjusVvx.js";import{C as ne}from"./checkbox-DtTttihB.js";import{o as v,A as le,a3 as de,a5 as oe}from"./vendor-icons-DptrxPFP.js";import"./vendor-office-CYA1a3lH.js";import"./vendor-query-Csct0vqx.js";import"./vendor-radix-DiGlQgnh.js";import"./vendor-supabase-C6T5JIOu.js";import"./vendor-analytics-4JMxWFgu.js";import"./vendor-forms-Crpao7PO.js";function ye(){var R;const u=re(),{id:a}=V(),[t,w]=n.useState(null),[c,i]=n.useState([]),[m,_]=n.useState(""),[B,y]=n.useState(!0),[S,k]=n.useState(!1),[C,H]=n.useState(""),[T,A]=n.useState(!1),[O,L]=n.useState(!1),[D,P]=n.useState(!1);async function M(){if(a){P(!0);try{await g({silent:!0});const{data:s,error:r}=await o.functions.invoke("supplement-send-test",{body:{supplement_id:a,recipient:"[email protected]"}});if(r)throw r;if(s!=null&&s.error)throw new Error(s.error);l.success("Test sent to [email protected]")}catch(s){l.error((s==null?void 0:s.message)??"Send failed")}finally{P(!1)}}}async function f(){if(!a)return;y(!0);const[{data:s},{data:r}]=await Promise.all([o.from("supplements").select("id, radar_id, period_month, status, is_light_month, intro_md, stat_strip, approved_at, approved_by, sent_at, radars(name, org_id)").eq("id",a).maybeSingle(),o.from("supplement_items").select("*").eq("supplement_id",a).order("kind").order("rank")]);w(s),i(r??[]),_((s==null?void 0:s.intro_md)??""),y(!1)}n.useEffect(()=>{u&&a&&f()},[u,a]);async function K(){if(a){A(!0);try{await g({silent:!0});const{data:s,error:r}=await o.functions.invoke("supplement-render-preview",{body:{supplement_id:a}});if(r)throw r;H(typeof s=="string"?s:(s==null?void 0:s.html)??""),L(!0)}catch(s){l.error((s==null?void 0:s.message)??"Preview failed")}finally{A(!1)}}}async function g(s={}){if(!t)return;k(!0);const r=m.trim().length>0&&t.status==="draft_awaiting_intro"?"draft":t.status,{error:j}=await o.from("supplements").update({intro_md:m,status:r}).eq("id",t.id);if(k(!1),j){l.error(j.message);return}s.silent||l.success("Saved"),await f()}async function p(s,r){await o.from("supplement_items").update({included:r}).eq("id",s.id),i(j=>j.map(N=>N.id===s.id?{...N,included:r}:N))}async function Q(){if(!t)return;if(!m.trim()){l.error("Add an intro paragraph first");return}t.status==="draft_awaiting_intro"&&await g({silent:!0});const{data:{user:s}}=await o.auth.getUser(),{error:r}=await o.from("supplements").update({status:"approved",approved_at:new Date().toISOString(),approved_by:(s==null?void 0:s.id)??null}).eq("id",t.id);if(r){l.error(r.message);return}l.success("Approved"),await f()}async function U(){if(!t)return;const{error:s}=await o.functions.invoke("supplement-generate-draft",{body:{radar_id:t.radar_id,period_month:t.period_month,discardExisting:!0}});if(s){l.error(s.message);return}l.success("Regenerated"),await f()}if(u===void 0||B)return e.jsx(b,{children:e.jsx("div",{className:"p-6",children:e.jsx(v,{className:"h-5 w-5 animate-spin"})})});if(!u)return e.jsx(W,{to:"/",replace:!0});if(!t)return e.jsx(b,{children:e.jsx("div",{className:"p-6",children:"Not found."})});const X=c.filter(s=>s.kind==="root"),Y=c.filter(s=>s.kind==="seed"),Z=c.filter(s=>s.kind==="signal"),G=c.filter(s=>s.kind==="creator"),J=c.filter(s=>s.kind==="competitor"),d=t.status==="approved"||t.status==="sent";return e.jsx(b,{children:e.jsxs("div",{className:"px-4 sm:px-6 md:px-10 py-6 space-y-6 max-w-[1400px]",children:[e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsxs($,{to:"/workspace/settings?app=supplements",className:"inline-flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground mb-3",children:[e.jsx(le,{className:"h-3.5 w-3.5"})," Back to supplements"]}),e.jsx("p",{className:"text-xs font-mono text-muted-foreground mb-1",children:t.period_month}),e.jsx("h1",{className:"font-redaction italic text-2xl md:text-3xl text-foreground leading-none",children:((R=t.radars)==null?void 0:R.name)??"RADAR"}),e.jsxs("div",{className:"flex gap-2 mt-3",children:[e.jsx(z,{variant:"secondary",children:t.status}),t.is_light_month&&e.jsx(z,{variant:"outline",children:"Light this month"})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs(x,{variant:"outline",size:"sm",onClick:U,disabled:d,children:[e.jsx(de,{className:"h-4 w-4 mr-1"}),"Regenerate"]}),e.jsxs(x,{variant:"outline",size:"sm",onClick:K,disabled:T,children:[T?e.jsx(v,{className:"h-4 w-4 mr-1 animate-spin"}):null,"Preview email"]}),e.jsxs(x,{variant:"outline",size:"sm",onClick:M,disabled:D,children:[D?e.jsx(v,{className:"h-4 w-4 mr-1 animate-spin"}):null,"Send test"]}),e.jsxs(x,{size:"sm",onClick:Q,disabled:d||!m.trim(),children:[e.jsx(oe,{className:"h-4 w-4 mr-1"}),"Approve"]})]})]}),e.jsx("div",{className:"grid grid-cols-1 gap-6",children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs(I,{children:[e.jsx(q,{children:e.jsx(E,{className:"text-base",children:"Intro paragraph (required)"})}),e.jsxs(F,{className:"space-y-3",children:[e.jsx(ie,{value:m,onChange:s=>_(s.target.value),rows:6,disabled:d,placeholder:"A single human-written paragraph from the strategy team. This is what makes the supplement feel curated, not automated."}),e.jsx("div",{className:"flex justify-end",children:e.jsxs(x,{size:"sm",variant:"outline",onClick:()=>g(),disabled:S||d,children:[S?e.jsx(v,{className:"h-4 w-4 mr-1 animate-spin"}):null," Save"]})})]})]}),e.jsx(h,{title:"Top Roots",items:X,onToggle:p,disabled:d,render:s=>e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"font-medium",children:s.name}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[s.status," · momentum ",Math.round(Number(s.momentum??0))," · ",Number(s.delta??0)>=0?"+":"",Number(s.delta??0).toFixed(1),"%"]})]})}),e.jsx(h,{title:"Seeds (radar-themed XYZ)",items:Y,onToggle:p,disabled:d,render:s=>e.jsxs("div",{className:"flex gap-3",children:[s.image_url&&e.jsx("img",{src:s.image_url,alt:"",className:"w-20 h-20 object-cover rounded border flex-shrink-0"}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"font-medium truncate",children:s.headline}),s.url&&e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:s.url})]})]})}),e.jsx(h,{title:"Signals",items:Z,onToggle:p,disabled:d,render:s=>e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"font-medium",children:s.headline}),s.url&&e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:s.url})]})}),e.jsx(h,{title:"Creator",items:G,onToggle:p,disabled:d,render:s=>e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"font-medium",children:[s.name," ",s.handle?e.jsx("span",{className:"text-muted-foreground",children:s.handle}):null]}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:["Reach ",s.reach??"â"," · Fit ",s.fit_score??"â"]})]})}),e.jsx(h,{title:"Competitor",items:J,onToggle:p,disabled:d,render:s=>e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"font-medium",children:s.name}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[s.threat_level??"â"," · ",s.last_move??""]})]})})]})}),e.jsx(ee,{open:O,onOpenChange:L,children:e.jsxs(se,{className:"max-w-5xl w-[95vw] h-[90vh] p-0 flex flex-col",children:[e.jsx(te,{className:"px-6 pt-6 pb-3 border-b",children:e.jsx(ae,{children:"Email preview"})}),e.jsx("div",{className:"flex-1 overflow-hidden p-4",children:C?e.jsx("iframe",{title:"Supplement preview",srcDoc:C,className:"w-full h-full border rounded bg-white"}):e.jsx("div",{className:"text-sm text-muted-foreground py-12 text-center",children:"No preview rendered yet."})})]})})]})})}function h({title:u,items:a,onToggle:t,render:w,disabled:c}){return e.jsxs(I,{children:[e.jsx(q,{children:e.jsxs(E,{className:"text-base",children:[u," ",e.jsxs("span",{className:"text-xs text-muted-foreground font-normal",children:["(",a.filter(i=>i.included).length,"/",a.length,")"]})]})}),e.jsxs(F,{className:"space-y-3",children:[a.length===0&&e.jsx("div",{className:"text-sm text-muted-foreground",children:"None."}),e.jsx("div",{className:a.length>
15?"max-h-[520px] overflow-y-auto pr-2 space-y-3":"space-y-3",children:a.map(i=>e.jsxs("div",{className:"flex gap-3 items-start border rounded-md p-3",children:[e.jsx(ne,{checked:i.included,disabled:c,onCheckedChange:m=>t(i,!!m)}),e.jsxs("div",{className:"flex-1 min-w-0",children:[w(i.snapshot??{}),i.implication&&e.jsxs("div",{className:"text-sm mt-1 italic text-foreground/80",children:['"',i.implication,'"']})]})]},i.id))})]})]})}export{ye 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.