const m="google-maps-js-api",w="dispatch-google-maps-script",d="__jobOnlineGoogleMapsJsPromise",u=(l=15e3)=>{const n=Date.now();return new Promise((o,t)=>{const i=()=>{const s=window.google?.maps;if(s&&typeof s.importLibrary=="function"){o();return}if(Date.now()-n>l){t(new Error("Timed out waiting for Google Maps JavaScript API to initialize."));return}requestAnimationFrame(i)};i()})};async function f(l){if(typeof window>"u")throw new Error("Google Maps API is only available in the browser.");const n=l.trim();if(!n)throw new Error("Google Maps API key is missing.");const o=window,t=o;if(o.google?.maps?.importLibrary)return o.google.maps;const i=t[d];return i||(t[d]=new Promise((s,a)=>{const p=async()=>{try{await u();const r=o.google?.maps;if(!r?.importLibrary){a(new Error("Google Maps API loaded but importLibrary is not available."));return}s(r)}catch(r){a(r instanceof Error?r:new Error(String(r)))}},c=document.getElementById(m)||document.getElementById(w);if(c){o.google?.maps?.importLibrary||c.complete?p():(c.addEventListener("load",()=>{p()},{once:!0}),c.addEventListener("error",()=>a(new Error("Failed to load Google Maps script.")),{once:!0}));return}const e=document.createElement("script");e.id=m,e.async=!0,e.defer=!0;const g=new URLSearchParams({key:n,loading:"async"});e.src=`https://maps.googleapis.com/maps/api/js?${g.toString()}`,e.onload=()=>{p()},e.onerror=()=>a(new Error("Failed to load Google Maps script.")),document.head.appendChild(e)}),t[d])}export{f as l};
