import{useEffect as f}from"react";import{useLocation as m}from"react-router-dom";let i=[],c=[];const s=new Map;let p=null;function E(t,e=!1,n){e?i.push({content:t,attributes:n}):i.push({src:t,attributes:n})}function P(t,e){t&&c.push({src:t,attributes:e})}function x(){return p||Promise.resolve()}function A(){const t=m();return f(()=>{p=new Promise(e=>{y(i).then(()=>{const n=h(c);return c=[],n}).then(()=>{e()}),i=[]})},[t.pathname]),null}function a(t,e){const n=s.get(t);if(n)return n;const o=new Promise((u,d)=>{const r=document.createElement("script");r.src=t,l(r,e),r.addEventListener("load",()=>u()),r.addEventListener("error",()=>d(`Error loading script: ${t}`)),document.body.appendChild(r)});return s.set(t,o),o}function S(t,e){return new Promise(n=>{const o=document.createElement("script");o.textContent=t,l(o,e),document.body.appendChild(o),n()})}async function y(t){for(const e of t)"src"in e?await a(e.src,e.attributes):"content"in e&&await S(e.content,e.attributes)}async function h(t){return Promise.allSettled(t.map(({src:e,attributes:n})=>a(e,n)))}function l(t,e){e&&Object.entries(e).forEach(([n,o])=>{typeof o=="boolean"?o&&t.setAttribute(n,""):t.setAttribute(n,o)})}export{A as ScriptLoader,P as loadScriptFromReactAsync,E as loadScriptFromReactSync,x as waitForContentScripts};
