/* ! @preserve @bnjmnrsh/satchel v0.2.6 | (c) 2024 bnjmnrsh | ISC | https://github.com/bnjmnrsh/satchel */ class t{static _stcl;#t;#e;#r;constructor(e=null,r={},s=!1,o="pocket"){if(this.#e=s?window.localStorage:window.sessionStorage,!e)throw new Error('Satchel: a "key" is required.');if("string"!=typeof e)throw new Error('Satchel: "key" must be a string.');if(!this.#s(r))throw new Error("Satchel: {cargo} must be an object.");if("boolean"!=typeof s)throw new Error('Satchel: "local" must be a boolean.');if("string"!=typeof o)throw new Error('Satchel: "pocket" must be a string.');this.#t=`${t.stcl}.${o}.${e}`,this.#r={data:void 0,expiry:null},r={...this.#r,...r},this.set(r)}static set stcl(t){if("string"!=typeof t)throw new Error("Satchel.stcl must be a string.");this._stcl=t}static get stcl(){return this._stcl?this._stcl:"stcl"}age(){const t=JSON.parse(this.#e.getItem(this.#t));return t?{age:Date.now()-t._creation,creation:t._creation,expiry:t.expiry?t.expiry:null,fresh:this.isFresh()}:null}bin(){const e=this.#e.getItem(this.#t);return e?(this.#e.removeItem(this.#t),t.#o({key:this.#t,oldValue:e,storageArea:t.#i(this.#e),action:"bin"}),!0):null}get(t=!1){const e=this.#e.getItem(this.#t);return e?!!(this.isFresh()&&!t||t)&&JSON.parse(e):null}set({data:e,expiry:r}){const s=JSON.parse(this.#e.getItem(this.#t));if(e=s?.data&&!e?s.data:e,r=s?.expiry&&!r?s.expiry:r,e&&"string"!=typeof e&&"number"!=typeof e&&!this.#s(e))throw new Error("Satchel.set({data}): must be either null or a number, string or object.");if("number"!=typeof r&&null!==r)throw new Error('Satchel.set({expiry}): "expiry" must be null or a number.');if(e&&this.#s(e))try{JSON.parse(JSON.stringify(e))}catch(t){throw new Error(`Satchel.set({data}): ${t}`)}const o=this.get(!0),i={};return i.data=e||null,i.expiry=r||null,i._creation=o?._creation||Date.now(),this.#e.setItem(this.#t,JSON.stringify(i)),t.#o({key:this.#t,newValue:JSON.stringify(i),oldValue:o?JSON.stringify(o):null,storageArea:t.#i(this.#e),action:"set"}),this}isFresh(){const t=JSON.parse(this.#e.getItem(this.#t));return t?!t?.expiry||t.expiry-Date.now()>0:null}getKey(){return this.#t}static#o(t={}){(t={key:null,newValue:null,oldValue:null,storageArea:null,action:null,...t}).url=window.location.href;const e=new CustomEvent("Satchel",{bubbles:!0,cancelable:!0,detail:t});return document.dispatchEvent(e)}static#i(t){return t===window.localStorage?"LocalStorage":"SessionStorage"}#s(t){return t&&"object"==typeof t&&t.constructor===Object}static getSatchel(e,r=!1,s="pocket"){if(!e)throw new Error('Satchel.getSatchel(key): a "key" is required.');if("string"!=typeof e)throw new Error('Satchel.getSatchel(key): "key" must be a string.');if("boolean"!=typeof r)throw new Error('Satchel.getSatchel(key, local): "local" must be a boolean.');if("string"!=typeof s)throw new Error('Satchel.getSatchel(key, local, pocket): "pocket" must be an string.');const o=`${t.stcl}.${s}.${e}`,i=r?window.localStorage:window.sessionStorage,n=JSON.parse(i.getItem(o));return n&&0!==n.length?new t(e,n,r,s):null}static setKey(e=null,r={},s=!1,o="pocket"){new t(e,r,s,o)}}export{t as Satchel}; //# sourceMappingURL=Satchel.min.js.map