import{t as e}from"./config-BYRrjYC5.mjs";import{t}from"./logger-B0fjfmRH.mjs";import{t as n}from"./proxy-CXHoVziX.mjs";import{chromium as r}from"playwright";const i=e=>e===`networkidle0`||e===`networkidle2`?`networkidle`:e,a=e=>e&&{...e,waitUntil:i(e.waitUntil)},o=e=>`domain`in e&&!(`path`in e)?{...e,path:`/`}:e,s=e=>`${e.protocol.replace(`socks5h:`,`socks5:`).replace(`socks4a:`,`socks4:`)}//${e.host}`,c=e=>{if(!e)return{};let n=e.urlHandler?.username,r=e.urlHandler?.password;return n||r?e.urlHandler.protocol===`http:`?{proxy:{password:decodeURIComponent(r??``),server:s(e.urlHandler),username:decodeURIComponent(n??``)}}:(t.warn(`SOCKS/HTTPS proxy with authentication is not supported by playwright, continue without proxy`),{}):{proxy:{server:e.uri.replace(`socks5h://`,`socks5://`).replace(`socks4a://`,`socks4://`).replace(/\/$/,``)}}},l=t=>({args:[`--no-sandbox`,`--disable-setuid-sandbox`,`--disable-blink-features=AutomationControlled`,`--window-position=0,0`,`--ignore-certificate-errors`,`--ignore-certificate-errors-spki-list`],executablePath:e.chromiumExecutablePath||void 0,headless:!0,...c(t)}),u=()=>({ignoreHTTPSErrors:!0}),d=e=>{let t=e.request(),n={abort:async t=>{n.handled=!0,await e.abort(t)},continue:async t=>{n.handled=!0,await e.continue(t)},handled:!1,resourceType:()=>t.resourceType(),url:()=>t.url()};return n},f=async(e,t,n=0)=>{t.handled||n>=e.length||(await e[n](t),await f(e,t,n+1))},p=(e,t)=>({response:()=>t?{status:()=>t.status()}:null,url:()=>e.url()}),m=(e,t)=>{let n=e,r=[],i=e.goto.bind(e),s=e.on.bind(e),c=e.route.bind(e),l=e.unroute.bind(e),u,m=!1;return n.goto=(e,t)=>i(e,a(t)),n.cookies=e=>t.cookies(e),n.setCookie=async(...e)=>{await t.addCookies(e.map(e=>o(e)))},n.authenticate=async()=>{},n.setUserAgent=async n=>{let r=t;if(r.newCDPSession){let t=await r.newCDPSession(e);await t.send(`Network.setUserAgentOverride`,{userAgent:n}),await t.detach();return}await e.setExtraHTTPHeaders({"User-Agent":n})},n.setRequestInterception=async e=>{m=e,e&&!u?(u=async e=>{let t=d(e);await f(r,t),!t.handled&&await e.continue()},await c(`**/*`,u)):!e&&u&&(await l(`**/*`,u),u=void 0)},n.on=((e,t)=>e===`request`?(r.push(t),m||s(e,t),n):e===`requestfinished`?(s(e,async e=>{let n=null;try{n=await e.response()}catch{}await t(p(e,n))}),n):(s(e,t),n)),n},h=async(t,n)=>{let r=await t.newContext(n),i=t,a=t.close.bind(t);return i.newPage=async()=>m(await r.newPage(),r),i.setCookie=async(...e)=>{await r.addCookies(e.map(e=>o(e)))},i.cookies=e=>r.cookies(e),i.userAgent=()=>e.ua,i.close=async e=>{try{await r.close()}catch{}await a(e)},i},g=async t=>{let n=l(t);return h(e.playwrightWSEndpoint?await r.connect(_(e.playwrightWSEndpoint,n)):await r.launch(n),u())},_=(e,t)=>{let n=new URL(e);return n.searchParams.set(`launch-options`,JSON.stringify(t)),n.toString()},v=(e,t=3e4)=>{setTimeout(()=>{e.close()},t)},y=async()=>{let e=n.getCurrentProxy(),t=await g(e&&n.proxyObj.url_regex===`.*`?e:null);return v(t),t},b=async(e,r={})=>{let i=!1,a=new RegExp(n.proxyObj.url_regex),o;try{o=new URL(e)}catch{}a.test(e)&&e.startsWith(`http`)&&!(o&&o.host===n.proxyUrlHandler?.host)&&(i=!0);let s=n.getCurrentProxy(),l=s&&i?s:null,u=!!c(l).proxy,d=await g(l);v(d,r.closeTimeout);let f=await d.newPage();if(u&&l&&t.debug(`Proxying request in playwright via ${l.uri}: ${e}`),r.onBeforeLoad&&await r.onBeforeLoad(f,d),!r.noGoto)try{await f.goto(e,r.gotoConfig||{waitUntil:`domcontentloaded`})}catch(e){throw u&&l&&n.multiProxy?(t.warn(`Playwright navigation failed with proxy ${l.uri}, marking as failed: ${e}`),n.markProxyFailed(l.uri),e):e}return{browser:d,destroy:async()=>{await d.close()},page:f}};export{y as n,b as t};