class AwpDelayScripts{constructor(){this.triggerEvents=["keydown","mousedown","mousemove","touchmove","touchstart","touchend","wheel"],this.scriptsLoaded=!1,this.userInteracted=!1,this.limitReached=!1,this.domContentLoaded=!1,this.eventListenerRemovers=[],this._addUserInteractionListener(),this._addDomContentLoadedListener()}_tryToLoad(){!this.scriptsLoaded&&this.domContentLoaded&&(this.userInteracted||this.limitReached)&&("requestIdleCallback"in window?window.requestIdleCallback((()=>this._loadDelayedScripts())):setTimeout((()=>this._loadDelayedScripts()),0))}_addDomContentLoadedListener(){document.addEventListener("DOMContentLoaded",(()=>{this.domContentLoaded=!0,setTimeout((()=>{this.limitReached=!0,this._tryToLoad()}),5e3)}))}_addUserInteractionListener(){this.triggerEvents.forEach((e=>{const t=()=>{this.userInteracted=!0,this._tryToLoad()};window.addEventListener(e,t,{once:!0,passive:!0}),this.eventListenerRemovers.push((()=>window.removeEventListener(e,t)))}))}_removeUserInteractionListeners(){this.eventListenerRemovers.forEach((e=>e()))}_loadDelayedScripts(){if(!this.scriptsLoaded){this.scriptsLoaded=!0,this._removeUserInteractionListeners();const e=Array.from(document.querySelectorAll('script[type="delay/oninteraction"]')).filter((e=>!e.dataset.ifdom||null!==document.querySelector(e.dataset.ifdom)));e.sort(((e,t)=>(parseInt(e.dataset.delay)||100)-(parseInt(t.dataset.delay)||100)));let t=0;e.forEach((e=>{const i=e.src,s=parseInt(e.dataset.delay)||100,n=Math.max(s,t+100);t=n,i&&this._preloadScript(i),setTimeout((()=>{"requestIdleCallback"in window?window.requestIdleCallback((()=>this._injectScript(e))):this._injectScript(e)}),n)}))}}_preloadScript(e){const t=document.createElement("link");t.href=e,t.rel="preload",t.as="script",t.fetchPriority="low",document.head.appendChild(t)}_injectScript(e){const t=document.createElement("script");t.async=!0,t.fetchPriority="low",Array.from(e.attributes).forEach((e=>{"type"!==e.name&&"data-delay"!==e.name&&"data-ifdom"!==e.name&&t.setAttribute(e.name,e.value)})),e.innerHTML.trim()?(t.innerHTML=e.innerHTML.trim(),e.id&&(t.innerHTML+=`\nconst event = new CustomEvent("${e.id}-loaded"); document.dispatchEvent(event);`)):e.id&&t.addEventListener("load",(()=>{const t=new CustomEvent(`${e.id}-loaded`);document.dispatchEvent(t)})),e.parentNode.replaceChild(t,e)}}new AwpDelayScripts();