"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 structures = require("../../../../tests/utilities/structures");
const env = require("../../../../environment");
function user() {
    return {
        _id: "user",
        _rev: "rev",
        createdAt: Date.now(),
        email: "test@test.com",
        roles: {},
        tenantId: "default",
        status: "active",
    };
}
jest.mock("../../../../utilities/workerRequests", () => ({
    getGlobalUsers: jest.fn(() => {
        return {
            _id: "us_uuid1",
        };
    }),
    getGlobalSelf: jest.fn(() => {
        return {
            _id: "us_uuid1",
        };
    }),
    allGlobalUsers: jest.fn(() => {
        return [user()];
    }),
    readGlobalUser: jest.fn(() => {
        return user();
    }),
    saveGlobalUser: jest.fn(() => {
        return { _id: "user", _rev: "rev" };
    }),
    deleteGlobalUser: jest.fn(() => {
        return { message: "deleted user" };
    }),
    removeAppFromUserRoles: jest.fn(),
}));
exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms));
let request, config;
exports.beforeAll = () => {
    config = new TestConfig();
    request = config.getRequest();
};
exports.afterAll = () => {
    if (config) {
        config.end();
    }
    // clear app files
    request = null;
    config = null;
};
exports.getRequest = () => {
    if (!request) {
        exports.beforeAll();
    }
    return request;
};
exports.getConfig = () => {
    if (!config) {
        exports.beforeAll();
    }
    return config;
};
exports.switchToSelfHosted = (func) => __awaiter(void 0, void 0, void 0, function* () {
    // self hosted stops any attempts to Dynamo
    env._set("NODE_ENV", "production");
    env._set("SELF_HOSTED", true);
    let error;
    try {
        yield func();
    }
    catch (err) {
        error = err;
    }
    env._set("NODE_ENV", "jest");
    env._set("SELF_HOSTED", false);
    // don't throw error until after reset
    if (error) {
        throw error;
    }
});
exports.structures = structures;
