"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Settings = void 0;
exports.getRpcInterfaces = getRpcInterfaces;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const presentation_common_1 = require("@itwin/presentation-common");
function getRpcInterfaces(settings) {
    const rpcInterfaces = [];
    if (settings.runDevToolsRpcTests)
        rpcInterfaces.push(core_common_1.DevToolsRpcInterface);
    if (settings.runPresentationRpcTests)
        rpcInterfaces.push(presentation_common_1.PresentationRpcInterface);
    if (settings.runiModelReadRpcTests)
        rpcInterfaces.push(core_common_1.IModelReadRpcInterface);
    if (settings.runiModelTileRpcTests)
        rpcInterfaces.push(core_common_1.IModelTileRpcInterface);
    return rpcInterfaces;
}
function checkEnabled(envVariable) {
    if (undefined === envVariable)
        return false;
    const regex = /true/i;
    return regex.test(envVariable);
}
class Settings {
    _backend = {};
    env = 0;
    oidcClientId;
    oidcScopes;
    oidcRedirect;
    oidcAuthority;
    gprid;
    logLevel;
    users = [];
    clientConfiguration;
    iModels = [];
    get iModel() { return this.iModels[0]; }
    get writeIModel() { return this.iModels[1]; }
    get user() { return this.users[0]; }
    // eslint-disable-next-line @typescript-eslint/naming-convention
    get Backend() { return this._backend; }
    get runiModelTileRpcTests() { return checkEnabled(process.env.RPC_IMODELTILE_ENABLE); }
    get runPresentationRpcTests() { return checkEnabled(process.env.RPC_PRESENTATION_ENABLE); }
    get runiModelReadRpcTests() { return checkEnabled(process.env.RPC_IMODELREAD_ENABLE); }
    get runiModelWriteRpcTests() { return checkEnabled(process.env.RPC_IMODELWRITE_ENABLE); }
    get runDevToolsRpcTests() { return checkEnabled(process.env.RPC_DEVTOOLS_ENABLE); }
    constructor(env) {
        const isFrontend = (typeof (process) === "undefined");
        if (!isFrontend && undefined === env.TF_BUILD) {
            const path = require("path"); // eslint-disable-line @typescript-eslint/no-require-imports
            const dotenv = require("dotenv"); // eslint-disable-line @typescript-eslint/no-require-imports
            const dotenvExpand = require("dotenv-expand"); // eslint-disable-line @typescript-eslint/no-require-imports
            // First check in process.cwd() for the config
            let result = dotenv.config();
            if (result.error) {
                const potential = path.resolve(process.cwd(), "..", "..", "..", "imodeljs-config", ".env");
                result = dotenv.config({ path: potential });
                if (result.error)
                    throw result.error;
            }
            dotenvExpand(result);
        }
        if (isFrontend)
            globalThis.process = { browser: true, env };
        // Loads the config out of the environment.
        this.load();
    }
    /** Loads the necessary variables from `process.env`.
     */
    load() {
        // Parse OIDC
        if (undefined === process.env.OIDC_CLIENT_ID)
            throw new Error("Missing the 'OIDC_CLIENT_ID' setting.");
        this.oidcClientId = process.env.OIDC_CLIENT_ID;
        if (undefined === process.env.OIDC_SCOPES)
            throw new Error("Missing the 'OIDC_SCOPES' setting");
        this.oidcScopes = process.env.OIDC_SCOPES;
        if (process.env.OIDC_AUTHORITY)
            this.oidcAuthority = process.env.OIDC_AUTHORITY;
        this.oidcRedirect = (undefined === process.env.OIDC_REDIRECT) ? "http://localhost:5000" : process.env.OIDC_REDIRECT;
        // Parse GPRId
        if (undefined !== process.env.GPRID)
            this.gprid = process.env.GPRID;
        //  Parse the iModel variables
        if (!process.env.IMODEL_PROJECTID && !process.env.IMODEL_PROJECTNAME)
            throw new Error("Missing the 'IMODEL_PROJECTID' or 'IMODEL_PROJECTNAME' setting.");
        if (!process.env.IMODEL_IMODELID && !process.env.IMODEL_IMODELNAME)
            throw new Error("Missing the 'IMODEL_IMODELID' or 'IMODEL_IMODELNAME' setting.");
        // Note: This is kind of messy but we don't sign-in to resolve the Names into IDs until the TestContext.
        this.iModels.push({
            useName: !process.env.IMODEL_IMODELID,
            id: process.env.IMODEL_IMODELID,
            name: process.env.IMODEL_IMODELNAME,
            useITwinName: !process.env.IMODEL_PROJECTID,
            iTwinId: process.env.IMODEL_PROJECTID,
            iTwinName: process.env.IMODEL_PROJECTNAME,
            // Neither of the next 2 are needed but since they'll be undefined anyway, just always set it.
            changeSetId: process.env.IMODEL_CHANGESETID,
        });
        // If write rpc interface is defined expect a separate iModel to be used.
        if (this.runiModelWriteRpcTests) {
            if (!process.env.IMODEL_WRITE_PROJECTID && !process.env.IMODEL_WRITE_PROJECTNAME)
                throw new Error("Missing the 'IMODEL_WRITE_PROJECTID' or 'IMODEL_WRITE_PROJECTNAME' setting.");
            if (!process.env.IMODEL_WRITE_IMODELID && !process.env.IMODEL_WRITE_IMODELNAME)
                throw new Error("Missing the 'IMODEL_WRITE_IMODELID' or 'IMODEL_WRITE_IMODELNAME' setting.");
            this.iModels.push({
                useName: !process.env.IMODEL_WRITE_IMODELID,
                id: process.env.IMODEL_WRITE_IMODELID,
                name: process.env.IMODEL_WRITE_IMODELNAME,
                useITwinName: !process.env.IMODEL_WRITE_PROJECTID,
                iTwinId: process.env.IMODEL_WRITE_PROJECTID,
                iTwinName: process.env.IMODEL_WRITE_PROJECTNAME,
            });
        }
        // Parse logging level
        if (undefined !== process.env.LOG_LEVEL) {
            const level = parseInt(process.env.LOG_LEVEL, 10);
            if (!isNaN(level) && undefined !== core_bentley_1.LogLevel[level])
                this.logLevel = level;
        }
        // Get backend data
        if (undefined === process.env.BACKEND_LOCATION)
            throw new Error("Missing the 'BACKEND_LOCATION' setting.");
        this._backend.location = process.env.BACKEND_LOCATION;
        if (undefined === process.env.BACKEND_VERSION)
            throw new Error("Missing the 'BACKEND_VERSION' setting.");
        this._backend.version = process.env.BACKEND_VERSION;
        if (undefined === process.env.BACKEND_NAME)
            throw new Error("Missing the 'BACKEND_NAME' setting.");
        this._backend.name = process.env.BACKEND_NAME;
        // Get users
        this.users.push({
            email: process.env.USER_WITH_ACCESS_USERNAME || "",
            password: process.env.USER_WITH_ACCESS_PASSWORD || "",
        });
        if (undefined !== process.env.CLIENT_WITH_ACCESS_ID && undefined !== process.env.CLIENT_WITH_ACCESS_SECRET && undefined !== process.env.CLIENT_WITH_ACCESS_SCOPES) {
            this.clientConfiguration = {
                clientId: process.env.CLIENT_WITH_ACCESS_ID,
                clientSecret: process.env.CLIENT_WITH_ACCESS_SECRET,
                scope: process.env.CLIENT_WITH_ACCESS_SCOPES,
                authority: this.oidcAuthority,
            };
        }
    }
    toString() {
        return `Configurations:
      backend location: ${this.Backend.location},
      backend name: ${this.Backend.name},
      backend version: ${this.Backend.version},
      oidc client id: ${this.oidcClientId},
      oidc scopes: ${this.oidcScopes},
      applicationId: ${this.gprid},
      log level: ${this.logLevel},
      testing iModelTileRpcTests: ${this.runiModelTileRpcTests},
      testing PresentationRpcTest: ${this.runPresentationRpcTests},
      testing iModelReadRpcTests: ${this.runiModelReadRpcTests},
      testing DevToolsRpcTests: ${this.runDevToolsRpcTests},
      testing iModelWriteRpcTests: ${this.runiModelWriteRpcTests}`;
    }
}
exports.Settings = Settings;
//# sourceMappingURL=Settings.js.map