"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 bentleyjs_core_1 = require("@bentley/bentleyjs-core");
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(contextId, imodelId, changesetId) {
        this.contextId = contextId;
        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 contextId;
        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}'`,
            });
            contextId = project.wsgId;
        }
        else
            contextId = iModelData.projectId;
        if (iModelData.useName) {
            const imodelClient = new imodelhub_client_1.IModelHubClient();
            const imodels = await imodelClient.iModels.get(requestContext, contextId, 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 ${contextId}.`);
            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(contextId, imodelId, iModelData.changeSetId);
    }
    async getConnection() {
        return undefined === this._iModel ? this.open() : this._iModel;
    }
    async open() {
        try {
            // eslint-disable-next-line deprecation/deprecation
            const env = bentleyjs_core_1.Config.App.get("imjs_buddi_resolve_url_using_region");
            // eslint-disable-next-line no-console
            console.log(`Environment: ${env}`);
            this._iModel = await imodeljs_frontend_1.CheckpointConnection.openRemote(this.contextId, 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