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());
    });
};
// tslint:disable-next-line:no-var-requires
const launcher = require('james-browser-launcher');
export function getLauncher() {
    return new Promise((resolve, reject) => {
        launcher((err, launch) => {
            if (err) {
                return reject(err);
            }
            resolve(launch);
        });
    });
}
export function openBrowser(browser, url, keepAlive, launch) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve, reject) => {
            const args = browser === 'chrome-headless' ? ['--disable-gpu', '--headless'] : ['--disable-gpu'];
            launch(url, { browser, options: args, detached: keepAlive }, (error, instance) => {
                if (error) {
                    return reject(error);
                }
                if (keepAlive) {
                    instance.process.unref();
                }
                else {
                    process.on('exit', () => tryStopInstance(instance));
                }
                resolve(instance);
            });
        });
    });
}
function tryStopInstance(browser) {
    try {
        browser.stop();
    }
    catch (_a) {
        return void 0;
    }
}
//# sourceMappingURL=openBrowser.js.map