/* esm.sh - bgutils-js@3.2.0 */ var N=Object.defineProperty;var S=(n,t)=>{for(var e in t)N(n,e,{get:t[e],enumerable:!0})};var F={};S(F,{BotGuardClient:()=>l,Challenge:()=>E,PoToken:()=>x,WebPoMinter:()=>f});var E={};S(E,{create:()=>G,descramble:()=>U,parseChallengeData:()=>R});var A="https://jnn-pa.googleapis.com",M="https://www.youtube.com",_="AIzaSyDyT5W0Jh49F30Pqqtyfdf7pDLFKLJoAnw",k="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36(KHTML, like Gecko)";var P=/[-_.]/g,B={"-":"+",_:"/",".":"="},g=class{constructor(){this.promise=new Promise((t,e)=>{this.resolve=t,this.reject=e})}},i=class extends TypeError{constructor(t,e,r){super(e),this.name="BGError",this.code=t,r&&(this.info=r)}};function m(n){let t;return P.test(n)?t=n.replace(P,function(e){return B[e]}):t=n,t=atob(t),new Uint8Array([...t].map(e=>e.charCodeAt(0)))}function y(n,t=!1){let e=btoa(String.fromCharCode(...n));return t?e.replace(/\+/g,"-").replace(/\//g,"_"):e}function V(){let n=typeof window<"u"&&typeof window.document<"u"&&typeof window.document.createElement<"u"&&typeof window.HTMLElement<"u"&&typeof window.navigator<"u"&&typeof window.getComputedStyle=="function"&&typeof window.requestAnimationFrame=="function"&&typeof window.matchMedia=="function",t=Object.getOwnPropertyDescriptor(globalThis,"window")?.get?.toString().includes("[native code]")??!1;return n&&t}function T(){let n={"content-type":"application/json+protobuf","x-goog-api-key":_,"x-user-agent":"grpc-web-javascript/0.1"};return V()||(n["user-agent"]=k),n}function b(n,t){return`${t?M:A}/${t?"api/jnn/v1":"$rpc/google.internal.waa.v1.Waa"}/${n}`}async function G(n,t){let e=n.requestKey;if(!n.fetch)throw new i("BAD_CONFIG","No fetch function provided");let r=[e];t&&r.push(t);let o=await n.fetch(b("Create",n.useYouTubeAPI),{method:"POST",headers:T(),body:JSON.stringify(r)});if(!o.ok)throw new i("REQUEST_FAILED","Failed to fetch challenge",{status:o.status});let a=await o.json();return R(a)}function R(n){let t=[];if(n.length>1&&typeof n[1]=="string"){let u=U(n[1]);t=JSON.parse(u||"[]")}else n.length&&typeof n[0]=="object"&&(t=n[0]);let[e,r,o,a,s,d,,p]=t,c=Array.isArray(r)?r.find(u=>u&&typeof u=="string"):null,w=Array.isArray(o)?o.find(u=>u&&typeof u=="string"):null;return{messageId:e,interpreterJavascript:{privateDoNotAccessOrElseSafeScriptWrappedValue:c,privateDoNotAccessOrElseTrustedResourceUrlWrappedValue:w},interpreterHash:a,program:s,globalName:d,clientExperimentsStateBlob:p}}function U(n){let t=m(n);if(t.length)return new TextDecoder().decode(t.map(e=>e+97))}var x={};S(x,{decodeColdStartToken:()=>j,generate:()=>L,generateColdStartToken:()=>v,generatePlaceholder:()=>D});var l=class n{constructor(t){this.deferredVmFunctions=new g,this.defaultTimeout=3e3,this.userInteractionElement=t.userInteractionElement,this.vm=t.globalObj[t.globalName],this.program=t.program}static async create(t){return await new n(t).load()}async load(){if(!this.vm)throw new i("VM_INIT","VM not found");if(!this.vm.a)throw new i("VM_INIT","VM init function not found");let t=(e,r,o,a)=>{this.deferredVmFunctions.resolve({asyncSnapshotFunction:e,shutdownFunction:r,passEventFunction:o,checkCameraFunction:a})};try{this.syncSnapshotFunction=await this.vm.a(this.program,t,!0,this.userInteractionElement,()=>{},[[],[]])[0]}catch(e){throw new i("VM_ERROR","Could not load program",{error:e})}return this}async snapshot(t,e=3e3){return await Promise.race([new Promise(async(r,o)=>{let a=await this.deferredVmFunctions.promise;if(!a.asyncSnapshotFunction)return o(new i("ASYNC_SNAPSHOT","Asynchronous snapshot function not found"));await a.asyncSnapshotFunction(s=>r(s),[t.contentBinding,t.signedTimestamp,t.webPoSignalOutput,t.skipPrivacyBuffer])}),new Promise((r,o)=>setTimeout(()=>o(new i("TIMEOUT","VM operation timed out")),e))])}async passEvent(t,e=this.defaultTimeout){return await Promise.race([(async()=>{let r=await this.deferredVmFunctions.promise;if(!r.passEventFunction)throw new i("PASS_EVENT","Pass event function not found");r.passEventFunction(t)})(),new Promise((r,o)=>setTimeout(()=>o(new i("TIMEOUT","VM operation timed out")),e))])}async checkCamera(t,e=this.defaultTimeout){return await Promise.race([(async()=>{let r=await this.deferredVmFunctions.promise;if(!r.checkCameraFunction)throw new i("CHECK_CAMERA","Check camera function not found");r.checkCameraFunction(t)})(),new Promise((r,o)=>setTimeout(()=>o(new i("TIMEOUT","VM operation timed out")),e))])}async shutdown(t=this.defaultTimeout){return await Promise.race([(async()=>{let e=await this.deferredVmFunctions.promise;if(!e.shutdownFunction)throw new i("SHUTDOWN","Shutdown function not found");e.shutdownFunction()})(),new Promise((e,r)=>setTimeout(()=>r(new i("TIMEOUT","VM operation timed out")),t))])}async snapshotSynchronous(t){if(!this.syncSnapshotFunction)throw new i("SYNC_SNAPSHOT","Synchronous snapshot function not found");return this.syncSnapshotFunction([t.contentBinding,t.signedTimestamp,t.webPoSignalOutput,t.skipPrivacyBuffer])}};var f=class n{constructor(t){this.mintCallback=t}static async create(t,e){let r=e[0];if(!r)throw new i("VM_ERROR","PMD:Undefined");if(!t.integrityToken)throw new i("INTEGRITY_ERROR","No integrity token provided",{integrityTokenResponse:t});let o=await r(m(t.integrityToken));if(!(o instanceof Function))throw new i("VM_ERROR","APF:Failed");return new n(o)}async mintAsWebsafeString(t){let e=await this.mint(t);return y(e,!0)}async mint(t){let e=await this.mintCallback(new TextEncoder().encode(t));if(!e)throw new i("VM_ERROR","YNJ:Undefined");if(!(e instanceof Uint8Array))throw new i("VM_ERROR","ODM:Invalid");return e}};async function L(n){let{program:t,bgConfig:e,globalName:r}=n,{identifier:o}=e,a=await l.create({program:t,globalName:r,globalObj:e.globalObj}),s=[],d=await a.snapshot({webPoSignalOutput:s}),p=[e.requestKey,d],w=await(await e.fetch(b("GenerateIT",e.useYouTubeAPI),{method:"POST",headers:T(),body:JSON.stringify(p)})).json(),[u,h,I,C]=w,O={integrityToken:u,estimatedTtlSecs:h,mintRefreshThreshold:I,websafeFallbackToken:C};return{poToken:await(await f.create(O,s)).mintAsWebsafeString(o),integrityTokenData:O}}function v(n,t){let e=new TextEncoder().encode(n);if(e.length>118)throw new i("BAD_INPUT","Content binding is too long.",{identifierLength:e.length});let r=Math.floor(Date.now()/1e3),o=[Math.floor(Math.random()*256),Math.floor(Math.random()*256)],a=o.concat([0,t??1],[r>>24&255,r>>16&255,r>>8&255,r&255]),s=new Uint8Array(2+a.length+e.length);s[0]=34,s[1]=a.length+e.length,s.set(a,2),s.set(e,2+a.length);let d=s.subarray(2),p=o.length;for(let c=p;c