"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const getModifier_1 = require("../tests/getModifier");
const updateModifier_1 = require("../tests/updateModifier");
const types_1 = require("../types");
const flatten_1 = require("./flatten");
function runTests(defaultTimeout, metadata) {
    return Promise.all(findTestsToRun(metadata).map(m => Promise.all(m.tests.map(runTest(defaultTimeout))).then(results => (Object.assign({}, m, { results })))));
}
exports.runTests = runTests;
function runTest(defaultTimeout) {
    return (test) => {
        const { modifier, timeout = defaultTimeout } = test[types_1.TYPED_TEST];
        return test.runTest({ timeout, skip: modifier === 'skip' });
    };
}
function findTestsToRun(metadata) {
    const tests = flatten_1.chain(x => x.tests, metadata);
    const hasOnly = tests.some(x => x[types_1.TYPED_TEST].modifier === 'only');
    return metadata.map(m => {
        const testsToRun = hasOnly
            ? m.tests.map(x => (getModifier_1.getModifier(x) === 'only' ? x : updateModifier_1.updateModifier('skip', x)))
            : m.tests;
        return Object.assign({}, m, { tests: testsToRun });
    });
}
//# sourceMappingURL=runTests.js.map