/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/flamethrower-router@0.0.0-meme.12/dist/main.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
function m(e,t){if(["link","go"].includes(e))if(t){const e=document.querySelector(t);e?e.scrollIntoView({behavior:"smooth",block:"start"}):window.scrollTo({top:0})}else window.scrollTo({top:0})}function u(e){const t=new URL(e||window.location.href).href;return t.endsWith("/")||t.includes(".")||t.includes("#")?t:`${t}/`}function E(e){(!window.history.state||window.history.state.url!==e)&&window.history.pushState({url:e},"internalLink",e)}function k(e){document.querySelector(e).scrollIntoView({behavior:"smooth",block:"start"})}function S(e){return{type:"popstate",next:u()}}function F(e){var t;let o;if(e.altKey||e.ctrlKey||e.metaKey||e.shiftKey)return{type:"disqualified"};for(let t=e.target;t.parentNode;t=t.parentNode)if("A"===t.nodeName){o=t;break}if(o&&o.host!==location.host)return o.target="_blank",{type:"external"};if(o&&"cold"in(null==o?void 0:o.dataset))return{type:"disqualified"};if(null!=o&&o.hasAttribute("href")){const r=o.getAttribute("href"),n=new URL(r,location.href);if(e.preventDefault(),null!=r&&r.startsWith("#"))return k(r),{type:"scrolled"};const i=null==(t=r.match(/#([\w'-]+)\b/g))?void 0:t[0];return{type:"link",next:u(n.href),prev:u(),scrollId:i}}return{type:"noop"}}function N(e){return(new DOMParser).parseFromString(e,"text/html")}function g(e){document.body.querySelectorAll("[flamethrower-preserve]").forEach((t=>{let o=e.body.querySelector('[flamethrower-preserve][id="'+t.id+'"]');if(o){const e=t.cloneNode(!0);o.replaceWith(e)}})),document.body.replaceWith(e.body)}function A(e){const t=e=>Array.from(e.querySelectorAll('head>:not([rel="prefetch"]')),o=t(document),r=t(e),{staleNodes:n,freshNodes:i}=L(o,r);n.forEach((e=>e.remove())),document.head.append(...i)}function L(e,t){const o=[],r=[];let n=0,i=0;for(;n<e.length||i<t.length;){const s=e[n],c=t[i];if(null!=s&&s.isEqualNode(c)){n++,i++;continue}const l=s?r.findIndex((e=>e.isEqualNode(s))):-1;if(-1!==l){r.splice(l,1),n++;continue}const a=c?o.findIndex((e=>e.isEqualNode(c))):-1;-1===a?(s&&o.push(s),c&&r.push(c),n++,i++):(o.splice(a,1),i++)}return{staleNodes:o,freshNodes:r}}function b(){document.head.querySelectorAll("[data-reload]").forEach(v),document.body.querySelectorAll("script").forEach(v)}function v(e){const t=document.createElement("script"),o=Array.from(e.attributes);for(const{name:e,value:r}of o)t[e]=r;t.append(e.textContent),e.replaceWith(t)}const x={log:!1,pageTransitions:!1};class q{constructor(e){this.opts=e,this.enabled=!0,this.prefetched=new Set,this.opts={...x,...null!=e?e:{}},null!=window&&window.history?(document.addEventListener("click",(e=>this.onClick(e))),window.addEventListener("popstate",(e=>this.onPop(e))),this.prefetch()):(console.warn("flamethrower router not supported in this browser or environment"),this.enabled=!1)}go(e){const t=window.location.href,o=new URL(e,location.origin).href;return this.reconstructDOM({type:"go",next:o,prev:t})}back(){window.history.back()}forward(){window.history.forward()}get allLinks(){return Array.from(document.links).filter((e=>e.href.includes(document.location.origin)&&!e.href.includes("#")&&e.href!==(document.location.href||document.location.href+"/")&&!this.prefetched.has(e.href)))}log(...e){this.opts.log&&console.log(...e)}prefetch(){if("visible"===this.opts.prefetch)this.prefetchVisible();else{if("hover"!==this.opts.prefetch)return;this.prefetchOnHover()}}prefetchOnHover(){this.allLinks.forEach((e=>{const t=e.getAttribute("href");e.addEventListener("pointerenter",(()=>this.createLink(t)),{once:!0})}))}prefetchVisible(){"IntersectionObserver"in window&&(this.observer||(this.observer=new IntersectionObserver(((e,t)=>{e.forEach((e=>{const o=e.target.getAttribute("href");this.prefetched.has(o)?t.unobserve(e.target):e.isIntersecting&&(this.createLink(o),t.unobserve(e.target))}))}),{root:null,rootMargin:"0px",threshold:1})),this.allLinks.forEach((e=>this.observer.observe(e))))}createLink(e){const t=document.createElement("link");t.rel="prefetch",t.href=e,t.as="document",t.onload=()=>this.log("🌩️ prefetched",e),t.onerror=t=>this.log("🤕 can't prefetch",e,t),document.head.appendChild(t),this.prefetched.add(e)}onClick(e){this.reconstructDOM(F(e))}onPop(e){this.reconstructDOM(S())}async reconstructDOM({type:e,next:t,prev:o,scrollId:r}){if(this.enabled)try{if(this.log("⚡",e),["popstate","link","go"].includes(e)&&t!==o){this.opts.log&&console.time("⏱️"),window.dispatchEvent(new CustomEvent("flamethrower:router:fetch")),"popstate"!=e&&E(t);const o=N(await(await fetch(t,{headers:{"X-Flamethrower":"1"}}).then((e=>{const t=e.body.getReader(),o=parseInt(e.headers.get("Content-Length"));let r=0;return new ReadableStream({start(e){!function n(){t.read().then((({done:t,value:i})=>{t?e.close():(r+=i.length,window.dispatchEvent(new CustomEvent("flamethrower:router:fetch-progress",{detail:{progress:Number.isNaN(o)?0:r/o*100,received:r,length:o||0}})),e.enqueue(i),n())}))}()}})})).then((e=>new Response(e,{headers:{"Content-Type":"text/html"}})))).text());A(o),this.opts.pageTransitions&&document.createDocumentTransition?document.createDocumentTransition().start((()=>{g(o),b(),m(e,r)})):(g(o),b(),m(e,r)),window.dispatchEvent(new CustomEvent("flamethrower:router:end")),setTimeout((()=>{this.prefetch()}),200),this.opts.log&&console.timeEnd("⏱️")}}catch(e){return window.dispatchEvent(new CustomEvent("flamethrower:router:error",e)),this.opts.log&&console.timeEnd("⏱️"),console.error("💥 router fetch failed",e),!1}else this.log("router disabled")}}const T=e=>{const t=new q(e);if(e.log&&console.log("🔥 flamethrower engaged"),window){window.flamethrower=t}return t};export{T as default};
//# sourceMappingURL=/sm/327699f537d18e156a57aab13823d8f31e47b7a4a13390f67edf6f2539db44c2.map