"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewStateHydrator = 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 core_bentley_1 = require("@itwin/core-bentley");
const BackendLoggerCategory_1 = require("./BackendLoggerCategory");
/** @internal */
class ViewStateHydrator {
    _imodel;
    constructor(iModel) {
        this._imodel = iModel;
    }
    async getHydrateResponseProps(options) {
        const response = {};
        const promises = [];
        if (options.acsId)
            promises.push(this.handleAcsId(response, options.acsId));
        if (options.sheetViewAttachmentIds)
            promises.push(this.handleSheetViewAttachmentIds(response, options.sheetViewAttachmentIds, options.viewStateLoadProps));
        if (options.spatialViewId)
            promises.push(this.handleSpatialViewId(response, options.spatialViewId, options.viewStateLoadProps));
        if (options.notLoadedModelSelectorStateModels)
            promises.push(this.handleModelSelectorStateModels(response, options.notLoadedModelSelectorStateModels));
        if (options.baseModelId)
            promises.push(this.handleBaseModelId(response, options.baseModelId));
        await Promise.all(promises);
        return response;
    }
    async handleBaseModelId(response, baseModelId) {
        let modelProps;
        try {
            modelProps = this._imodel.models.getModelProps(baseModelId);
        }
        catch (err) {
            core_bentley_1.Logger.logError(BackendLoggerCategory_1.BackendLoggerCategory.ViewStateHydrator, `Error getting modelProps for baseModelId: ${baseModelId}`, () => ({ error: core_bentley_1.BentleyError.getErrorProps(err) }));
        }
        response.baseModelProps = modelProps;
    }
    async handleModelSelectorStateModels(response, models) {
        const decompressedModelIds = core_bentley_1.CompressedId64Set.decompressSet(models);
        const modelJsonArray = [];
        for (const id of decompressedModelIds) {
            try {
                const modelProps = this._imodel.models.getModelProps(id);
                modelJsonArray.push(modelProps);
            }
            catch { }
        }
        response.modelSelectorStateModels = modelJsonArray;
    }
    async handleSpatialViewId(response, spatialViewId, viewStateLoadProps) {
        response.spatialViewProps = await this._imodel.views.getViewStateProps(spatialViewId, viewStateLoadProps);
    }
    async handleAcsId(response, acsId) {
        try {
            const props = this._imodel.elements.getElementProps(acsId);
            response.acsElementProps = props;
        }
        catch { }
    }
    async handleSheetViewAttachmentIds(response, sheetViewAttachmentIds, viewStateLoadProps) {
        const decompressedIds = core_bentley_1.CompressedId64Set.decompressSet(sheetViewAttachmentIds);
        const attachmentProps = [];
        for (const id of decompressedIds) {
            try {
                attachmentProps.push(this._imodel.elements.getElementProps({ id }));
            }
            catch { }
        }
        const promises = [];
        for (const attachment of attachmentProps) {
            const loadView = async () => {
                try {
                    const view = await this._imodel.views.getViewStateProps(attachment.view.id, viewStateLoadProps);
                    return view;
                }
                catch {
                    return undefined;
                }
            };
            promises.push(loadView());
        }
        const views = await Promise.all(promises);
        response.sheetViewViews = views;
        response.sheetViewAttachmentProps = attachmentProps;
        return;
    }
}
exports.ViewStateHydrator = ViewStateHydrator;
//# sourceMappingURL=ViewStateHydrator.js.map