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