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