"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.stopCleanupInterval = exports.startCleanupInterval = exports.isActorInGroup = exports.hasTag = exports.getActorsInGroup = exports.getActorsByTagPredicate = void 0;
const dbManager = require("./db-manager");
const notifier = require("./websocket-notifier");
var cleanupIntervalId;
function cleanupActors() {
    return __awaiter(this, void 0, void 0, function* () {
        const db = dbManager.getDb();
        let actors = yield db.getActorsByTypeAndTags();
        for (let actor of actors) {
            // TODO: Implement unannounce API and increase this timeout
            // Delete actors that are not assigned to a session and have
            // not been seen for a while
            if (!actor.testSessionId && (Date.now() - actor.lastSeenTime > 8 * 1000)) {
                yield db.deleteActor(actor.id);
                notifier.onActorsChanged(actor.id);
            }
        }
    });
}
/** Returns all the test actors from a collection whose tags conform to the
 * specified expression, for example "(tag1 || tag2) && tag3". */
function getActorsByTagPredicate(allActors, tagPredicate) {
    const results = [];
    for (let actor of allActors) {
        const currentPredicate = tagPredicate.replace(/[\w\-+\[\].\/]+/g, function (match) {
            return hasTag(actor, match) ? 'true' : 'false';
        });
        try {
            if (eval(currentPredicate)) {
                results.push(actor);
            }
        }
        catch (err) {
            console.log(`ERROR: Failed to evaluate expression "${tagPredicate}" while selecting tests by tags. The error message was: ${err.message}. Please review the expression and correct it.`);
            break;
        }
    }
    return results;
}
exports.getActorsByTagPredicate = getActorsByTagPredicate;
/** Returns all actors from the set provided that are in the specified group. */
function getActorsInGroup(allActors, group) {
    return getActorsByTagPredicate(allActors, group.actorTags);
}
exports.getActorsInGroup = getActorsInGroup;
/** Returns true if an actor has the specified tag and false otherwise. */
function hasTag(actor, requiredTag) {
    let tagWasFound = false;
    for (let testTag of actor.tags || []) {
        if (testTag === requiredTag) {
            tagWasFound = true;
            break;
        }
    }
    return tagWasFound;
}
exports.hasTag = hasTag;
function isActorInGroup(actor, group) {
    const actors = getActorsByTagPredicate([actor], group.actorTags);
    return actors && actors.length === 1;
}
exports.isActorInGroup = isActorInGroup;
function startCleanupInterval() {
    cleanupIntervalId = setInterval(cleanupActors, 5000);
}
exports.startCleanupInterval = startCleanupInterval;
function stopCleanupInterval() {
    clearInterval(cleanupIntervalId);
}
exports.stopCleanupInterval = stopCleanupInterval;
//# sourceMappingURL=actor-helper.js.map