var O=Object.defineProperty;var V=(t,e,i)=>e in t?O(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i;var b=(t,e,i)=>V(t,typeof e!="symbol"?e+"":e,i);import{u as I}from"./useEvent-WUlsM6MC.js";import{o as j}from"./cells-CCtxWKxf.js";import{t as T}from"./compiler-runtime-B3qBwwSJ.js";import{c as k,m as E}from"./useEventListener-CjXriKgk.js";import{i as N}from"./utils-CORJ8yYa.js";import{h as P,x as _}from"./config-D9X9QKiL.js";import{r as z}from"./requests-DZy_SQ_v.js";import{f as D}from"./layout-DnAy5Tn0.js";import{s as $,u as B}from"./download-BTm4CTKr.js";import{t as H}from"./Deferred-D2igisxY.js";import{t as L}from"./use-toast-BhMe50aT.js";import{t as x}from"./useInterval-lamrIyKJ.js";var R=class{constructor(){b(this,"capturedInputs",new Map);b(this,"inFlight",new Map)}cancelEntry(t){t.controller.abort(),t.deferred.status==="pending"&&t.deferred.resolve(void 0)}needsCapture(t,e){if(this.capturedInputs.get(t)===e)return!1;let i=this.inFlight.get(t);return!(i&&i.inputValue===e)}waitForInFlight(t,e){let i=this.inFlight.get(t);return i&&i.inputValue===e?i.deferred.promise:null}startCapture(t,e){let i=this.inFlight.get(t);i&&this.cancelEntry(i);let n=new AbortController,l=new H,u={controller:n,inputValue:e,deferred:l};return this.inFlight.set(t,u),{signal:n.signal,markCaptured:p=>{this.inFlight.get(t)===u&&(l.resolve(p),this.capturedInputs.set(t,e),this.inFlight.delete(t))},markFailed:()=>{this.inFlight.get(t)===u&&(l.resolve(void 0),this.inFlight.delete(t))}}}prune(t){for(let e of this.capturedInputs.keys())t.has(e)||this.capturedInputs.delete(e);for(let[e,i]of this.inFlight)t.has(e)||(this.cancelEntry(i),this.inFlight.delete(e))}get isCapturing(){return this.inFlight.size>0}abort(){for(let t of this.inFlight.values())this.cancelEntry(t);this.inFlight.clear()}reset(){this.abort(),this.capturedInputs.clear()}},Y=T(),M=5e3;function q(){let t=(0,Y.c)(14),e=I(N),{state:i}=I(P),n=e.auto_download.includes("markdown"),l=e.auto_download.includes("html"),u=e.auto_download.includes("ipynb"),p=i===_.OPEN,m=!n||!p,d=!l||!p,h=!u||!p,{autoExportAsHTML:a,autoExportAsIPYNB:s,autoExportAsMarkdown:r,updateCellOutputs:o}=z(),c=S(),f;t[0]===r?f=t[1]:(f=async()=>{await r({download:!1})},t[0]=r,t[1]=f);let g;t[2]===m?g=t[3]:(g={delayMs:M,whenVisible:!0,disabled:m},t[2]=m,t[3]=g),x(f,g);let w;t[4]===a?w=t[5]:(w=async()=>{await a({download:!1,includeCode:!0,files:D.INSTANCE.filenames()})},t[4]=a,t[5]=w);let v;t[6]===d?v=t[7]:(v={delayMs:M,whenVisible:!0,disabled:d},t[6]=d,t[7]=v),x(w,v);let F;t[8]!==s||t[9]!==c||t[10]!==o?(F=async()=>{await A({takeScreenshots:()=>c({progress:B.indeterminate()}),updateCellOutputs:o}),await s({download:!1})},t[8]=s,t[9]=c,t[10]=o,t[11]=F):F=t[11];let y;t[12]===h?y=t[13]:(y={delayMs:M,whenVisible:!0,disabled:h,skipIfRunning:!0},t[12]=h,t[13]=y),x(F,y)}var G=new Set(["text/html","application/vnd.vegalite.v5+json","application/vnd.vega.v5+json","application/vnd.vegalite.v6+json","application/vnd.vega.v6+json"]);const C=new R;function S(){let t=I(j);return async e=>{var d,h;let{progress:i}=e,n=new Set(k.keys(t));C.prune(n);let l=[],u=[];for(let[a,s]of k.entries(t)){let r=(d=s.output)==null?void 0:d.data;if((h=s.output)!=null&&h.mimetype&&G.has(s.output.mimetype)&&r)if(C.needsCapture(a,r))l.push([a,r]);else{let o=C.waitForInFlight(a,r);o&&u.push({cellId:a,promise:o})}}if(l.length===0&&u.length===0)return{};l.length>0&&i.addTotal(l.length);let p={};for(let[a,s]of l){let r=C.startCapture(a,s);try{let o=await $(a);if(r.signal.aborted)continue;if(!o){E.error(`Failed to capture screenshot for cell ${a}`),r.markFailed();continue}let c=["image/png",o];p[a]=c,r.markCaptured(c)}catch(o){E.error(`Error screenshotting cell ${a}:`,o),r.markFailed()}finally{i.increment(1)}}let m=await Promise.allSettled(u.map(({promise:a})=>a));for(let[a,{cellId:s}]of u.entries()){let r=m[a];r.status==="fulfilled"&&r.value&&(p[s]=r.value)}return p}}async function A(t){let{takeScreenshots:e,updateCellOutputs:i}=t;try{let n=await e();k.size(n)>0&&await i({cellIdsToOutput:n})}catch(n){E.error("Error updating cell outputs with screenshots:",n),L({title:"Failed to capture cell outputs",description:"Some outputs may not appear in the PDF. Continuing with export.",variant:"danger"})}}export{q as n,S as r,A as t};
