import{createStore}from"@stencil/store";const safeRead=(e,t)=>{try{return JSON.parse(e.getItem(t))}catch{return null}},debounce=e=>{let t=!1;return()=>{t||(t=!0,setTimeout((()=>{e(),t=!1}),0))}};export const createStorageStore=(e,t,r,o=!1)=>{var s;const a=createStore(null!==(s=safeRead(e,t))&&void 0!==s?s:r,((e,t)=>JSON.stringify(e)!==JSON.stringify(t))),n=debounce((()=>e.setItem(t,JSON.stringify(a.state))));return n(),o&&window.addEventListener("storage",(()=>{const r=safeRead(e,t);if(null!==r)for(const e in r)a.set(e,r[e])})),a.use({set:n,reset:n}),a};export const createLocalStore=(e,t,r=!1)=>createStorageStore(localStorage,e,t,r);export const createSessionStore=(e,t)=>createStorageStore(sessionStorage,e,t);