import*as e from"@preact/signals";var a={875:function(e,a,t){t.d(a,{XR:()=>r,c9:()=>n,js:()=>u});let l=()=>Math.random().toString(36).substring(2,9),r=e=>new URL(e).hash.replace(/^#\/?#?/,"")||"/",u=e=>`/${e}`,n=(e,a={},t=0)=>({url:e,key:l(),id:l(),index:t,sameDocument:!0,state:a,hash:r(e)})},452:function(a){a.exports=e}},t={};function l(e){var r=t[e];if(void 0!==r)return r.exports;var u=t[e]={exports:{}};return a[e](u,u.exports,l),u.exports}l.d=(e,a)=>{for(var t in a)l.o(a,t)&&!l.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:a[t]})},l.o=(e,a)=>Object.prototype.hasOwnProperty.call(e,a);var r={};(()=>{l.d(r,{A:()=>n,Z:()=>u});var e=l(452),a=l(875);let t=()=>({url:window.location.href,entries:[(0,a.c9)(window.location.href)],index:0,subscriptions:new Set}),u=()=>{let l=t(),r=(0,e.signal)(l.url),u=(0,e.signal)(l.entries),n=(0,e.signal)(l.index),i=l.subscriptions,s=(0,e.computed)(()=>u.value[n.value]),o=(0,e.computed)(()=>[...u.value]),v=(0,e.computed)(()=>n.value>0),h=(0,e.computed)(()=>n.value<u.value.length-1),c=(0,e.computed)(()=>n.value>0?u.value[n.value-1]:null),d=()=>{let t=window.history.state,l=(0,a.c9)(window.location.href,t);(0,e.batch)(()=>{r.value=l.url,u.value=[l],n.value=0})},w=t=>{let l=t.newURL,r=window.history.state;if(l===s.value?.url)return;let i=u.value.findIndex(e=>e.url===l);if(i>=0){n.value=i;let e=u.value[i];if(JSON.stringify(e.state)!==JSON.stringify(r)){let a=[...u.value];a[i]={...e,state:r},u.value=a}}else{let t=(0,a.c9)(l,r,u.value.length),i=[...u.value];n.value<u.value.length-1&&i.splice(n.value+1),i.push(t),(0,e.batch)(()=>{u.value=i,n.value=i.length-1})}},f=(a,t)=>{(0,e.batch)(()=>{t&&(r.value=t),n.value=a})},p=(e,a)=>{let t=[...u.value],l={...t[e],...a};return t[e]=l,u.value=t,l},g=(e,a)=>p(e,{state:a}),y=(e,a,t=n.value)=>(window.history.replaceState(a,"",e),g(t,a)),b=(t,l={})=>{let i=s.value.hash,o=new URL(`#${(0,a.js)(t)}`,window.location.href).href;if(i!==t){let t=(0,a.c9)(o,l.state,u.value.length);window.history.pushState(l.state||null,"",o);let i=[...u.value];n.value<u.value.length-1&&i.splice(n.value+1),i.push(t),(0,e.batch)(()=>{u.value=i,n.value=i.length-1,r.value=o,f(i.length-1)})}else{let e=s.value;l.state&&JSON.stringify(e.state)!==JSON.stringify(l.state)&&y(o,l.state)}},x=(0,e.effect)(()=>{let e=s.value;e&&(0,a.XR)(r.value)!==(0,a.XR)(e.url)&&(r.value=e.url)});return{currentEntry:s,prevEntry:c,entries:o,canGoBack:v,canGoForward:h,navigate:b,traverseTo:(e,a={})=>{let t=u.value.findIndex(a=>a.key===e);if(-1===t)return;let l=u.value[t],r=t-n.value,i=l;a.state&&(i=y(l.url,a.state,t)),window.history.go(r),f(t,i.url)},back:()=>{if(!c.value)return window.history.back(),null;window.history.back(),f(c.value.index,c.value.url)},forward:()=>{if(!h.value)return window.history.forward(),null;let e=n.value+1,a=u.value[e];window.history.forward(),f(e,a.url)},updateCurrentEntry:(e={})=>{y(window.location.hash,e.state,n.value)},goToPrev:()=>{if(!v.value||!c.value)return window.history.back(),null;b(c.value.hash,c.value.state)},subscribe:a=>{let t=s.value,l=t;a(t,c.value,t.hash);let r=(0,e.effect)(()=>{let e=s.value,t=e.hash;l!==e&&(a(e,c.value,t),l=e)}),u=()=>{r(),i.delete(u)};return i.add(u),u},updateCurrentEntryHash:(e,t)=>{let l=new URL(`#${(0,a.js)(e)}`,window.location.href).href;return window.history.replaceState(t,"",l),p(n.value,{url:l,hash:(0,a.XR)(l),...t&&{state:t}})},create:()=>{d(),window.addEventListener("hashchange",w)},destroy:()=>{window.removeEventListener("hashchange",w),i.forEach(e=>e()),i.clear();let a=t();(0,e.batch)(()=>{r.value=a.url,u.value=a.entries,n.value=a.index}),x()}}},n=u()})();var u=r.Z,n=r.A;export{u as createHashNavigation,n as hashNavigation};
//# sourceMappingURL=hashNavigation.js.map