// @bun
var{defineProperty:Z,getOwnPropertyNames:m,getOwnPropertyDescriptor:p}=Object,C=Object.prototype.hasOwnProperty;function l(q){return this[q]}var i=(q)=>{var z=(A??=new WeakMap).get(q),B;if(z)return z;if(z=Z({},"__esModule",{value:!0}),q&&typeof q==="object"||typeof q==="function"){for(var I of m(q))if(!C.call(z,I))Z(z,I,{get:l.bind(q,I),enumerable:!(B=p(q,I))||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)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 $}from"crypto";import{readFile as f,stat as H}from"fs/promises";async function M(q){try{let z=await f(q);return $("sha256").update(z).digest("hex").slice(0,16)}catch{return""}}function Iq(q){return $("sha256").update(q).digest("hex").slice(0,16)}function K(q){return $("sha256").update(q).digest("hex").slice(0,16)}function X(q,z){let B=JSON.stringify(z,Object.keys(z).sort());return`${K(q)}-${K(B)}`}async function w(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 G,stat as n}from"fs/promises";import{join as U,dirname as r}from"path";async function a(q){try{await h(q,{recursive:!0})}catch{}}function F(q,z,B){return U(q,B,`${z}.json`)}async function b(q,z,B){if((Date.now()-q.createdAt)/86400000>B)return!1;if(await w(z)!==q.srcMtime)return!1;return!0}async function k(q,z,B,I=W){if(!I.enabled)return null;let J=X(q,z),L=O.get(J);if(L){if(await b(L,q,I.maxAge))return L.lastAccessedAt=Date.now(),L.data;O.delete(J)}let R=F(I.directory,J,B);if(await S(R))try{let N=await j(R,"utf-8"),Q=JSON.parse(N);if(await b(Q,q,I.maxAge))return Q.lastAccessedAt=Date.now(),O.set(J,Q),Q.data}catch{}return null}async function V(q,z,B,I,J=W){if(!J.enabled)return;let L=X(q,z),R=await M(q),N=await w(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=F(J.directory,L,I);try{await a(r(T)),await s(T,JSON.stringify(Q,null,2))}catch(d){console.warn(`[stx-media] Failed to write cache: ${d}`)}}async function Qq(q,z,B=W){for(let[I,J]of O.entries())if(J.src===q)O.delete(I)}async function D(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 G(J,{recursive:!0,force:!0})}catch{}}}async function Rq(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 x(I);z.diskEntries[B]=J.length;for(let L of J){let R=U(I,L),N=await n(R);z.totalSizeMB+=N.size/1048576}}catch{}}return z}async function Tq(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 x(J);for(let R of L){let N=U(J,R);try{let Q=await j(N,"utf-8"),T=JSON.parse(Q);if(Date.now()-T.createdAt>B)await G(N),O.delete(T.key),z++}catch{await G(N).catch(()=>{}),z++}}}catch{}}return z}var O,W;var P=_(()=>{E();O=new Map,W={enabled:!0,directory:".stx/media-cache",maxAge:30,maxSize:500}});class u{config;constructor(q={}){this.config={...o,...q}}async get(q,z){return k(q,z,"video",this.config)}async set(q,z,B){return V(q,z,B,"video",this.config)}async clear(){return D("video",this.config)}updateConfig(q){this.config={...this.config,...q}}}function v(q){if(!Y)Y=new u(q);else if(q)Y.updateConfig(q);return Y}async function Xq(q,z,B){let I=B?{directory:`${B.outputDir}/../.cache`}:{};return v(I).get(q,z)}async function Yq(q,z,B,I){let J=I?{directory:`${I.outputDir}/../.cache`}:{};return v(J).set(q,z,B)}async function Zq(){return v().clear()}var o,Y=null;var t=_(()=>{P();o={enabled:!0,directory:".stx/media-cache",maxAge:30,maxSize:2000,cacheTranscoded:!0,cacheThumbnails:!0,cacheManifests:!0}});t();export{Yq as setCachedVideoResult,Xq as getCachedVideoResult,Zq as clearVideoCache,u as VideoProcessorCache};
