"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestContext = void 0;
const chai_1 = require("chai");
const bentleyjs_core_1 = require("@bentley/bentleyjs-core");
const imodeljs_common_1 = require("@bentley/imodeljs-common");
const imodeljs_frontend_1 = require("@bentley/imodeljs-frontend");
const frontend_1 = require("@bentley/oidc-signin-tool/lib/frontend");
const Settings_1 = require("../../common/Settings");
const SideChannels_1 = require("../../common/SideChannels");
const IModelSession_1 = require("./IModelSession");
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/indent */
class TestContext {
    constructor(env) {
        this.settings = new Settings_1.Settings(env);
    }
    static async instance() {
        if (this._instance === undefined) {
            this._instance = new TestContext(await SideChannels_1.getProcessEnvFromBackend());
            await this._instance.initialize();
        }
        return this._instance;
    }
    /** Initialize configuration for the rpc interfaces used by the application. */
    initializeRpcInterfaces(info) {
        // Url without trailing slash
        const uriPrefix = this.settings.Backend.location.replace(/\/$/, "");
        imodeljs_common_1.BentleyCloudRpcManager.initializeClient({ info, uriPrefix }, Settings_1.getRpcInterfaces(this.settings));
    }
    async initialize() {
        chai_1.expect(this.settings.users.length).to.be.gte(1, `Unexpected number of users found in settings - got ${this.settings.users.length}, expected at least 2`);
        chai_1.expect(this.settings.iModels.length).to.be.gte(1, `Unexpected number of iModels found in settings - got ${this.settings.iModels.length}, expected at least 1`);
        // Print out the configuration
        console.log(this.settings.toString());
        // Configure iModel.js frontend logging to go to the console
        bentleyjs_core_1.Logger.initializeToConsole();
        bentleyjs_core_1.Logger.setLevelDefault(this.settings.logLevel === undefined ? bentleyjs_core_1.LogLevel.Warning : this.settings.logLevel);
        // Setup environment
        // eslint-disable-next-line deprecation/deprecation
        bentleyjs_core_1.Config.App.set("imjs_buddi_resolve_url_using_region", this.settings.env);
        if (undefined !== this.settings.oidcClientId) {
            this.adminUserAccessToken = await frontend_1.getAccessTokenFromBackend({
                email: this.settings.users[0].email,
                password: this.settings.users[0].password,
            }, {
                clientId: this.settings.oidcClientId,
                redirectUri: this.settings.oidcRedirect,
                scope: this.settings.oidcScopes,
            });
        }
        if (undefined !== this.settings.clientConfiguration)
            this.clientAccessToken = await SideChannels_1.getClientAccessTokenFromBackend(this.settings.clientConfiguration);
        this.initializeRpcInterfaces({ title: this.settings.Backend.name, version: this.settings.Backend.version });
        await imodeljs_frontend_1.NoRenderApp.startup({
            applicationVersion: PACKAGE_VERSION,
            applicationId: this.settings.gprid,
            authorizationClient: new frontend_1.TestFrontendAuthorizationClient(this.adminUserAccessToken),
        });
        const requestContext = new imodeljs_frontend_1.AuthorizedFrontendRequestContext(this.adminUserAccessToken);
        this.iModelWithChangesets = await IModelSession_1.IModelSession.create(requestContext, this.settings.iModel);
        this.contextId = this.iModelWithChangesets.contextId;
        if (this.settings.runiModelWriteRpcTests)
            this.iModelForWrite = await IModelSession_1.IModelSession.create(requestContext, this.settings.writeIModel);
        console.log("TestSetup: Done");
    }
}
exports.TestContext = TestContext;
TestContext._instance = undefined;
//# sourceMappingURL=TestContext.js.map