import*as Root from"../root/root.js";function disableLoggingForTest(){console.log=()=>{}}class LighthousePort{constructor(){this._onMessage,this._onClose}on(e,o){"message"===e?this._onMessage=o:"close"===e&&(this._onClose=o)}send(e){notifyFrontendViaWorkerMessage("sendProtocolMessage",{message:e})}}const port=new LighthousePort;async function start(e){Root.Runtime.Runtime.queryParam("isUnderTest")&&(disableLoggingForTest(),e.flags.maxWaitForLoad=2e3),self.listenForStatus((e=>{notifyFrontendViaWorkerMessage("statusUpdate",{message:e[1]})}));try{const o=await fetchLocaleData(e.locales),s=e.flags;s.logLevel=s.logLevel||"info",s.channel="devtools",s.locale=o;const t=self.setUpWorkerConnection(port),a=self.createConfig(e.categoryIDs,s.emulatedFormFactor),n=e.url;return self.runLighthouse(n,s,a,t)}catch(o){return{fatal:!0,message:o.message,stack:o.stack}}}async function fetchLocaleData(e){const o=self.lookupLocale(e);if("en-US"!==o&&"en"!==o)try{let e;const s=Root.Runtime.getRemoteBase();if(s&&s.base){const t=`${s.base}third_party/lighthouse/locales/${o}.json`;e=Root.Runtime.loadResourcePromise(t)}else{e=Root.Runtime.Runtime.instance().module("lighthouse_worker").fetchResource(`../third_party/lighthouse/locales/${o}.json`)}const t=new Promise(((e,o)=>setTimeout((()=>o(new Error("timed out fetching locale"))),5e3))),a=await Promise.race([t,e]),n=JSON.parse(a);return self.registerLocaleData(o,n),o}catch(s){console.error(s)}}function notifyFrontendViaWorkerMessage(e,o){self.postMessage(JSON.stringify({method:e,params:o}))}self.onmessage=async e=>{const o=JSON.parse(e.data);if("start"===o.method){const e=await start(o.params);self.postMessage(JSON.stringify({id:o.id,result:e}))}else{if("dispatchProtocolMessage"!==o.method)throw new Error(`Unknown event: ${e.data}`);port._onMessage&&port._onMessage(o.params.message)}},globalThis.global=self,globalThis.global.isVinn=!0,globalThis.global.document={},globalThis.global.document.documentElement={},globalThis.global.document.documentElement.style={WebkitAppearance:"WebkitAppearance"};