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 { watchTestMetadata } from '../tests/watchTestMetadata';
import { findTsConfig } from '../typescript/findTsConfig';
import { findTypedTestConfigs } from './findTypedTestConfigs';
import { logResults, logTypeCheckResults } from './log';
import { TestRunner } from './TestRunner';
import { watchBrowserTests } from './watchBrowserTests';
import { findTestMetadata } from '../tests/findTestMetadata';
import { Results } from './Results';
import { getTestResults, getTestStats } from '../results';
const EXCLUDE = ['./node_modules/**'];
export function runTypedTest(userOptions) {
    return __awaiter(this, void 0, void 0, function* () {
        const cwd = process.cwd();
        const { compilerOptions, files = [], include = [], exclude = EXCLUDE } = findTsConfig(cwd);
        const fileGlobs = [...files, ...include, ...exclude.map(x => `!${x}`)];
        const typedTestConfigs = findTypedTestConfigs(compilerOptions, cwd);
        const results = new Results();
        const hasWatch = typedTestConfigs.some(x => !!x.watch) || (!!userOptions && !!userOptions.watch);
        const disposables = yield Promise.all(typedTestConfigs.map(typedTestConfig => run(Object.assign({}, typedTestConfig, userOptions), cwd, fileGlobs, compilerOptions, results, hasWatch)));
        if (!hasWatch) {
            const stats = getTestStats(getTestResults(results.getResults()));
            const exitCode = stats.failing > 0 ? 1 : 0;
            process.exit(exitCode);
        }
        return disposables;
    });
}
function run(userOptions, cwd, fileGlobs, compilerOptions, globalResults, useGlobalResults) {
    return __awaiter(this, void 0, void 0, function* () {
        const { options, results: { removeFilePath }, runTests, logger, } = new TestRunner(userOptions, useGlobalResults ? globalResults : null, cwd);
        const { updateResults } = globalResults;
        const { mode, watch, files: userFiles } = options;
        const fileGlobsToUse = userFiles.length > 0 ? userFiles : fileGlobs;
        if (mode == 'browser' && watch) {
            return watchBrowserTests(fileGlobsToUse, compilerOptions, options, cwd, logger, ({ results }) => {
                updateResults(results);
                logResults(logger, results);
            }, console.error, results => logTypeCheckResults(logger, results));
        }
        const handleMetadata = (metadata) => __awaiter(this, void 0, void 0, function* () {
            const [{ results }, processResults] = yield runTests(metadata);
            updateResults(results);
            logTypeCheckResults(logger, processResults);
            logResults(logger, results);
        });
        if (watch) {
            return watchTestMetadata(cwd, fileGlobsToUse, compilerOptions, mode, logger, removeFilePath, handleMetadata);
        }
        const metadata = yield findTestMetadata(cwd, fileGlobsToUse, compilerOptions, mode);
        yield handleMetadata(metadata);
        return { dispose: () => { } };
    });
}
//# sourceMappingURL=runTypedTest.js.map