import{isEmpty}from"../base/helpers.mjs";class SessionStorage{constructor(e){this.driver=e,this.scopes={}}getScope(e){return this.scopes.hasOwnProperty(e)||(this.scopes[e]=new ScopedStorage(e,this.driver)),this.scopes[e]}getAll(){let e=this;return Object.getOwnPropertyNames(this.scopes).reduce((function(t,r){return t[r]=e.getScope(r).getAll(),t}),{})}clear(){for(let e in this.scopes)this.getScope(e).clear()}}class ScopedStorage{constructor(e,t,r){this.scope=e,this.driver=t,this.ttl=r,void 0===this.ttl&&(this.ttl=36e5)}getAll(){let e=this;return this.keys().reduce((function(t,r){return t[r]=e.getItem(r),t}),{})}setItem(e,t){let r=`${this.scope}-${e}`,s=`${this.scope}-${e}-expiration`;this.driver.setItem(r,JSON.stringify(t)),this.driver.setItem(s,(new Date).getTime()+this.ttl)}getItem(e){let t=`${this.scope}-${e}`,r=`${this.scope}-${e}-expiration`,s=this.driver.getItem(t),i=this.driver.getItem(r);if(void 0!==s&&"undefined"!==s)return null===s||"null"===s?null:(s=JSON.parse(s),!isEmpty(i)&&i<=(new Date).getTime()?(this.removeItem(t),null):s)}keys(){let e,t=`${this.scope}-`;return e=void 0!==this.driver.keys?this.driver.keys():Object.getOwnPropertyNames(this.driver),e.filter((e=>e.startsWith(t))).map((e=>e.substring(t.length))).filter((e=>"expiration"!=e))}removeItem(e){let t=`${this.scope}-${e}`;return this.driver.removeItem(t)}removePrefix(e){for(let t of this.keys())t.startsWith(e)&&this.removeItem(t)}clear(){for(let e of this.keys())this.removeItem(e);return this.setItem("expiration",{}),this.driver.clear()}key(e){return this.keys()[e]}}class CookieStorage{constructor(){this.expiration=new Date,this.expiration.setTime(this.expiration.getTime()+2592e6)}keys(){let e,t=decodeURIComponent(document.cookie).split(";"),r=[];for(let s of t){for(e=s.split("=")[0];" "==e.charAt(0);)e=e.substring(1);r.push(e)}return r}getItem(e){let t=decodeURIComponent(document.cookie).split(";");for(let r of t){for(;" "==r.charAt(0);)r=r.substring(1);if(r.startsWith(`${e}=`))return JSON.parse(r.substring(e.length+1))}return null}setItem(e,t,r){void 0===r&&(r=this.expiration),document.cookie=`${e}=${JSON.stringify(t)};expires=${r.toUTCString()};path=/;`}removeItem(e){let t=new Date;t.setTime(0),this.setItem(e,"",t)}clear(){for(let e of this.keys())this.removeItem(e)}key(e){return this.keys()[e]}}class MemoryStorage{constructor(){this.storage={}}keys(){return Object.getOwnPropertyNames(this.storage)}setItem(e,t){this.storage[e]=t}getItem(e){return this.storage.hasOwnProperty(e)?this.storage[e]:null}removeItem(e){delete this.storage[e]}removePrefix(e){for(let t of this.keys())t.startsWith(e)&&this.removeItem(t)}clear(){for(let e of this.keys)delete this.storage[e]}key(e){return this.keys()[e]}}let getDriver=function(){try{if(void 0!==typeof Storage)return window.localStorage}catch(e){return console.error("Couldn't get local storage, defaulting to memory.",e),new MemoryStorage}};export{SessionStorage,MemoryStorage,CookieStorage,ScopedStorage};export let Session=new SessionStorage(getDriver());
