"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 core_bentley_1 = require("@itwin/core-bentley");
const core_frontend_1 = require("@itwin/core-frontend");
const frontend_1 = require("@itwin/oidc-signin-tool/lib/cjs/frontend");
const Settings_1 = require("../../common/Settings");
const SideChannels_1 = require("../../common/SideChannels");
const IModelSession_1 = require("./IModelSession");
const core_common_1 = require("@itwin/core-common");
const imodels_access_frontend_1 = require("@itwin/imodels-access-frontend");
const imodels_client_management_1 = require("@itwin/imodels-client-management");
class TestContext {
    constructor(env) {
        this.settings = new Settings_1.Settings(env);
    }
    static async instance() {
        if (this._instance === undefined) {
            this._instance = new TestContext(await (0, 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(/\/$/, "");
        core_common_1.BentleyCloudRpcManager.initializeClient({ info, uriPrefix }, (0, Settings_1.getRpcInterfaces)());
    }
    async initialize() {
        (0, 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`);
        // Print out the configuration
        console.log(this.settings.toString()); // eslint-disable-line
        // Configure iTwin.js frontend logging to go to the console
        core_bentley_1.Logger.initializeToConsole();
        core_bentley_1.Logger.setLevelDefault(this.settings.logLevel === undefined ? core_bentley_1.LogLevel.Warning : this.settings.logLevel);
        if (undefined !== this.settings.oidcClientId) {
            this.adminUserAccessToken = await (0, 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,
                authority: this.settings.oidcAuthority,
            });
        }
        const iModelData = this.settings.iModel;
        this.iModelWithChangesets = await IModelSession_1.IModelSession.create(this.adminUserAccessToken, iModelData);
        this.iTwinId = this.iModelWithChangesets.iTwinId;
        this.initializeRpcInterfaces({ title: this.settings.Backend.name, version: this.settings.Backend.version });
        const iModelClient = new imodels_client_management_1.IModelsClient({ api: { baseUrl: `https://${process.env.IMJS_URL_PREFIX ?? ""}api.bentley.com/imodels` } });
        await core_frontend_1.NoRenderApp.startup({
            applicationId: this.settings.gprid,
            authorizationClient: new frontend_1.TestFrontendAuthorizationClient(this.adminUserAccessToken),
            hubAccess: new imodels_access_frontend_1.FrontendIModelsAccess(iModelClient),
        });
        console.log("TestSetup: Done"); // eslint-disable-line
    }
}
exports.TestContext = TestContext;
TestContext._instance = undefined;
//# sourceMappingURL=TestContext.js.map