"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_common_1 = require("@itwin/core-common");
const core_frontend_1 = require("@itwin/core-frontend");
const frontend_1 = require("@itwin/oidc-signin-tool/lib/cjs/frontend");
const imodels_access_frontend_1 = require("@itwin/imodels-access-frontend");
const imodels_client_management_1 = require("@itwin/imodels-client-management");
const Settings_1 = require("../../common/Settings");
const SideChannels_1 = require("../../common/SideChannels");
const IModelSession_1 = require("./IModelSession");
/* eslint-disable no-console */
class TestContext {
    clientAccessToken;
    serviceAuthToken;
    iModelWithChangesets;
    iModelForWrite;
    iTwinId;
    settings;
    constructor(env) {
        this.settings = new Settings_1.Settings(env);
    }
    static _instance = undefined;
    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)(this.settings));
    }
    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`);
        (0, 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 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 (this.settings?.clientConfiguration && this.settings.clientConfiguration.clientId) {
            this.serviceAuthToken = await (0, frontend_1.getServiceAuthTokenFromBackend)({
                clientId: this.settings.clientConfiguration.clientId,
                clientSecret: this.settings.clientConfiguration.clientSecret,
                scope: this.settings.clientConfiguration.scope,
                authority: `https://${process.env.IMJS_URL_PREFIX === "dev-"
                    ? "qa-"
                    : process.env.IMJS_URL_PREFIX ?? ""}ims.bentley.com`,
            });
        }
        if (undefined !== this.settings.clientConfiguration)
            this.clientAccessToken = await (0, SideChannels_1.getClientAccessTokenFromBackend)();
        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({
            applicationVersion: PACKAGE_VERSION,
            applicationId: this.settings.gprid,
            authorizationClient: new frontend_1.TestFrontendAuthorizationClient(this.serviceAuthToken),
            hubAccess: new imodels_access_frontend_1.FrontendIModelsAccess(iModelClient),
        });
        this.iModelWithChangesets = await IModelSession_1.IModelSession.create(this.serviceAuthToken, this.settings.iModel);
        this.iTwinId = this.iModelWithChangesets.iTwinId;
        if (this.settings.runiModelWriteRpcTests)
            this.iModelForWrite = await IModelSession_1.IModelSession.create(this.serviceAuthToken, this.settings.writeIModel);
    }
}
exports.TestContext = TestContext;
//# sourceMappingURL=TestContext.js.map