import{b as T,r as x,u as $,c as y,j as e}from"./query-Bw69rE6b.js";import{P as I}from"./PageHeader-qtVByY5c.js";import{E as M}from"./EmptyState-CgO0CCgi.js";import{J as Q}from"./JobProgress-CArMtskB.js";import{C as L,b as F}from"./Card-nuKO-lKR.js";import{S as J}from"./StatusPill-DBEWzsQ3.js";import{d as q,j as O,B as j,Y as B,b as m,q as H,X as N,e as v,I as K,C as R,c as _,V as D,n as V}from"./index-CoUu6FiM.js";import"./react-CQKYBN-P.js";import{A as X}from"./AlertDialog-IkMNJ6QG.js";import{E as Y}from"./eye-DjFgzw_3.js";import"./Badge-DwoQNRW3.js";/**
 * @license lucide-react v0.577.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const G=[["polyline",{points:"22 12 16 12 14 15 10 15 8 12 2 12",key:"o97t9d"}],["path",{d:"M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z",key:"oot6mr"}]],U=q("inbox",G);/**
 * @license lucide-react v0.577.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const W=[["path",{d:"M5 5a2 2 0 0 1 3.008-1.728l11.997 6.998a2 2 0 0 1 .003 3.458l-12 7A2 2 0 0 1 5 19z",key:"10ikf1"}]],Z=q("play",W);function he(){const t=T(),a=O(),[r,n]=x.useState(null),[i,h]=x.useState(!1),[o,f]=x.useState(""),[l,k]=x.useState(null),[w,C]=x.useState(!1),c=$({queryKey:["pending"],queryFn:()=>m("/api/pending"),retry:!1}),z=y({mutationFn:s=>m(`/api/pending/${s}`,{method:"DELETE"}),onSuccess:()=>{t.invalidateQueries({queryKey:["pending"]}),a.push({title:"Row removed",tone:"info",duration:2200})}}),P=y({mutationFn:()=>m("/api/pending/clear",{method:"POST"}),onSuccess:()=>{t.invalidateQueries({queryKey:["pending"]}),a.push({title:"Pending queue cleared",tone:"success",duration:2500}),h(!1)},onError:s=>{a.push({title:"Could not clear queue",description:s.message,tone:"error"})}}),S=y({mutationFn:s=>m(`/api/pending/${s.idx}`,{method:"PATCH",body:JSON.stringify({final_description:s.final_description})}),onSuccess:()=>t.invalidateQueries({queryKey:["pending"]})});async function A(){try{const s=await m("/api/pending/apply",{method:"POST",body:JSON.stringify({})});n(s.job_id),a.push({title:"Apply started",description:`${c.data?.count??0} ${c.data?.count===1?"row":"rows"} streaming…`,tone:"info",duration:2200})}catch(s){a.push({title:"Apply failed",description:s instanceof Error?s.message:"Apply failed.",tone:"error"})}}async function E(){C(!0);try{const s=await m("/api/pending/preview",{method:"POST",body:JSON.stringify({})});k(s.events)}catch(s){a.push({title:"Preview failed",description:s instanceof Error?s.message:"Preview failed.",tone:"error"})}finally{C(!1)}}const b=x.useMemo(()=>{const s=c.data?.pending??[],p=o.trim().toLowerCase();return p?s.filter(u=>[u.schema,u.table,u.column??"",u.final_description,u.confidence,u.asset_kind].join(" ").toLowerCase().includes(p)):s},[c.data?.pending,o]),d=c.data?.count??0,g=b.length;return e.jsxs(e.Fragment,{children:[e.jsx(I,{title:"Pending review",breadcrumbs:[{label:"Pending"}],actions:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(j,{variant:"subtle",size:"md",leadingIcon:e.jsx(B,{size:14}),disabled:!d,onClick:()=>h(!0),children:"Clear all"}),e.jsx(j,{variant:"subtle",size:"md",leadingIcon:e.jsx(Y,{size:14}),disabled:!d||w,onClick:E,children:w?"Previewing…":"Preview SQL"}),e.jsxs(j,{variant:"primary",size:"md",leadingIcon:e.jsx(Z,{size:14}),disabled:!d||!!r,onClick:A,children:["Apply (",d,")"]})]})}),l!==null&&e.jsx(te,{events:l,onClose:()=>k(null)}),r&&e.jsx("div",{className:"mb-6",children:e.jsx(Q,{jobId:r,kind:"apply",onCancel:async()=>{try{await m(`/api/apply/${r}/cancel`,{method:"POST"})}catch{}},onTerminal:()=>{t.invalidateQueries({queryKey:["pending"]})}})}),e.jsxs(L,{children:[e.jsxs("header",{className:"flex items-center justify-between gap-3 border-b border-border px-5 py-3",children:[e.jsxs("div",{className:"text-sm text-ink-muted",children:[e.jsx("span",{className:"font-medium text-ink",children:c.isLoading?"—":g})," ",g===1?"entry":"entries",o&&d!==g&&e.jsxs("span",{className:"ml-2 text-xs text-ink-dim",children:["(of ",d," total)"]})]}),e.jsxs("div",{className:"relative flex h-8 min-w-[16rem] items-center rounded-md border border-border bg-surface-raised pl-2.5 pr-2 focus-within:border-accent focus-within:ring-2 focus-within:ring-accent/20",children:[e.jsx(H,{size:13,className:"text-ink-dim"}),e.jsx("input",{type:"search",value:o,onChange:s=>f(s.target.value),placeholder:"Search schema, table, or text…",className:"ml-1.5 h-full flex-1 bg-transparent text-sm text-ink placeholder:text-ink-dim focus:outline-none"}),o&&e.jsx("button",{type:"button",onClick:()=>f(""),"aria-label":"Clear search",className:"ml-1 rounded p-0.5 text-ink-dim hover:bg-surface-subtle hover:text-ink",children:e.jsx(N,{size:12})})]})]}),e.jsx(F,{className:"p-0",children:c.isLoading?e.jsx("ul",{className:"divide-y divide-border",children:Array.from({length:4}).map((s,p)=>e.jsxs("li",{className:"px-5 py-3",children:[e.jsx(v,{className:"h-3 w-1/3"}),e.jsx(v,{className:"mt-2 h-3 w-full"}),e.jsx(v,{className:"mt-1 h-3 w-4/5"})]},p))}):c.error?e.jsx("div",{className:"px-5 py-6 text-sm text-critical",children:c.error.message}):b.length?e.jsx("ul",{className:"divide-y divide-border",children:b.map(s=>e.jsx(ee,{row:s,onRemove:()=>z.mutate(s.idx),onPick:p=>S.mutate({idx:s.idx,final_description:p}),isPicking:S.isPending},s.idx))}):o?e.jsxs("div",{className:"px-5 py-6 text-sm text-ink-dim",children:["No rows match ",e.jsxs("span",{className:"font-mono",children:['"',o,'"']}),"."]}):e.jsx("div",{className:"px-5 py-5",children:e.jsx(M,{icon:U,title:"Queue is empty",description:"Approved descriptions land here when /run finishes or /apply marks rows for write-back.",compact:!0})})})]}),e.jsx(X,{open:i,onClose:()=>h(!1),onConfirm:()=>P.mutate(),loading:P.isPending,title:"Clear the entire pending queue?",description:`${d} ${d===1?"row":"rows"} will be removed without being written to the database. This action cannot be undone.`,confirmLabel:"Clear queue"})]})}function ee({row:t,onRemove:a,onPick:r,isPicking:n}){const i=se(t.alternatives),h=i.includes(t.final_description)?i:[t.final_description,...i];return e.jsx("li",{className:"px-5 py-3",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("div",{className:"flex flex-wrap items-center gap-2 text-xs",children:[e.jsx("span",{className:"font-mono",children:`${t.schema}.${t.table}`}),t.column&&e.jsxs("span",{className:"font-mono text-ink-dim",children:[".",t.column]}),e.jsx(J,{tone:t.confidence==="high"?"positive":t.confidence==="low"?"warning":"accent",children:t.confidence}),t.logprob_score!=null&&e.jsxs("span",{className:"inline-flex items-center gap-1 font-mono text-[10px] text-ink-dim",children:["logprob ",t.logprob_score.toFixed(3),e.jsx(K,{size:10,text:"Average log-probability the model assigned to its chosen description. Higher (closer to 0) = more confident; very negative = the model was guessing."})]}),e.jsx("span",{className:"ml-auto text-[10px] uppercase tracking-wider text-ink-dim",children:t.source||t.asset_kind})]}),e.jsx("div",{className:"mt-2 space-y-1",children:h.map((o,f)=>{const l=o===t.final_description;return e.jsxs("button",{type:"button",onClick:()=>{!l&&!n&&r(o)},disabled:l||n,className:_("flex w-full items-start gap-2 rounded-md border px-2.5 py-1.5 text-left text-xs transition-colors duration-fast",l?"border-accent/40 bg-accent-soft/40 text-ink":"border-border text-ink-muted hover:border-accent/40 hover:bg-surface-subtle/50 hover:text-ink",n&&"opacity-60"),title:l?"This is the chosen alternative":"Make this the chosen alternative",children:[e.jsx("span",{className:_("mt-0.5 inline-flex h-4 w-4 shrink-0 items-center justify-center rounded text-[10px] font-semibold",l?"bg-accent text-white":"bg-surface-subtle text-ink-dim"),children:l?e.jsx(R,{size:10}):String.fromCharCode(65+f)}),e.jsx("span",{className:"leading-relaxed",children:o})]},`${t.idx}-${f}`)})})]}),e.jsx(D,{content:"Remove from queue",children:e.jsx(V,{icon:e.jsx(N,{size:13}),label:"Remove from queue",size:"sm",onClick:a})})]})})}function te({events:t,onClose:a}){const r=t.filter(i=>i.sql_template),n=t.filter(i=>i.skipped_reason||i.error);return e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 px-4 pt-12",role:"dialog","aria-modal":"true","aria-labelledby":"preview-modal-title",onClick:i=>{i.target===i.currentTarget&&a()},children:e.jsxs("div",{className:"max-h-[80vh] w-full max-w-3xl overflow-hidden rounded-lg border border-border bg-surface-raised shadow-xl",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-border px-4 py-3",children:[e.jsxs("div",{children:[e.jsx("h2",{id:"preview-modal-title",className:"text-lg font-semibold",children:"Apply preview (dry-run)"}),e.jsxs("p",{className:"text-xs text-text-soft",children:[r.length," statement",r.length===1?"":"s"," would run; ",n.length," skipped. Nothing has been written to the database."]})]}),e.jsx("button",{type:"button",onClick:a,"aria-label":"Close preview",className:"rounded p-1 hover:bg-surface",children:e.jsx(N,{size:16})})]}),e.jsx("div",{className:"max-h-[60vh] overflow-y-auto px-4 py-2",children:t.length===0?e.jsx("p",{className:"py-8 text-center text-sm text-text-soft",children:"No pending rows to preview."}):e.jsx("ul",{className:"divide-y divide-border text-sm",children:t.map(i=>e.jsxs("li",{className:"py-3",children:[e.jsx("div",{className:"font-mono text-xs text-text-soft",children:[i.schema,i.table,i.column].filter(Boolean).join(".")}),i.sql_template?e.jsx("pre",{className:"mt-1 overflow-x-auto rounded bg-surface p-2 font-mono text-xs",children:i.sql_template}):e.jsx("p",{className:"mt-1 text-xs italic text-text-soft",children:i.skipped_reason??i.error??"(no preview)"})]},i.idx))})})]})})}function se(t){const a=[];for(const n of t??[])if(typeof n=="string")a.push(n);else if(n&&typeof n=="object"){const i=n.description;typeof i=="string"&&a.push(i)}const r=new Set;return a.filter(n=>r.has(n)?!1:(r.add(n),!0))}export{he as default};
