import https from"https";import os from"os";import ProgressBar from"progress";import puppeteer from"../node.js";import{PUPPETEER_REVISIONS}from"../revisions.js";const supportedProducts={chrome:"Chromium",firefox:"Firefox Nightly"};export async function downloadBrowser(){const o=process.env.PUPPETEER_DOWNLOAD_HOST||process.env.npm_config_puppeteer_download_host||process.env.npm_package_config_puppeteer_download_host,e=process.env.PUPPETEER_PRODUCT||process.env.npm_config_puppeteer_product||process.env.npm_package_config_puppeteer_product||"chrome",r=process.env.PUPPETEER_DOWNLOAD_PATH||process.env.npm_config_puppeteer_download_path||process.env.npm_package_config_puppeteer_download_path,n=puppeteer.createBrowserFetcher({product:e,host:o,path:r}),t=await function(){if("chrome"===e)return process.env.PUPPETEER_CHROMIUM_REVISION||process.env.npm_config_puppeteer_chromium_revision||PUPPETEER_REVISIONS.chromium;if("firefox"===e)return puppeteer._preferredRevision=PUPPETEER_REVISIONS.firefox,function(){const o="https://product-details.mozilla.org/1.0/firefox_versions.json";return new Promise(((e,r)=>{let n="";logPolitely(`Requesting latest Firefox Nightly version from ${o}`),https.get(o,(o=>{if(o.statusCode>=400)return r(new Error(`Got status code ${o.statusCode}`));o.on("data",(o=>{n+=o})),o.on("end",(()=>{try{const o=JSON.parse(n);return e(o.FIREFOX_NIGHTLY)}catch{return r(new Error("Firefox version not found"))}}))})).on("error",r)}))}().catch((o=>{console.error(o),process.exit(1)}));throw new Error(`Unsupported product ${e}`)}();await function(o){const r=n.revisionInfo(o);if(r.local)return void logPolitely(`${supportedProducts[e]} is already in ${r.folderPath}; skipping download.`);const t=process.env.npm_config_https_proxy||process.env.npm_config_proxy,s=process.env.npm_config_http_proxy||process.env.npm_config_proxy,p=process.env.npm_config_no_proxy;t&&(process.env.HTTPS_PROXY=t);s&&(process.env.HTTP_PROXY=s);p&&(process.env.NO_PROXY=p);let c=null,i=0;return n.download(r.revision,(function(r,n){c||(c=new ProgressBar(`Downloading ${supportedProducts[e]} r${o} - ${function(o){const e=o/1024/1024;return Math.round(10*e)/10+" Mb"}(n)} [:bar] :percent :etas `,{complete:"=",incomplete:" ",width:20,total:n}));const t=r-i;i=r,c.tick(t)})).then((()=>n.localRevisions())).then((function(o){"arm64"!==os.arch()&&logPolitely(`${supportedProducts[e]} (${r.revision}) downloaded to ${r.folderPath}`),o=o.filter((o=>o!==r.revision));const t=o.map((o=>n.remove(o)));Promise.all([...t])})).catch((function(r){console.error(`ERROR: Failed to set up ${supportedProducts[e]} r${o}! Set "PUPPETEER_SKIP_DOWNLOAD" env variable to skip download.`),console.error(r),process.exit(1)}))}(t)}export function logPolitely(o){const e=process.env.npm_config_loglevel;["silent","error","warn"].indexOf(e)>-1||console.log(o)}