/*!
 * Custom elements for Nanopore-Digital Web applications
 */
import{g as t}from"./index-BM3Om9WE.js";import{d as e}from"./throttle-C93FMm2Z.js";function s(t){const e=new URLSearchParams([...new URLSearchParams(t)].map((([t,e])=>[t.replace(/([[\]])/g,((t,e)=>"["===e?".":"")),e]))),s={};return new Set(e.keys()).forEach((t=>{const n=e.getAll(t).join(",").split(",").map((t=>decodeURIComponent(t.replace(/\+/g," "))));let r=t,o=1===n.length?n[0]:n;if(r.includes(".")){const[t,...e]=r.split("."),n=s[t]||{},i=e.pop();e.reduce(((t,e)=>t[e]??={}),n)[i]=o,r=t,o=n}s[r]=o})),s}const n="nano-component-store:",r=(t="hash",e=window)=>e&&e.location?"hash"===t&&e.location.hash&&e.location.hash.match(/&|=/)?s(e.location.hash.replace(/^#/,"")):"query"===t&&e.location.search&&e.location.search.match(/&|=/)?s(e.location.search.replace(/^\?/,"")):{}:{},o=(t,e,n="hash",o=window,i="replace")=>{t=Object.keys(s(t))[0];const h={};for(const t in e)(!e[t]||Array.isArray(e[t])&&!e[t].length)&&delete e[t];h[t]=e;let u=r(n,o);(u[t]||h[t])&&(u={...u,...h},((t,e="hash",s=window,n="replace")=>{if(!s||!s.history)return;const r="query"===e?s.location.search:s.location.hash,o="query"===e?s.location.pathname:s.location.pathname+s.location.search;let i=function(t){if(!t)return"";const e=[["",t]],s=new URLSearchParams;for(;e.length;){const[t,n]=e.pop();Object.entries(n).forEach((([n,r])=>{if(null==r)return;const o=typeof r,i=t?`${t}.${n}`:n;if(null===r||"object"!==o||Array.isArray(r)){let t="string"===o?r:"";t=Array.isArray(r)?r.join(","):r?.toJSON?r.toJSON():r?.toString?r.toString():String(r),null!=t&&s.append(i,t)}else e.push([i,r])}))}return s.sort(),s.toString()}(t);if(i&&i.length)if("hash"!==e||s.location.hash!=="#"+i)i=("hash"===e?"#":"?")+i,i!==r&&("replace"===n?s.history.replaceState(null,null,o+i):s.history.pushState(null,null,o+i));else try{document.querySelector("#"+i)?.focus()}catch(t){}else{if(!r||!r.length)return;"replace"===n?s.history.replaceState(null,null,o):s.history.pushState(null,null,o)}})(u,n,o,i))},i=(t,e="hash",n=window)=>(t=Object.keys(s(t))[0],r(e,n)[t]||null),h=new Map,u={init(e,s,n,r){const o=r||e.storeId||t(e).id;if(!o)return void console.error("No id set. You can only instantiate a new component store with an id. ");if(h.get(o))return void console.error("Duplicate store id usage. A store ID must be unique!",o);const i=s.reduce(((t,s)=>(void 0!==e[s]&&(t[s]=e[s]),t)),{});h.set(o,new a(o,e,s,i,n)),h.get(o).dispatchToProps();const u=e.connectedCallback;e.connectedCallback=function(){h.set(o,new a(o,e,s,i,n));const t=h.get(o);u&&u.call(e),t&&t.dispatchToProps()};const l=e.componentDidRender;e.componentDidRender=function(){const t=h.get(o);l&&l.call(e),t&&t.savePropsToStore()};const c=e.disconnectedCallback;e.disconnectedCallback=function(){const t=h.get(o);t&&t.delete(),h.delete(o),c&&c.call(e)}},update(t){const e=h.get(t);e?e.savePropsToStore():console.error(`No store found for id: ${t}`)},get:t=>h.get(t)};class a{method="session";keys;id;component;updateStore=!0;initialState={};constructor(t,s,n,r,o){if(!t)throw new Error("No id set. You can only instantiate a new component store with an id");this.id=t,this.keys=n,this.component=s,r&&(this.initialState=r),o&&(this.method=o),this.savePropsToStore=e(this.savePropsToStore,500),"url-hash-push"!==this.method&&"url-query-push"!==this.method||window.addEventListener("popstate",this.onUrlChange)}onUrlChange=()=>{this.dispatchToProps(!1)};equality(t,e){return typeof t==typeof e&&("object"!=typeof t?t===e:JSON.stringify(t)===JSON.stringify(e))}get getData(){const t=this.keys.reduce(((t,e)=>{const s=this.equality(this.component[e],this.initialState[e]);return void 0===this.component[e]||s?s&&delete t[e]:t[e]=this.component[e],t}),{});return Object.keys(t).length?t:void 0}retrieve(){switch(this.method){case"url-hash":case"url-hash-push":return i(this.id,"hash");case"url-query":case"url-query-push":return i(this.id,"query");case"local":return((t,e=window)=>{const s=e.localStorage.getItem(n+this.id);return null!==s?JSON.parse(s):null})();default:return((t,e=window)=>{const s=e.sessionStorage.getItem(n+this.id);return null!==s?JSON.parse(s):null})()}}save(t){switch(this.method){case"url-hash":return o(this.id,t,"hash");case"url-query":return o(this.id,t,"query");case"url-hash-push":return o(this.id,t,"hash",window,"push");case"url-query-push":return o(this.id,t,"query",window,"push");case"local":return((t,e,s=window)=>{e?s.localStorage.setItem(n+t,JSON.stringify(e)):s.localStorage.removeItem(n+t)})(this.id,t);default:return((t,e,s=window)=>{e?s.sessionStorage.setItem(n+t,JSON.stringify(e)):s.sessionStorage.removeItem(n+t)})(this.id,t)}}savePropsToStore=()=>{this.updateStore?this.save(this.getData):this.updateStore||(this.updateStore=!0)};dispatchToProps=(t=!0)=>{const e=this.retrieve();!e&&t||(this.updateStore=t,this.keys.forEach((t=>{this.component[t]=e?e[t]:void 0})))};delete=()=>{"url-hash-push"!==this.method&&"url-query-push"!==this.method||window.removeEventListener("popstate",this.onUrlChange)}}export{u as C}