(function(u,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(u=typeof globalThis<"u"?globalThis:u||self,a(u.AmperWave={}))})(this,function(u){"use strict";const a="amperwave-scratch-container",w="AmperWave_",d={setProperty(e,t){localStorage.setItem(e,t)},getProperty(e){return localStorage.getItem(e)},clear(){localStorage.clear()},removeProperty(e){localStorage.removeItem(e)}};function M(e){let t;return new Promise(n=>{Object.defineProperty(window,e,{get(){return t},set(i){t=i,n(i)},configurable:!0})})}function T(e){return e instanceof Error?e.message:String(e)}function p(e){let t=document.querySelector(`#${e}`);return t||(t=document.createElement("div"),t.id=e,document.body.appendChild(t)),t}function I(e,t,n=!1){return new Promise((i,r)=>{const o=document.createElement("script");o.type="text/javascript",o.src=t,o.async=n,e.appendChild(o),o.addEventListener("load",()=>i()),o.addEventListener("error",()=>r("Script loading event failed"))})}function D(e,t){const n=document.createElement("img");n.src=t,n.height=1,n.width=1,n.alt="",n.style.borderStyle="none",e.appendChild(n)}function b(e){let t=document.cookie.split(";");for(let n=0;nsetTimeout(t,e))}function $(){return window.crypto.randomUUID()}function C(e){const t=w+"userId";return e||(e=d.getProperty(t),e||(e=$(),d.setProperty(t,e)),e)}function k(){return window.location.hostname}async function A(e){if(!e||!e.gpp||!e.usPrivacy||!e.userId)throw new Error("[adsWizz] Missing params");const t=p(a),n=M("com_adswizz_synchro_listenerid"),i=`https://synchrobox.adswizz.com/register2.php?us_privacy=${e.usPrivacy}&gpp=${e.gpp}`;await I(t,i);const r=await n,c=`https://synchroscript.deliveryengine.adswizz.com/syncMe?partnerDomain=amperwave.net&partnerUserId=${e.userId}&idType=cookie`;return D(t,c),r}function U(e){return e.includes(":")?e.split(":")[1].trim():e}function j(e,t,n){return`https://playerservices.live.streamtheworld.com/api/idsync.js?stationId=${typeof e=="number"?e.toString():U(e)}&gdpr=${t}&us_privacy=${n}&withCredentials='true'`}async function O(){const t=await(await fetch("https://yield-op-idsync.live.streamtheworld.com/partnerIds",{credentials:"include"})).json();return Object.keys(t).length?G(t):null}function G(e){try{return btoa(JSON.stringify(e)).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}catch{throw new Error("[triton] Unable to encode JSON string")}}async function q(e){if(!e)throw new Error("[triton] Missing params");if(!e.tapStationId)throw new Error("[triton] Missing TAP station id");const t=e.gdpr?1:0,n=p(a);await I(n,j(e.tapStationId,t,e.usPrivacy||""),!0);const i=await O();if(!i)throw new Error("[triton] Unable to retrieve TAP listener ID");return i}const P="magnite_id",S="amperwave-multisync-iframe";function z(e,t,n,i,r){let o=n.querySelector(`#${S}`);o&&n.removeChild(o),N(e,t,n,i,r)}function N(e,t,n,i,r){const o=document.createElement("iframe");o.id=S,o.style.width="0",o.style.height="0",o.style.display="none",o.src=`https://eus.rubiconproject.com/usync.html?p=${i}&endpoint=${r}&auid=${e}`,o.onload=()=>t(e),n.appendChild(o)}function E(){const e=b(P);return!e||e.trim()===""||e==="false"?null:e}function K(e){const t="; expires= Fri, 31 Dec 9999 23:59:59 GMT";document.cookie=P+"="+(e||"")+t+"; path='/'; Secure"}async function W(e,t=10,n=1e4){let i=1,r=1e3;for(;i<=t;)try{const o=await fetch(`https://sync.amperwave.net/api/get/magnite/auid=${e}`);if(!o.ok){i++,i<=t&&(r=Math.min(r+1e3,n),await l(r));continue}const c=await o.json();if(!Object.keys(c).length||c.error){i++,i<=t&&(r=Math.min(r+1e3,n),await l(r));continue}if(c.partner_id)return c.partner_id;i++,i<=t&&(r=Math.min(r+1e3,n),await l(r))}catch(o){throw o}return null}async function J(e){const t=await W(e);t&&K(t)}async function L(e){if(!e||!e.userId)throw new Error("[magnite] Missing params");if(!e.magnitePartnerKey)throw new Error("[magnite] Missing partner key param");if(!e.magniteEndpoint)throw new Error("[magnite] Missing endpoint param");let t=E();if(!t){const n=p(a);if(z(e.userId,J,n,e.magnitePartnerKey,e.magniteEndpoint),t=E(),!t)throw new Error("[magnite] Unable to retrieve MAG listener ID")}return t}const _="ttd_match_tag",m="ttd_id";function R(e,t){const{userId:n,ttdPId:i,ttdId:r,gdpr:o,gdprConsent:c}=t,f=o?1:0;let s="",h="";const g=r||d.getProperty(m);return g&&(h=`&ttd_id=${g}`),f===1&&c&&(s=`&gdpr_consent=${c}`),`${e}?ttd_pid=${i}${h}&ttd_puid=${n}&ttd_tpi=1&gdpr=${f}${s}&domain=${k()}`}function F(e,t,n,i){const r=t.querySelector(`#${_}`);r&&t.removeChild(r),V(e,t,n,i)}function V(e,t,n,i){const r=document.createElement("img");r.id=_,r.src=n,r.height=1,r.width=1,r.alt="",r.style.borderStyle="none",r.onload=()=>i(e),t.appendChild(r)}async function H(e,t=10,n=1e4){let i=1,r=1e3;for(;i<=t;)try{const o=await fetch(`https://sync.amperwave.net/api/get/TTD/${e}`);if(!o.ok){i++,i<=t&&(r=Math.min(r+1e3,n),await l(r));continue}const c=await o.json();if(!Object.keys(c).length||c.error){i++,i<=t&&(r=Math.min(r+1e3,n),await l(r));continue}if(c.partner_id)return c.partner_id;i++,i<=t&&(r=Math.min(r+1e3,n),await l(r))}catch(o){throw o}return null}async function Q(e){const t=await H(e);t&&d.setProperty(m,t)}async function Z(e){if(!e||!e.userId)throw new Error("[tradedesk] Missing params");if(!e.ttdPId)throw new Error("[tradedesk] Missing TTD Partner ID (ttd_pid)");const t=p(a),n=R("https://match.adsrvr.org/track/cmf/generic",e);if(F(e.userId,t,n,Q),!d.getProperty(m))throw new Error("[tradedesk] Unable to retrieve TTD ID");return e.userId||""}function Y(e){return e}const v={AWZ:{queryParam:"a_partner_ids",sync:A},TAP:{queryParam:"t_partner_ids",sync:q},MAG:{queryParam:"m_partner_ids",sync:L},TTD:{queryParam:"awid",sync:Z}};async function B(e){var n;if(!e||!Object.keys(e).length)throw new Error("Missing params");e.userId=C(e.userId);const t=(n=e.enabledDSPS)!=null&&n.length?e.enabledDSPS:Object.keys(v);return new Promise(i=>{const r={};let o=!1,c;e.timeout!==void 0&&(c=setTimeout(()=>{o=!0,i(r)},e.timeout)),Promise.all(t.map(f=>{const s=v[f],h=w+`${f}_${e.userId}`,g=d.getProperty(h);if(g&&(r[s.queryParam]=g),!r[s.queryParam]||e.skipCache)return s.cachedPromise||(s.cachedPromise=s.sync(e)),s.cachedPromise.then(y=>{o||(r[s.queryParam]=y,d.setProperty(h,y))}).catch(y=>{s.cachedPromise=void 0;const X=T(y);console.error(X)})})).then(()=>{clearTimeout(c),o=!0,i(r)})})}u.syncCookies=B,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});