"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());
    });
};
const TestConfig = require("../../../tests/utilities/TestConfiguration");
const { TENANT_ID } = require("../../../tests/utilities/structures");
const { doInTenant } = require("@devlego/backend-core/tenancy");
const actions = require("../../actions");
const emitter = require("../../../events/index");
const env = require("../../../environment");
let config;
exports.getConfig = () => {
    if (!config) {
        config = new TestConfig(false);
    }
    return config;
};
exports.afterAll = () => {
    config.end();
};
exports.runInProd = (fn) => __awaiter(void 0, void 0, void 0, function* () {
    env._set("NODE_ENV", "production");
    let error;
    try {
        yield fn();
    }
    catch (err) {
        error = err;
    }
    env._set("NODE_ENV", "jest");
    if (error) {
        throw error;
    }
});
exports.runStep = function runStep(stepId, inputs) {
    return __awaiter(this, void 0, void 0, function* () {
        return doInTenant(TENANT_ID, () => __awaiter(this, void 0, void 0, function* () {
            let step = yield actions.getAction(stepId);
            expect(step).toBeDefined();
            return step({
                inputs,
                appId: config ? config.getAppId() : null,
                // don't really need an API key, mocked out usage quota, not being tested here
                apiKey: exports.apiKey,
                emitter,
            });
        }));
    });
};
exports.apiKey = "test";
exports.actions = actions.ACTION_DEFINITIONS;
