import localforage from"localforage";class SessionCache{constructor(){this.map=new Map}async getItem(a){return this.map.get(a)}async setItem(a,b){this.map.set(a,b)}async removeItem(a){this.map.delete(a)}async clear(){this.map.clear()}async keys(){return Array.from(this.map.keys())}}export let CacheType=function(a){return a.auto="auto",a.memory="memory",a.local="local",a}({});export default function createCache(a={}){const b=localforage.supports(localforage.INDEXEDDB);b;let c;c=a.type!==CacheType.memory&&b?localforage.createInstance({name:"dt-engine",storeName:"dt-engine-model",description:"tsl",...a}):new SessionCache;let d=b=>`${a.prefix||"default"}:${b}`,e=async a=>{a=d(a);const b=await c.getItem(a);if(!b)return null;let{time:e,data:f}=b;return"undefined"==typeof e?f:new Date().getTime()>e?(await c.removeItem(a),null):f},f=async(a,b,e)=>{a=d(a);let f=new Date().getTime();await c.setItem(a,{time:e?e+f:void 0,data:b})};return c.keys().then(a=>{a.forEach(a=>{c.getItem(a).then(b=>{if(b){let{time:d}=b;("undefined"==typeof d||new Date().getTime()>d)&&c.removeItem(a)}})})}),{getItem:e,setItem:f,removeItem:async a=>(a=d(a),await c.removeItem(a)),clear:async()=>await c.clear(),keys:async()=>await c.keys()}}