var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
const puppeteer = require('puppeteer');
let page;
let browser = null;
const { logger } = require('../util');
exports.launchHeadless = (host, remotePort) => __awaiter(this, void 0, void 0, function* () {
    browser = yield puppeteer.launch({
        args: [`--remote-debugging-port=${remotePort}`, `--disable-gpu`]
    });
    logger.verbose(`Headless has been launched`);
    page = yield browser.newPage();
    yield page.setUserAgent('5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36');
    yield page.goto(`http://${host}/runtime/runtime.html`);
    logger.verbose(`Headless page goto http://${host}/runtime/runtime.html`);
});
exports.closeHeadless = () => __awaiter(this, void 0, void 0, function* () {
    if (page) {
        yield page.close();
    }
    if (browser) {
        yield browser.close();
    }
    browser = null;
    logger.verbose(`Cloased headless`);
});
