// @bun
var{defineProperty:_,getOwnPropertyNames:p,getOwnPropertyDescriptor:C}=Object,l=Object.prototype.hasOwnProperty;function m(q){return this[q]}var t=(q)=>{var z=(A??=new WeakMap).get(q),B;if(z)return z;if(z=_({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var J of p(q))if(!l.call(z,J))_(z,J,{get:m.bind(q,J),enumerable:!(B=C(q,J))||B.enumerable})}return A.set(q,z),z},A;var y=(q)=>q;function g(q,z){this[q]=y.bind(null,z)}var c=(q,z)=>{for(var B in z)_(q,B,{get:z[B],enumerable:!0,configurable:!0,set:g.bind(z,B)})};var $=(q,z)=>()=>(q&&(z=q(q=0)),z);var e=import.meta.require;import{createHash as w}from"crypto";import{readFile as f,stat as H}from"fs/promises";async function M(q){try{let z=await f(q);return w("sha256").update(z).digest("hex").slice(0,16)}catch{return""}}function Jq(q){return w("sha256").update(q).digest("hex").slice(0,16)}function K(q){return w("sha256").update(q).digest("hex").slice(0,16)}function Y(q,z){let B=JSON.stringify(z,Object.keys(z).sort());return`${K(q)}-${K(B)}`}async function G(q){try{return(await H(q)).mtimeMs}catch{return 0}}async function S(q){try{return await H(q),!0}catch{return!1}}var E=()=>{};import{mkdir as h,readFile as j,writeFile as s,readdir as x,rm as v,stat as n}from"fs/promises";import{join as W,dirname as r}from"path";async function o(q){try{await h(q,{recursive:!0})}catch{}}function F(q,z,B){return W(q,B,`${z}.json`)}async function b(q,z,B){if((Date.now()-q.createdAt)/86400000>B)return!1;if(await G(z)!==q.srcMtime)return!1;return!0}async function I(q,z,B,J=X){if(!J.enabled)return null;let L=Y(q,z),N=Q.get(L);if(N){if(await b(N,q,J.maxAge))return N.lastAccessedAt=Date.now(),N.data;Q.delete(L)}let T=F(J.directory,L,B);if(await S(T))try{let O=await j(T,"utf-8"),R=JSON.parse(O);if(await b(R,q,J.maxAge))return R.lastAccessedAt=Date.now(),Q.set(L,R),R.data}catch{}return null}async function k(q,z,B,J,L=X){if(!L.enabled)return;let N=Y(q,z),T=await M(q),O=await G(q),R={key:N,src:q,srcHash:T,srcMtime:O,optionsHash:Y("",z),data:B,createdAt:Date.now(),lastAccessedAt:Date.now()};Q.set(N,R);let U=F(L.directory,N,J);try{await o(r(U)),await s(U,JSON.stringify(R,null,2))}catch(d){console.warn(`[stx-media] Failed to write cache: ${d}`)}}async function Rq(q,z,B=X){for(let[J,L]of Q.entries())if(L.src===q)Q.delete(J)}async function D(q,z=X){if(q==="all")Q.clear();else Q.clear();let B=q==="all"?["image","video"]:[q];for(let J of B){let L=W(z.directory,J);try{await v(L,{recursive:!0,force:!0})}catch{}}}async function Tq(q=X){let z={memoryEntries:Q.size,diskEntries:{image:0,video:0},totalSizeMB:0};for(let B of["image","video"]){let J=W(q.directory,B);try{let L=await x(J);z.diskEntries[B]=L.length;for(let N of L){let T=W(J,N),O=await n(T);z.totalSizeMB+=O.size/1048576}}catch{}}return z}async function Uq(q=X){let z=0,B=q.maxAge*24*60*60*1000;for(let J of["image","video"]){let L=W(q.directory,J);try{let N=await x(L);for(let T of N){let O=W(L,T);try{let R=await j(O,"utf-8"),U=JSON.parse(R);if(Date.now()-U.createdAt>B)await v(O),Q.delete(U.key),z++}catch{await v(O).catch(()=>{}),z++}}}catch{}}return z}var Q,X;var P=$(()=>{E();Q=new Map,X={enabled:!0,directory:".stx/media-cache",maxAge:30,maxSize:500}});class u{config;constructor(q={}){this.config={...a,...q}}async get(q,z){return I(q,z,"image",this.config)}async set(q,z,B){return k(q,z,B,"image",this.config)}async clear(){return D("image",this.config)}updateConfig(q){this.config={...this.config,...q}}}function V(q){if(!Z)Z=new u(q);else if(q)Z.updateConfig(q);return Z}async function Yq(q,z,B){let J=B?{directory:`${B.outputDir}/../.cache`}:{};return V(J).get(q,z)}async function Zq(q,z,B,J){let L=J?{directory:`${J.outputDir}/../.cache`}:{};return V(L).set(q,z,B)}async function _q(){return V().clear()}var a,Z=null;var i=$(()=>{P();a={enabled:!0,directory:".stx/media-cache",maxAge:30,maxSize:500,cacheVariants:!0,cachePlaceholders:!0}});i();export{Zq as setCachedImageResult,Yq as getCachedImageResult,_q as clearImageCache,u as ImageProcessorCache};
