"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IModelSession = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const chai_1 = require("chai");
const imodeljs_frontend_1 = require("@bentley/imodeljs-frontend");
const imodelhub_client_1 = require("@bentley/imodelhub-client");
const context_registry_client_1 = require("@bentley/context-registry-client");
const imodeljs_common_1 = require("@bentley/imodeljs-common");
class IModelSession {
    constructor(iTwinId, imodelId, changesetId) {
        this.iTwinId = iTwinId;
        this.iModelId = imodelId;
        this.changesetId = changesetId;
        this._imodelVersion = changesetId ? imodeljs_common_1.IModelVersion.asOfChangeSet(changesetId) : imodeljs_common_1.IModelVersion.latest();
    }
    static async create(requestContext, iModelData) {
        let iTwinId;
        let imodelId;
        // Turn the project name into an id
        if (iModelData.useProjectName) {
            const client = new context_registry_client_1.ContextRegistryClient();
            const project = await client.getProject(requestContext, {
                $select: "*",
                $filter: `Name+eq+'${iModelData.projectName}'`,
            });
            iTwinId = project.wsgId;
        }
        else
            iTwinId = iModelData.projectId;
        if (iModelData.useName) {
            const imodelClient = new imodelhub_client_1.IModelHubClient();
            const imodels = await imodelClient.iModels.get(requestContext, iTwinId, new imodelhub_client_1.IModelQuery().byName(iModelData.name));
            if (undefined === imodels || imodels.length === 0)
                throw new Error(`The iModel ${iModelData.name} does not exist in project ${iTwinId}.`);
            imodelId = imodels[0].wsgId;
        }
        else
            imodelId = iModelData.id;
        console.log(`Using iModel { name:${iModelData.name}, id:${iModelData.id}, projectId:${iModelData.projectId}, changesetId:${iModelData.changeSetId} }`); // eslint-disable-line no-console
        return new IModelSession(iTwinId, imodelId, iModelData.changeSetId);
    }
    async getConnection() {
        return undefined === this._iModel ? this.open() : this._iModel;
    }
    async open() {
        var _a;
        try {
            const env = (_a = process.env.IMJS_BUDDI_RESOLVE_URL_USING_REGION) !== null && _a !== void 0 ? _a : "";
            // eslint-disable-next-line no-console
            console.log(`Environment: ${env}`);
            this._iModel = await imodeljs_frontend_1.CheckpointConnection.openRemote(this.iTwinId, this.iModelId, this._imodelVersion);
            chai_1.expect(this._iModel).to.exist;
        }
        catch (e) {
            throw new Error(`Failed to open test iModel. Error: ${e.message}`);
        }
        return this._iModel;
    }
}
exports.IModelSession = IModelSession;
//# sourceMappingURL=IModelSession.js.map