"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 core_frontend_1 = require("@itwin/core-frontend");
const imodels_client_management_1 = require("@itwin/imodels-client-management");
const itwins_client_1 = require("@itwin/itwins-client");
const imodels_access_frontend_1 = require("@itwin/imodels-access-frontend");
class IModelSession {
    constructor(iModelId, iTwinId, changesetId) {
        this.iTwinId = iTwinId;
        this.iModelId = iModelId;
        this.changesetId = changesetId;
    }
    static async create(accessToken, iModelData) {
        let iTwinId;
        let imodelId;
        // Turn the iTwin name into an id
        if (iModelData.useITwinName && iModelData.iTwinName) {
            const client = new itwins_client_1.ITwinsAccessClient();
            const iTwinListResponse = await client.queryAsync(accessToken, itwins_client_1.ITwinSubClass.Project, {
                displayName: iModelData.iTwinName,
            });
            const iTwinList = iTwinListResponse.data;
            if (!iTwinList) {
                throw new Error(`ITwin ${iModelData.iTwinName} returned with no data when queried.`);
            }
            if (iTwinList.length === 0)
                throw new Error(`ITwin ${iModelData.iTwinName} was not found for the user.`);
            else if (iTwinList.length > 1)
                throw new Error(`Multiple iTwins named ${iModelData.iTwinName} were found for the user.`);
            iTwinId = iTwinList[0].id ?? iModelData.iTwinId;
        }
        else
            iTwinId = iModelData.iTwinId;
        if (iModelData.useName) {
            const imodelClient = new imodels_client_management_1.IModelsClient({ api: { baseUrl: `https://${process.env.IMJS_URL_PREFIX ?? ""}api.bentley.com/imodels` } });
            const iModels = imodelClient.iModels.getRepresentationList({
                authorization: imodels_access_frontend_1.AccessTokenAdapter.toAuthorizationCallback(accessToken),
                urlParams: {
                    iTwinId,
                    name: iModelData.name,
                },
            });
            for await (const iModel of iModels) {
                imodelId = iModel.id;
                break;
            }
            if (!imodelId)
                throw new Error(`The iModel ${iModelData.name} does not exist in iTwin ${iTwinId}.`);
        }
        else
            imodelId = iModelData.id;
        console.log(`Using iModel { name:${iModelData.name}, id:${imodelId}, iTwinId:${iTwinId}, changesetId:${iModelData.changesetId} }`); // eslint-disable-line no-console
        return new IModelSession(imodelId, iTwinId, iModelData.changesetId);
    }
    async getConnection() {
        return undefined === this._iModel ? this.open() : this._iModel;
    }
    async open() {
        try {
            // eslint-disable-next-line no-console
            console.log(`Environment: ${process.env.IMJS_URL_PREFIX}`);
            this._iModel = await core_frontend_1.CheckpointConnection.openRemote(this.iTwinId, this.iModelId);
            (0, 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