"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const tempy = require("tempy");
const createIndexHtml_1 = require("./createIndexHtml");
const generateTestBundle_1 = require("./generateTestBundle");
const webpack_1 = require("./webpack");
function setupBrowser(cwd, port, timeout, logger, testMetadata, extendConfiguration) {
    return __awaiter(this, void 0, void 0, function* () {
        const outputDirectory = tempy.directory();
        const temporaryPath = path_1.join(outputDirectory, path_1.basename(tempy.file({ extension: 'ts' })));
        const bundlePath = path_1.join(outputDirectory, path_1.basename(tempy.file({ extension: 'js' })));
        const browserApiFile = generateTestBundle_1.generateTestBundle(cwd, outputDirectory, port, timeout, testMetadata);
        const indexHtmlPath = path_1.join(outputDirectory, 'index.html');
        const { writeFileSync } = require('fs');
        writeFileSync(temporaryPath, browserApiFile);
        yield webpack_1.bundleFileOrExit(cwd, temporaryPath, bundlePath, logger, extendConfiguration);
        writeFileSync(indexHtmlPath, createIndexHtml_1.createIndexHtml(path_1.basename(bundlePath)));
        return { outputDirectory, bundlePath, indexHtmlPath };
    });
}
exports.setupBrowser = setupBrowser;
//# sourceMappingURL=setupBrowser.js.map