'use client';
import {useState} from 'react';
import {Button} from '@/components/ui/button';
import {Textarea} from '@/components/ui/textarea';
export default function Runner({tool}:{tool:string}){const [input,setInput]=useState(tool==='citation-audit'?'https://doi.org/10.1038/s41586-023-06747-5\ndoi:10.1038/s41586-023-06747-5\nhttps://example.org/report?utm_source=newsletter\nhttps://example.org/report':'{"claim":"ready for review","evidence":[]}'),[result,setResult]=useState<any>(null),[busy,setBusy]=useState(false),[error,setError]=useState('');async function run(){setBusy(true);setError('');try{const r=await fetch('/api/v1/utilities/'+tool,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(tool==='citation-audit'?{sources:input.split('\n').map(s=>s.trim()).filter(Boolean)}:{text:input,format:true})});const j=await r.json();if(!r.ok)throw Error(j.error?.message||'Unable to check input');setResult(j.data)}catch(e:any){setError(e.message)}finally{setBusy(false)}}return <div className="panel panel-body"><label htmlFor="utility-input">{tool==='citation-audit'?'One source URL or DOI per line':'JSON to check'}</label><Textarea id="utility-input" value={input} onChange={e=>setInput(e.target.value)} rows={7} style={{fontFamily:'monospace',fontSize:14,margin:'12px 0'}}/><Button onClick={run} disabled={busy}>{busy?'Checking…':tool==='citation-audit'?'Find duplicate citations':'Check JSON syntax'}</Button><p className="meta" style={{marginTop:12}}>No account required. Input is processed without being stored in the application database. Do not paste secrets.</p>{error&&<p className="error" role="alert">{error}</p>}{result&&<div aria-live="polite"><h2 style={{marginTop:24}}>Result</h2><pre>{JSON.stringify(result,null,2)}</pre></div>}</div>}
