1import{y as se,r as m,j as e,f as B,C as $,B as g,A as U,Q as H,V as re,O as ae,v as ne,h as oe,b as P,t as w,X as le}from"./main-C2rM9v8a.js";import{U as ie}from"./usStates-wrzsZ7vi.js";import{S as ce,a as de,b as me,c as xe,d as S}from"./select-B7bx_0Qj.js";import{T as pe}from"./triangle-alert-Cmt4eS2T.js";import"./chevron-up-DG4nd5Ke.js";const T="lb-ii-fill-states-v1",O=25,R=25,he=new Set(ie);function D(d){return d.error?"error":d.confidence>=.9?"high":d.confidence>=.6?"mid":"low"}function ue({row:d}){const i=D(d);if(i==="error")return e.jsx("span",{className:"text-xs font-semibold text-destructive",children:"â error"});const x=Math.round((d.confidence||0)*100),l=i==="high"?"text-emerald-600":i==="mid"?"text-amber-600":"text-red-600",N=i==="high"?"ð¢":i==="mid"?"ð¡":"ð´";return e.jsxs("span",{className:`text-xs font-semibold ${l}`,children:[N," ",x,"%"]})}function ge({value:d,onChange:i,disabled:x}){const[l,N]=m.useState("");function A(){const a=l.trim().toUpperCase();if(N(""),!!a){if(!he.has(a)){w.error(`"${a}" is not a valid US state code`);return}d.includes(a)||i([...d,a])}}return e.jsxs("div",{className:"flex flex-wrap items-center gap-1",children:[d.length===0&&e.jsx("span",{className:"text-xs text-muted-foreground italic mr-1",children:"national / none"}),d.map(a=>e.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-0.5 rounded-md bg-primary/10 text-primary text-xs font-semibold",children:[a,!x&&e.jsx("button",{onClick:()=>i(d.filter(f=>f!==a)),className:"hover:text-destructive","aria-label":`Remove ${a}`,children:e.jsx(le,{className:"w-3 h-3"})})]},a)),!x&&e.jsx("input",{value:l,onChange:a=>N(a.target.value),onKeyDown:a=>{(a.key==="Enter"||a.key===",")&&(a.preventDefault(),A())},onBlur:A,placeholder:"+ add",maxLength:2,className:"w-12 h-6 px-1 text-xs rounded border border-input bg-transparent uppercase focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"})]})}function ye(){const d=se(),[i,x]=m.useState("idle"),[l,N]=m.useState(null),[A,a]=m.useState(!1),[f,j]=m.useState([]),[y,C]=m.useState({done:0,total:0}),[v,E]=m.useState(null),[k,V]=m.useState(""),[I,W]=m.useState("all"),[z,G]=m.useState(!0);m.useEffect(()=>{try{const t=localStorage.getItem(T);if(t){const s=JSON.parse(t);Array.isArray(s==null?void 0:s.rows)&&s.rows.length>0&&(j(s.rows),x("review"))}}catch(t){console.error("[AdminFillProgramStates] Failed to restore saved review:",t)}},[]);const b=m.useCallback(t=>{try{localStorage.setItem(T,JSON.stringify({savedAt:Date.now(),rows:t}))}catch(s){console.error("[AdminFillProgramStates] Failed to persist review to localStorage:",s)}},[]);async function J(){a(!0);try{const s=(await P.entities.Program.filter({programModule:"ADD_ON"},"-updated_date",1e3)).filter(n=>!Array.isArray(n.availabilityStates)||n.availabilityStates.length===0);N(s.map(n=>n.id))}catch(t){console.error("[AdminFillProgramStates] Failed to load in-scope programs:",t),w.error("Failed to load programs. Check console for details.")}finally{a(!1)}}m.useEffect(()=>{i==="idle"&&J()},[i]);async function K(){var n;if(!l||l.length===0){w.info("No in-scope ADD_ON programs need states.");return}x("analyzing"),C({done:0,total:l.length});const t=[];let s=!1;for(let r=0;r<l.length;r+=O){const c=l.slice(r,r+O);try{const o=await P.functions.invoke("aiFillProgramStates",{dryRun:!0,programIds:c}),p=((n=o==null?void 0:o.data)==null?void 0:n.results)||(o==null?void 0:o.results)||[];for(const u of p)t.push({...u,approved:!u.error&&u.confidence>=.9,committed:!1})}catch(o){s=!0,console.error(`[AdminFillProgramStates] Analyze batch ${r/O+1} failed:`,o)}C({done:Math.min(r+O,l.length),total:l.length}),j([...t]),b(t)}s&&w.error("Some analyze batches failed â see console. Partial results shown."),x("review")}function _(t,s){j(n=>{const r=n.map(c=>c.programId===t?{...c,...s}:c);return b(r),r})}function Y(t,s){_(t,{suggestedStates:s})}function Q(t,s){_(t,{approved:s})}const F=m.useMemo(()=>f.filter(t=>{var s;return!(z&&t.committed||k&&!((s=t.name)!=null&&s.toLowerCase().includes(k.toLowerCase()))||I!=="all"&&D(t)!==I)}),[f,z,k,I]);function X(t){j(s=>{const n=s.map(r=>t(r)?{...r,approved:!0}:r);return b(n),n})}function M(t,s){const n=new Set(t);j(r=>{const c=r.map(o=>n.has(o.programId)?{...o,approved:s}:o);return b(c),c})}const L=f.filter(t=>t.approved&&!t.committed&&!t.error),h=L.filter(t=>t.suggestedStates.length>0);async function Z(){if(h.length===0){w.info("No approved rows with states to commit.");return}x("committing"),C({done:0,total:h.length});const t={written:0,skippedOutOfScope:0,failed:0,errors:[]},s=[];for(let r=0;r<h.length;r+=R){const c=h.slice(r,r+R),o=c.map(p=>({programId:p.programId,states:p.suggestedStates}));try{const p=await P.functions.invoke("aiFillProgramStates",{dryRun:!1,approvals:o}),u=(p==null?void 0:p.data)||p||{};t.written+=u.written||0,t.skippedOutOfScope+=u.skippedOutOfScope||0,t.failed+=u.failed||0,Array.isArray(u.errors)&&t.errors.push(...u.errors),c.forEac
1h(te=>s.push(te.programId))}catch(p){t.failed+=c.length,console.error(`[AdminFillProgramStates] Commit batch ${r/R+1} failed:`,p)}C({done:Math.min(r+R,h.length),total:h.length})}const n=new Set(s);j(r=>{const c=r.map(o=>n.has(o.programId)?{...o,committed:!0}:o);return b(c),c}),E(t),x("done"),t.failed>0?w.error(`${t.failed} record(s) failed to write â see summary.`):w.success(`Wrote states to ${t.written} program(s).`)}function q(){try{localStorage.removeItem(T)}catch(t){console.error("[AdminFillProgramStates] Failed to clear saved review:",t)}j([]),E(null),x("idle")}if(i==="analyzing"||i==="committing"){const t=y.total?Math.round(y.done/y.total*100):0;return e.jsxs("div",{className:"max-w-xl mx-auto py-16 text-center space-y-4",children:[e.jsx(B,{className:"w-8 h-8 animate-spin text-primary mx-auto"}),e.jsx("p",{className:"font-semibold",children:i==="analyzing"?"Analyzing programs with AIâ¦":"Writing approved statesâ¦"}),e.jsx("div",{className:"w-full bg-muted rounded-full h-3 overflow-hidden",children:e.jsx("div",{className:"h-3 rounded-full bg-[#2196D9] transition-all duration-300",style:{width:`${t}%`}})}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[y.done," / ",y.total," (",t,"%)"]})]})}if(i==="done"&&v)return e.jsxs("div",{className:"max-w-xl mx-auto py-12 space-y-6 text-center",children:[e.jsxs("div",{className:"flex items-center justify-center gap-3 p-5 bg-emerald-50 border border-emerald-200 rounded-xl",children:[e.jsx($,{className:"w-8 h-8 text-emerald-600 shrink-0"}),e.jsxs("div",{className:"text-left",children:[e.jsx("p",{className:"font-heading font-bold text-emerald-800 text-lg",children:"Commit complete"}),e.jsxs("p",{className:"text-emerald-700 text-sm",children:["â Wrote ",v.written," · Skipped ",v.skippedOutOfScope," (out of scope) · Failed ",v.failed]})]})]}),v.errors.length>0&&e.jsxs("div",{className:"text-left bg-destructive/10 border border-destructive/30 rounded-lg p-4 space-y-1",children:[e.jsxs("p",{className:"text-sm font-semibold text-destructive",children:[v.errors.length," error(s):"]}),v.errors.slice(0,20).map((t,s)=>e.jsxs("p",{className:"text-xs text-destructive font-mono",children:[t.programId,": ",t.error]},s))]}),e.jsxs("div",{className:"flex gap-3 justify-center",children:[e.jsxs(g,{onClick:()=>x("review"),variant:"outline",className:"gap-2",children:[e.jsx(U,{className:"w-4 h-4"})," Back to review"]}),e.jsxs(g,{onClick:()=>d("/admin/programs?module=ADD_ON"),className:"gap-2",children:["Go to Add-On Programs ",e.jsx(U,{className:"w-4 h-4"})]})]})]});if(i==="idle")return e.jsxs("div",{className:"max-w-2xl mx-auto space-y-6",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-2xl font-heading font-bold flex items-center gap-2",children:[e.jsx(H,{className:"w-6 h-6 text-primary"})," Fill States (AI)"]}),e.jsxs("p",{className:"text-muted-foreground mt-1 text-sm",children:["Smart-fill ",e.jsx("code",{className:"text-xs",children:"availabilityStates"})," on legacy ADD_ON programs whose states are empty. AI suggests states from the program name; you review and edit every row before anything is written. Nothing is published."]})]}),e.jsx("div",{className:"bg-card border border-border rounded-2xl p-8 text-center space-y-6",children:A?e.jsx(B,{className:"w-6 h-6 animate-spin text-primary mx-auto"}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-3xl font-heading font-bold",children:(l==null?void 0:l.length)??0}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"in-scope ADD_ON programs missing states"})]}),e.jsxs(g,{onClick:K,disabled:!l||l.length===0,size:"lg",className:"bg-[#F5A800] hover:bg-[#F5A800]/90 text-[#0A0F1A] font-semibold h-14 px-10 text-base rounded-xl gap-2",children:[e.jsx(re,{className:"w-5 h-5"})," Analyze with AI"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Read-only â runs the LLM in dry-run mode. No writes until you approve."})]})})]});const ee=f.filter(t=>D(t)==="high"&&!t.committed).length;return e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("h1",{className:"text-xl font-heading font-bold flex items-center gap-2",children:[e.jsx(H,{className:"w-5 h-5 text-primary"})," Fill States â Review"]}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-0.5",children:[f.length," analyzed · ",L.length," approved · ",h.length," with states to write"]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(g,{variant:"outline",size:"sm",className:"gap-1.5",onClick:q,children:[e.jsx(ae,{className:"w-4 h-4"})," Discard & re-analyze"]}),e.jsxs(g,{size:"sm",className:"gap-1.5",disabled:h.length===0,onClick:Z,children:[e.jsx($,{className:"w-4 h-4"})," Commit ",h.length," approved"]})]})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"relative flex-1 min-w-0 sm:max-w-xs",children:[e.jsx(ne,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(oe,{placeholder:"Search programs...",value:k,onChange:t=>V(t.target.value),className:"pl-9 h-9"})]}),e.jsxs(ce,{value:I,onValueChange:W,children:[e.jsx(de,{className:"w-40 h-9",children:e.jsx(me,{placeholder:"Confidence"})}),e.jsxs(xe,{children:[e.jsx(S,{value:"all",children:"All confidence"}),e.jsx(S,{value:"high",children:"ð¢ High (â¥90%)"}),e.jsx(S,{value:"mid",children:"ð¡ Mid (60â89%)"}),e.jsx(S,{value:"low",children:"ð´ Low (<60%)"}),e.jsx(S,{value:"error",children:"â Errors"})]})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:z,onChange:t=>G(t.target.checked),className:"w-4 h-4 accent-primary"}),"Hide committed"]}),e.jsxs("div",{className:"flex gap-2 sm:ml-auto",children:[e.jsxs(g,{variant:"outline",size:"sm",onClick:()=>X(t=>D(t)==="high"&&!t.committed),children:["Approve all high-confidence (",ee,")"]}),e.jsx(g,{variant:"outline",size:"sm",onClick:()=>M(F.map(t=>t.programId),!0),children:"Approve visible"}),e.jsx(g,{variant:"outline",size:"sm",onClick:()=>M(F.map(t=>t.programId),!1),children:"Reject visible"})]})]}),e.jsx("div",{className:"bg-card rounded-xl border border-border overflow-x-auto",children:e.jsxs("table",{className:"w-full min-w-[860px]",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-border",children:[e.jsx("th",{className:"text-left py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Program name"}),e.jsx("th",{className:"text-left py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Current"}),e.jsx("th",{className:"text-left py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"AI-suggested (editable)"}),e.jsx("th",{className:"text-left py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Conf."}),e.jsx("th",{className:"text-left py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Reasoning"}),e.jsx("th",{className:"text-center py-3 px-4 text-xs font-semibold text-muted-foreground uppercase tracking-wide",children:"Approve"})]})}),e.jsx("tbody",{children:F.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:6,className:"text-center py-12 text-muted-foreground text-sm",children:"No rows match the current filter."})}):F.map(t=>{var s,n;return e.jsxs("tr",{className:`border-b border-border/50 hover:bg-muted/30 transition-colors ${t.committed?"opacity-50":""}`,children:[e.jsx("td",{className:"py-3 px-4 text-sm font-medium",children:t.name}),e.jsx("td",{className:"py-3 px-4 text-xs text-muted-foreground",children:(s=t.currentStates)!=null&&s.length?t.currentStates.join(", "):"â"}),e.jsxs("td",{className:"py-3 px-4",children:[t.error?e.jsx("span",{className:"text-xs text-destructive",children:t.error}):e.jsx(ge,{value:t.suggestedStates,onChange:r=>Y(t.programId,r),disabled:t.committed}
1),((n=t.invalidStates)==null?void 0:n.length)>0&&e.jsxs("p",{className:"text-[11px] text-amber-600 mt-1",children:["AI returned invalid: ",t.invalidStates.join(", ")]})]}),e.jsx("td",{className:"py-3 px-4",children:e.jsx(ue,{row:t})}),e.jsx("td",{className:"py-3 px-4 text-xs text-muted-foreground max-w-[280px]",children:t.reasoning||"â"}),e.jsx("td",{className:"py-3 px-4 text-center",children:e.jsx("input",{type:"checkbox",className:"w-4 h-4 accent-primary disabled:opacity-40",checked:!!t.approved,disabled:t.committed||!!t.error,onChange:r=>Q(t.programId,r.target.checked)})})]},t.programId)})})]})}),e.jsxs("p",{className:"text-xs text-muted-foreground flex items-center gap-1.5",children:[e.jsx(pe,{className:"w-3.5 h-3.5"}),"Approved rows with no states (national / no restriction) are left unwritten â the field is already empty. This tool never changes ",e.jsx("code",{children:"programStatus"}),"; newly state-filled DRAFTs stay DRAFT."]})]})}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.