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