import { Browser, chromium, LaunchOptions, Page } from 'playwright';

export async function launchBrowserWithISPProxy(country?: string, ip?: string): Promise<{ browser: Browser; page: Page }> {
  const proxyServer = 'http://brd.superproxy.io:33335';
  const username = ip
    ? `brd-customer-hl_bc33aa9b-zone-isp_proxy_perifit-country-${country}-ip-${ip}`
    : `brd-customer-hl_bc33aa9b-zone-isp_proxy_perifit-country-${country}`;

  const password = 'oz7t5zllihr4';

  const options: LaunchOptions = {
    channel: 'chrome',
    proxy: {
      server: proxyServer,
      username,
      password,
    },
    headless: process.env.FORCE_HEADED ? false : true
  };

  const browser = await chromium.launch(options);
  const page = await browser.newPage({ ignoreHTTPSErrors: true });
  return { browser, page };
}