(()=>{var S=performance.now();function i(n){let e=document.getElementById(n);if(!e)throw new Error(`Missing required element: #${n}`);return e}function l(n,e,d){n.dataset.state=e,n.textContent=d}function m(n,e){for(let d of n)d.disabled=e}function w(n){return`${JSON.stringify(n,null,2)}
`}function v(){let n=i("ast-input"),e=i("fixture-select"),d=i("generate"),c=i("upload-button"),M=i("upload-json"),u=i("download-link"),s=i("status"),x=i("page-count"),y=i("boot-ms"),L=i("render-ms"),E=i("fixture-picker-label"),f=null,g=0,p=()=>{f&&(VMPrintPipeline.revokeDownloadUrl(f),f=null),u.removeAttribute("href"),u.setAttribute("aria-disabled","true"),u.textContent="Download PDF",x.textContent=""},D=()=>{let r=performance.now()-S;y.textContent=`${r.toFixed(1)} ms`},P=async(r,o=!0)=>{let t=++g;E.dataset.loading="true",e.disabled=!0,m([d,c],!0),l(s,"rendering",`Loading built-in fixture "${r}"...`);try{let a=await VMPrintPipeline.getBuiltinFixtureDocument(r);if(t!==g)return;n.value=w(a),o&&l(s,"idle",`Loaded built-in fixture "${r}".`),p()}catch(a){if(t!==g)return;l(s,"error",String(a))}finally{if(t!==g)return;E.dataset.loading="false",e.disabled=!1,m([d,c],!1)}},b=()=>{let r=VMPrintPipeline.getBuiltinFixturePresets();e.innerHTML="";for(let o of r){let t=document.createElement("option");t.value=o.id,t.textContent=o.label,t.title=o.description,e.appendChild(t)}r.length>0&&(e.value=r[0].id)};e.addEventListener("change",()=>{e.value&&P(e.value)}),c.addEventListener("click",()=>{M.click()}),M.addEventListener("change",async r=>{let o=r.target,t=o.files?.[0];if(o.value="",!!t)try{let a=await t.text();n.value=`${a.trim()}
`,p(),l(s,"idle",`Loaded "${t.name}".`)}catch(a){l(s,"error",`Failed to read file: ${String(a)}`)}}),d.addEventListener("click",async()=>{m([d,c],!0),l(s,"rendering","Rendering PDF..."),p();let r=performance.now();try{let o=VMPrintPipeline.parseDocumentJson(n.value),{pdfBytes:t,pageCount:a}=await VMPrintPipeline.renderDocumentToPdfBytes(o),B=performance.now()-r,T=VMPrintPipeline.createDownloadUrl(t);f=T,u.href=T,u.setAttribute("aria-disabled","false"),u.textContent="Download PDF",x.textContent=`${a} page${a===1?"":"s"}`,L.textContent=`${B.toFixed(1)} ms`,l(s,"success","Render complete. Download your PDF.")}catch(o){let t=performance.now()-r;L.textContent=`${t.toFixed(1)} ms (failed)`,l(s,"error",String(o))}finally{m([d,c],!1)}}),b(),e.value?P(e.value,!1).then(()=>{l(s,"idle","Built-in fixture loaded. You can edit the AST JSON and generate.")}):(n.value=w(VMPrintPipeline.SAMPLE_DOCUMENT),l(s,"idle","Sample loaded. You can edit the AST JSON and generate.")),D()}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",v):v();})();
