import puppeteer from 'puppeteer-extra';
// add stealth plugin and use defaults (all evasion techniques)
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import { ConvertHtmlToPdfModel } from '../shared';
puppeteer.use(StealthPlugin());
const CACHE = [];
async function getBrowser(proxy) {
    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 config = {};
    const item = { active: true, proxy };
    if (proxy) {
        config.args = [`--proxy-server=${proxy}`];
    }
    item.browser = await puppeteer.launch(config);
    CACHE.push(item);
    return item.browser;
}
export async function convertHTMLToPNG(source) {
    const { input, output, viewport, proxy, waitUntil } = ConvertHtmlToPdfModel.parse(source);
    const b = await getBrowser(proxy ? `${proxy}` : undefined);
    const p = await b.newPage();
    await p.goto(`${input.file.path}`);
    await p.waitForNavigation({ waitUntil });
    const opts = {
        scale: 1,
        path: output.file.path,
        printBackground: true,
        preferCSSPageSize: true,
    };
    if (viewport?.width) {
        opts.width = viewport.width;
    }
    if (viewport?.height) {
        opts.height = viewport.height;
    }
    await p.screencast(opts);
    const item = CACHE.find(x => x.browser === b);
    if (item) {
        item.active = false;
    }
}
// https://stackoverflow.com/questions/1403087/how-can-i-convert-an-html-table-to-csv
// https://stackoverflow.com/questions/37498713/how-to-export-an-html-table-as-a-xlsx-file
export async function scrapeHTMLTables(source) {
    const { input, output, viewport, proxy, waitUntil } = ConvertHtmlToPdfModel.parse(source);
    const b = await getBrowser(proxy ? `${proxy}` : undefined);
    const p = await b.newPage();
    await p.goto(`${input.file.path}`);
    await p.waitForNavigation({ waitUntil });
}
export async function scrapeHTMLMetaTags(source) {
    const { input, output, viewport, proxy, waitUntil } = ConvertHtmlToPdfModel.parse(source);
    const b = await getBrowser(proxy ? `${proxy}` : undefined);
    const p = await b.newPage();
    await p.goto(`${input.file.path}`);
    await p.waitForNavigation({ waitUntil });
}
// export async function convertHTMLToPDF(source: ConvertHtmlToPdf) {
//   const {
//     inputPath,
//     outputPath,
//     viewportWidth,
//     viewportHeight,
//     proxy,
//     waitUntil,
//   } = ConvertHtmlToPdfModel.parse(source)
//   const b = await getBrowser(proxy ? `${proxy}` : undefined)
//   const p = await b.newPage()
//   await p.goto(`${inputPath}`)
//   await p.waitForNavigation({ waitUntil })
//   const opts: Record<string, any> = {
//     scale: 1,
//     path: outputPath,
//     printBackground: true,
//     preferCSSPageSize: true,
//   }
//   if (viewportWidth) {
//     opts.width = viewportWidth
//   }
//   if (viewportHeight) {
//     opts.height = viewportHeight
//   }
//   await p.pdf(opts)
//   const item = CACHE.find(x => x.browser === b)
//   if (item) {
//     item.active = false
//   }
// }
// // function doit(type, fn) {
// //   var elt = document.getElementById('data-table')
// //   var wb = XLSX.utils.table_to_book(elt, { sheet: 'Sheet JS' })
// //   XLSX.writeFile(wb, fn || 'SheetJSTableExport.' + (type || 'xlsx'))
// // }
//# sourceMappingURL=html.js.map