import puppeteer from 'puppeteer';
const CACHE = [];
export function inactivateBrowser(b) {
    const item = CACHE.find(x => x.browser === b);
    if (item) {
        item.active = false;
    }
}
export async function closeAllBrowsers() {
    for (const item of CACHE) {
        await item.browser.close();
    }
}
export async function getBrowser(proxy, headless = true) {
    const b = CACHE.find(item => {
        if (item.active) {
            return false;
        }
        if (proxy) {
            return item.proxy === proxy;
        }
        else if (item.proxy) {
            return false;
        }
        return true;
    });
    if (b) {
        return b.browser;
    }
    // const executablePath = await locateChrome()
    const config = {
        headless: true,
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--user-data-dir',
        ],
        // executablePath,
    };
    const item = { active: true, proxy };
    // if (proxy) {
    //   config.push(`--proxy-server=${proxy}`)
    // }
    // disable user-data-dir
    // config.push('--user-data-dir')
    item.browser = await puppeteer.launch({
        dumpio: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
    });
    CACHE.push(item);
    return item.browser;
}
//# sourceMappingURL=browser.js.map