import*as e from"../../core/root/root.js";import*as t from"../../services/puppeteer/puppeteer.js";import*as s from"../../third_party/third-party-web/third-party-web.js";import"../../third_party/lighthouse/lighthouse-dt-bundle.js";class n{sessionId;onMessage;onDisconnect;constructor(e){this.sessionId=e,this.onMessage=null,this.onDisconnect=null}setOnMessage(e){this.onMessage=e}setOnDisconnect(e){this.onDisconnect=e}getOnDisconnect(){return this.onDisconnect}getSessionId(){return this.sessionId}sendRawMessage(e){r("sendProtocolMessage",{message:e})}async disconnect(){this.onDisconnect?.("force disconnect"),this.onDisconnect=null,this.onMessage=null}}let o,a;async function i(i,c){let l;e.Runtime.Runtime.queryParam("isUnderTest")&&(console.log=()=>{},c.flags.maxWaitForLoad=2e3),self.listenForStatus((e=>{r("statusUpdate",{message:e[1]})}));try{if("endTimespan"===i){if(!a)throw new Error("Cannot end a timespan before starting one");const e=await a();return a=void 0,e}const r=await async function(t){const s=self.lookupLocale(t);if("en-US"===s||"en"===s)return;try{const t=e.Runtime.getRemoteBase();let n;n=t?.base?`${t.base}third_party/lighthouse/locales/${s}.json`:new URL(`../../third_party/lighthouse/locales/${s}.json`,import.meta.url).toString();const o=new Promise(((e,t)=>setTimeout((()=>t(new Error("timed out fetching locale"))),5e3))),a=await Promise.race([o,fetch(n).then((e=>e.json()))]);return self.registerLocaleData(s,a),s}catch(e){console.error(e)}return}(c.locales),g=c.flags;g.logLevel=g.logLevel||"info",g.channel="devtools",g.locale=r;const p=c.config||self.createConfig(c.categoryIDs,g.formFactor),f=c.url;self.thirdPartyWeb.provideThirdPartyWeb(s.ThirdPartyWeb);const{rootTargetId:d,mainSessionId:h}=c;o=new n(h),l=await t.PuppeteerConnection.PuppeteerConnectionHelper.connectPuppeteerToConnectionViaTab({connection:o,rootTargetId:d,isPageTargetCallback:e=>"page"===e.type});const{page:u}=l;if(!u)throw new Error("Could not create page handle for the target page");if("snapshot"===i)return await self.snapshot(u,{config:p,flags:g});if("startTimespan"===i){const e=await self.startTimespan(u,{config:p,flags:g});return void(a=e.endTimespan)}return await self.navigation(u,f,{config:p,flags:g})}catch(e){return{fatal:!0,message:e.message,stack:e.stack}}finally{"startTimespan"!==i&&await(l?.browser.disconnect())}}function r(e,t){self.postMessage({action:e,args:t})}self.onmessage=async function(e){const t=e.data;switch(t.action){case"startTimespan":case"endTimespan":case"snapshot":case"navigation":{const e=await i(t.action,t.args);e&&"object"==typeof e&&("report"in e&&delete e.report,"artifacts"in e&&(e.artifacts.Timing=JSON.parse(JSON.stringify(e.artifacts.Timing)))),self.postMessage({id:t.id,result:e});break}case"dispatchProtocolMessage":o?.onMessage?.(t.args.message);break;default:throw new Error(`Unknown event: ${e.data}`)}},globalThis.global=self,globalThis.global.isVinn=!0,globalThis.global.document={},globalThis.global.document.documentElement={},globalThis.global.document.documentElement.style={WebkitAppearance:"WebkitAppearance"},self.postMessage("workerReady");
