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());
    });
};
import { typecheckInAnotherProcess } from '../typescript/typeCheckInAnotherProcess';
import { Results } from './Results';
import { runBrowserTests } from './runBrowserTests';
import { runNodeTests } from './runNodeTests';
const defaultOptions = {
    mode: 'node',
    files: [],
    timeout: 2000,
    browser: 'chrome-headless',
    keepAlive: false,
    typeCheck: false,
    watch: false,
    webpackConfiguration: x => x,
};
const defaultLogger = {
    log: (x) => Promise.resolve(console.log(x)),
    error: (x) => Promise.resolve(console.error(x)),
    clear: () => Promise.resolve(console.clear()),
};
export class TestRunner {
    constructor(userOptions, previousResults, cwd = process.cwd(), logger) {
        this.runTests = (metadata) => __awaiter(this, void 0, void 0, function* () {
            const { run, options, cwd, logger } = this;
            const { typeCheck } = options;
            const sourcePaths = Array.from(new Set(metadata.map(x => x.filePath)));
            const [testResults, processResults = { exitCode: 0 }] = yield Promise.all([
                run(options, cwd, logger, metadata),
                typeCheck
                    ? logger
                        .log('Typechecking...')
                        .then(() => typecheckInAnotherProcess(cwd, sourcePaths))
                        .then(results => logger.log('Typechecking complete.').then(() => results))
                    : Promise.resolve(void 0),
            ]);
            return [testResults, processResults];
        });
        this.setLogger = (logger) => {
            this.logger = logger;
        };
        const options = Object.assign({}, defaultOptions, userOptions);
        this.logger = logger || defaultLogger;
        this.cwd = cwd;
        this.options = options;
        this.results = previousResults || new Results();
        if (options.mode === 'node') {
            this.run = runNodeTests;
        }
        else {
            this.run = runBrowserTests;
        }
    }
}
//# sourceMappingURL=TestRunner.js.map