"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;var n={moderateEvents:["mouseenter","touchstart","focus"],fallbackTriggerSupport:["visible","moderate"],fallbackTrigger:"moderate",fallbackAction:"prefetch",validActions:["prefetch","prerender"],validTriggers:["visible","immediate","eager","moderate","conservative"]},g=!1,d=new Set,f=HTMLScriptElement.supports("speculationrules"),T=_optionalChain([document, 'access', _ => _.createElement, 'call', _2 => _2("link"), 'access', _3 => _3.relList, 'optionalAccess', _4 => _4.supports, 'optionalCall', _5 => _5("prefetch")]),s,c=t=>{s.unobserve(t),k(t)},b=t=>{for(let e of n.moderateEvents)t.addEventListener(e,m,{passive:!0})},k=t=>{for(let e of n.moderateEvents)t.removeEventListener(e,m)},m=t=>{let e=t.target;e&&a(e,l(e.rel))},a=(t,e)=>{if(!A({href:t.href,target:t.target}))return c(t);f?S(t.href,e):T?E(t.href):fetch(t.href,{priority:"low"}),d.add(t.href),c(t)},S=(t,e)=>{try{let i=document.createElement("script");i.type="speculationrules";let r=[{source:"list",urls:[t],eagerness:e[1]==="visible"?void 0:e[1]}];i.textContent=e[0]==="prefetch"?JSON.stringify({prefetch:r}):JSON.stringify({prerender:r,prefetch:r}),document.head.appendChild(i)}catch(i){console.error(i)}},E=t=>{let e=document.createElement("link");e.rel="prefetch",e.href=t,e.as="document",document.head.appendChild(e)},l=t=>{let e,i;for(let r of t.split(" "))if(r.startsWith("prefetch:")||r.startsWith("prerender:")){[e,i]=r.split(":");break}return e||(e=n.fallbackAction),i||(i=n.fallbackTrigger),f||(e=n.fallbackAction,n.fallbackTriggerSupport.includes(i)||(i=n.fallbackTrigger)),n.validActions.includes(e)||(e=n.fallbackAction),n.validTriggers.includes(i)||(i=n.fallbackTrigger),[e,i]},A=t=>{try{if(t.href=t.href.replace(/#.*/,""),t.target==="_blank"||t.href.includes("mailto:")||t.href.includes("tel:"))return!1;let e=new URL(t.href);return!(e.origin!==window.location.origin||e.pathname===window.location.pathname||d.has(t.href))}catch (e2){return!1}},L=()=>{if(!navigator.onLine){console.warn("The device is offline, speculate library not initialised.");return}if("connection"in navigator){let t=navigator.connection;if(_optionalChain([t, 'optionalAccess', _6 => _6.saveData])){console.warn("Save-Data is enabled, speculate library not initialised.");return}if(/(2|3)g/.test(_optionalChain([t, 'optionalAccess', _7 => _7.effectiveType]))){console.warn("2G or 3G connection is detected, speculate library not initialised.");return}}},h=()=>{if(g)return;g=!0,L(),s=s||new IntersectionObserver(e=>{for(let i of e)if(i.isIntersecting&&i.target instanceof HTMLAnchorElement){let r=i.target,[v,u]=l(r.rel);u==="visible"?a(r,[v,u]):c(r)}});let t=document.querySelectorAll('a[rel*="prefetch:"], a[rel*="prerender:"]');for(let e of t){let[i,r]=l(e.rel);r==="visible"?s.observe(e):r==="moderate"&&!f?b(e):a(e,[i,r])}},y=()=>typeof requestIdleCallback<"u"?requestIdleCallback(h):setTimeout(h,0),w= exports.speculateLinks =y;var o= (_class =class{__init() {this.config=null}__init2() {this.promise=null}__init3() {this.time=null}constructor(e){;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this);this.config=e,this.initialise()}initialise(){this.prefetch=this.prefetch.bind(this),this.click=this.click.bind(this),_optionalChain([this, 'access', _8 => _8.getTarget, 'optionalAccess', _9 => _9.addEventListener, 'call', _10 => _10("click",this.click)]),_optionalChain([this, 'access', _11 => _11.getTarget, 'optionalAccess', _12 => _12.addEventListener, 'call', _13 => _13("mouseover",this.prefetch)])}prefetch(e){_optionalChain([this, 'access', _14 => _14.config, 'optionalAccess', _15 => _15.fetch])&&(!this.promise||this.stale)&&(this.promise=this.config.fetch(),this.time=Date.now())}click(e){_optionalChain([this, 'access', _16 => _16.config, 'optionalAccess', _17 => _17.onClick])&&this.promise&&this.promise.then(this.config.onClick)}destroy(){_optionalChain([this, 'access', _18 => _18.getTarget, 'optionalAccess', _19 => _19.removeEventListener, 'call', _20 => _20("click",this.click)]),_optionalChain([this, 'access', _21 => _21.getTarget, 'optionalAccess', _22 => _22.removeEventListener, 'call', _23 => _23("mouseover",this.prefetch)])}get getTarget(){return this.config?document.querySelector(this.config.target):null}get stale(){return!this.time||!_optionalChain([this, 'access', _24 => _24.config, 'optionalAccess', _25 => _25.staletime])?!1:Date.now()-this.time>this.config.staletime}}, _class);exports.PrefetchData = o; exports.speculateLinks = w; //# sourceMappingURL=index.cjs.map