(()=>{var Z=performance.now();function o(i){let n=document.getElementById(i);if(!n)throw new Error(`Missing required element: #${i}`);return n}function l(i,n,m){i.dataset.state=n,i.textContent=m}function C(i,n){for(let m of i)m.disabled=n}function z(i){return`${JSON.stringify(i,null,2)}
`}function G(){let i=o("ast-input"),n=o("fixture-select"),m=o("upload-button"),v=o("copy-json"),B=o("upload-json"),N=o("preview-scale"),H=o("preview-dpi"),R=o("text-render-mode"),p=o("preview-canvas"),k=o("previous-page"),D=o("next-page"),j=o("current-page"),a=o("status"),I=o("page-count"),W=o("boot-ms"),A=o("layout-ms"),O=o("render-ms"),V=o("fixture-picker-label"),_=o("inline-error"),X=o("dispatch-callout"),S=0,u="",s=null,c=0,P=!1,w=null,g=null,E="builtin",h=e=>{_.textContent=e??"",_.hidden=!e},T=()=>E==="builtin"&&g!==null&&VMPrintPipeline.AI_GENERATED_FIXTURE_IDS.has(g),f=()=>{X.hidden=!T()},L=()=>{let e=s?.pageCount??0;j.textContent=e>0?`Page ${c+1} of ${e}`:"No pages rendered",k.disabled=!s||c<=0,D.disabled=!s||c>=e-1},x=()=>{s=null,c=0;let e=p.getContext("2d");e&&e.clearRect(0,0,p.width,p.height),p.width=1,p.height=1,I.textContent="",A.textContent="-",L()},M=async()=>{if(!s){L();return}let e=Number(N.value||"1"),t=H.value,r=t==="auto"?void 0:Number(t);await s.renderPage(c,p,e,r),L()},K=()=>{W.textContent=`${(performance.now()-Z).toFixed(1)} ms`},b=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`,q=e=>{if(!e){u="";return}if(e.phase==="cache-hit"){u=`Fonts: ${e.fileName} loaded from cache`;return}if(e.phase==="complete"){u=`Fonts: ${e.fileName} loaded (${b(e.loadedBytes)})`;return}if(e.phase==="finalizing"){u=`Fonts: ${e.fileName} downloaded, finalizing...`;return}if(e.phase==="caching"){u=`Fonts: ${e.fileName} saving to cache...`;return}let t=Number.isFinite(e.percent)?`${Number(e.percent).toFixed(0)}%`:`${b(e.loadedBytes)} downloaded`,r=Number.isFinite(e.totalBytes)?` / ${b(Number(e.totalBytes))}`:"";u=`Fonts: downloading ${e.fileName} (${t}${r})`},y=async(e=!1)=>{if(P)return;let t;try{t=VMPrintPipeline.parseDocumentJson(i.value)}catch{e||(h("JSON syntax error \u2014 check the document structure."),l(a,"error","JSON parse error. Fix the syntax and try again."));return}if(h(null),P=!0,!e){C([m],!0);let d=T()?"Rendering sample document\u2026 first visit may take longer while fonts download and cache.":"Rendering pages to canvas\u2026";l(a,"rendering",d),x()}u="";let r=performance.now();try{s=await VMPrintPipeline.createCanvasPreviewSession(t,{textRenderMode:R.value}),c=0,await M(),I.textContent=`${s.pageCount} page${s.pageCount===1?"":"s"}`,A.textContent=`${s.layoutMs.toFixed(1)} ms`,O.textContent=`${s.renderMs.toFixed(1)} ms`;let F=u?` ${u}.`:"";l(a,"success",`Render complete. Single-page canvas preview is ready.${F}`)}catch(d){let F=performance.now()-r;O.textContent=`${F.toFixed(1)} ms (failed)`;let $=String(d);h(`Render error: ${$}`),e?l(a,"error",`Auto-refresh failed: ${$}`):(l(a,"error",$),x())}finally{P=!1,e||C([m],!1)}},J=e=>{let t=n.querySelector('option[value="custom"]');t||(t=document.createElement("option"),t.value="custom",n.insertBefore(t,n.firstChild)),t.textContent=e,n.value="custom",f()},Q=()=>{n.querySelector('option[value="custom"]')?.remove()},U=async e=>{let t=++S;E="builtin",g=e,f(),V.dataset.loading="true",n.disabled=!0,C([m],!0),l(a,"rendering",VMPrintPipeline.AI_GENERATED_FIXTURE_IDS.has(e)?"Loading the sample document\u2026 first visit may take a little longer while example fonts download and cache.":`Loading \u201C${e}\u201D\u2026`),Q();try{let r=await VMPrintPipeline.getBuiltinFixtureDocument(e);if(t!==S)return;i.value=z(r),h(null),E="builtin",g=e,f(),x()}catch(r){if(t!==S)return;l(a,"error",String(r));return}finally{if(t!==S)return;V.dataset.loading="false",n.disabled=!1,C([m],!1)}await y(!1)},Y=()=>{let e=VMPrintPipeline.getBuiltinFixturePresets();n.innerHTML="";for(let t of e){let r=document.createElement("option");r.value=t.id,r.textContent=t.label,r.title=t.description,n.appendChild(r)}e.length>0&&(n.value=e[0].id)};n.addEventListener("change",()=>{let e=n.value;!e||e==="custom"||U(e)}),m.addEventListener("click",()=>B.click()),N.addEventListener("change",()=>{s&&M().catch(e=>l(a,"error",String(e)))}),H.addEventListener("change",()=>{s&&M().catch(e=>l(a,"error",String(e)))}),R.addEventListener("change",()=>{x(),y(!1)}),k.addEventListener("click",()=>{!s||c<=0||(c-=1,M().catch(e=>l(a,"error",String(e))))}),D.addEventListener("click",()=>{!s||c>=s.pageCount-1||(c+=1,M().catch(e=>l(a,"error",String(e))))}),window.addEventListener("vmprint:webfont-progress",e=>{let t=e;if(t.detail&&(q(t.detail),a.dataset.state==="rendering")){let r=T()?"Loading sample assets\u2026 ":"Preparing preview\u2026 ",d=T()?"Loading fonts for the sample \u2014 first visit may take a moment.":"Rendering pages to canvas\u2026";l(a,"rendering",`${r}${u||d}`)}}),B.addEventListener("change",async e=>{let t=e.target,r=t.files?.[0];if(t.value="",!!r)try{let d=await r.text();i.value=`${d.trim()}
`,h(null),E="custom",g=null,J(`\u2191 ${r.name}`),f(),x(),await y(!1)}catch(d){l(a,"error",`Failed to read file: ${String(d)}`)}}),i.addEventListener("paste",()=>{setTimeout(()=>{E="custom",g=null,J("\u2191 Custom (pasted)"),f()},0)}),i.addEventListener("input",()=>{w!==null&&clearTimeout(w),w=setTimeout(()=>{w=null,y(!0)},1500)}),v.addEventListener("click",async()=>{let e=v.textContent??"Copy JSON";try{await navigator.clipboard.writeText(i.value),v.textContent="Copied!"}catch{v.textContent="Failed"}setTimeout(()=>{v.textContent=e},1800)}),Y(),L(),f(),n.value?U(n.value):(i.value=z(VMPrintPipeline.SAMPLE_DOCUMENT),l(a,"idle","Sample loaded. Edit the AST JSON above to render.")),K()}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",G):G();})();
