"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Settings = void 0;
exports.getRpcInterfaces = getRpcInterfaces;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const ecschema_rpcinterface_common_1 = require("@itwin/ecschema-rpcinterface-common");
function getRpcInterfaces() {
    const rpcInterfaces = [];
    rpcInterfaces.push(core_common_1.IModelReadRpcInterface);
    rpcInterfaces.push(ecschema_rpcinterface_common_1.ECSchemaRpcInterface);
    return rpcInterfaces;
}
class Settings {
    // eslint-disable-next-line @typescript-eslint/naming-convention
    get Backend() { return this._backend; }
    get user() { return this.users[0]; }
    constructor(env) {
        this._backend = {};
        this.users = [];
        this.iModel = {};
        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.");
        this.iModel = {
            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,
            changesetId: process.env.IMODEL_CHANGESETID,
        };
        // 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 || "",
        });
    }
    toString() {
        return `Configurations:
      oidc client id: ${this.oidcClientId},
      oidc scopes: ${this.oidcScopes},
      applicationId: ${this.gprid},
      log level: ${this.logLevel}`;
    }
}
exports.Settings = Settings;
//# sourceMappingURL=Settings.js.map