"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module ModelState
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SectionDrawingModelState = exports.DrawingModelState = exports.SpatialLocationModelState = exports.PhysicalModelState = exports.SpatialModelState = exports.SheetModelState = exports.GeometricModel3dState = exports.GeometricModel2dState = exports.GeometricModelState = exports.ModelState = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const core_geometry_1 = require("@itwin/core-geometry");
const EntityState_1 = require("./EntityState");
const RealityDataSource_1 = require("./RealityDataSource");
const internal_1 = require("./tile/internal");
const SpatialClassifiersState_1 = require("./SpatialClassifiersState");
/** Represents the front-end state of a [Model]($backend).
 * @public
 * @extensions
 */
class ModelState extends EntityState_1.EntityState {
    static get className() { return "Model"; }
    constructor(props, iModel, state) {
        super(props, iModel, state);
        this.modeledElement = core_common_1.RelatedElement.fromJSON(props.modeledElement);
        this.name = props.name ? props.name : "";
        this.parentModel = core_bentley_1.Id64.fromJSON(props.parentModel); // NB! Must always match the model of the modeledElement!
        this.isPrivate = core_bentley_1.JsonUtils.asBool(props.isPrivate);
        this.isTemplate = core_bentley_1.JsonUtils.asBool(props.isTemplate);
    }
    /** Add all custom-handled properties of a Model to a json object. */
    toJSON() {
        const val = super.toJSON();
        val.modeledElement = this.modeledElement;
        val.parentModel = this.parentModel;
        val.name = this.name;
        if (this.isPrivate)
            val.isPrivate = this.isPrivate;
        if (this.isTemplate)
            val.isTemplate = this.isTemplate;
        return val;
    }
    /** Determine whether this is a GeometricModel */
    get isGeometricModel() { return false; }
    /** Attempts to cast this model to a geometric model. */
    get asGeometricModel() { return undefined; }
    /** Attempts to cast this model to a 3d geometric model. */
    get asGeometricModel3d() { return undefined; }
    /** Attempts to cast this model to a 2d geometric model. */
    get asGeometricModel2d() { return undefined; }
    /** Attempts to cast this model to a spatial model. */
    get asSpatialModel() { return undefined; }
    /**
     * Return the tool tip for this model. This is called only if the hit does not return a tooltip.
     * @internal
     */
    getToolTip(_hit) { return undefined; }
}
exports.ModelState = ModelState;
/** Represents the front-end state of a [GeometricModel]($backend).
 * The contents of a GeometricModelState can be rendered inside a [[Viewport]].
 * @public
 * @extensions
 */
class GeometricModelState extends ModelState {
    static get className() { return "GeometricModel"; }
    constructor(props, iModel, state) {
        super(props, iModel, state);
        this.geometryGuid = props.geometryGuid;
    }
    get asGeometricModel() { return this; }
    /** Returns true if this is a 2d model (a [[GeometricModel2dState]]). */
    get is2d() { return !this.is3d; }
    get isGeometricModel() { return true; }
    /** @internal */
    get treeModelId() { return this.id; }
    /** Query for the union of the ranges of all the elements in this GeometricModel.
     * @note This value is cached after the first time it is called.
     * @public
     */
    async queryModelRange() {
        if (undefined === this._modelRange) {
            const ranges = await this.iModel.models.queryExtents(this.id);
            this._modelRange = core_geometry_1.Range3d.fromJSON(ranges[0]?.extents);
        }
        return this._modelRange;
    }
    /** @internal */
    createTileTreeReference(view) {
        // If this is a reality model, its tile tree is obtained from reality data service URL.
        const spatialModel = this.asSpatialModel;
        const rdSourceKey = this.jsonProperties.rdSourceKey;
        const getDisplaySettings = () => view.displayStyle.settings.getRealityModelDisplaySettings(this.id) ?? core_common_1.RealityModelDisplaySettings.defaults;
        if (rdSourceKey) {
            const useOrbitGtTileTreeReference = rdSourceKey.format === core_common_1.RealityDataFormat.OPC;
            const treeRef = (!useOrbitGtTileTreeReference) ?
                (0, internal_1.createRealityTileTreeReference)({
                    rdSourceKey,
                    iModel: this.iModel,
                    source: view,
                    modelId: this.id,
                    // url: tilesetUrl, // If rdSourceKey is defined, url is not used
                    classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
                    getDisplaySettings,
                }) :
                (0, internal_1.createOrbitGtTileTreeReference)({
                    rdSourceKey,
                    iModel: this.iModel,
                    source: view,
                    modelId: this.id,
                    // orbitGtBlob: props.orbitGtBlob!, // If rdSourceKey is defined, orbitGtBlob is not used
                    classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
                    getDisplaySettings,
                });
            return treeRef;
        }
        const orbitGtBlob = this.jsonProperties.orbitGtBlob;
        // If this is an OrbitGt reality model, create it's reference
        if (orbitGtBlob) {
            let orbitGtName = "";
            if (orbitGtBlob.blobFileName !== "") {
                if (orbitGtBlob.blobFileName[0] === "/")
                    orbitGtName = orbitGtBlob.blobFileName.substring(1);
                else
                    orbitGtName = orbitGtBlob.blobFileName;
            }
            // Create rdSourceKey if not provided
            const rdSourceKeyOGT = RealityDataSource_1.RealityDataSource.createKeyFromOrbitGtBlobProps(orbitGtBlob);
            return (0, internal_1.createOrbitGtTileTreeReference)({
                rdSourceKey: rdSourceKeyOGT,
                iModel: this.iModel,
                source: view,
                modelId: this.id,
                orbitGtBlob,
                name: orbitGtName,
                classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
                getDisplaySettings,
            });
        }
        // If this is a TileTree reality model, create it's reference
        const tilesetUrl = this.jsonProperties.tilesetUrl;
        if (tilesetUrl) {
            const rdSourceKeyCS = RealityDataSource_1.RealityDataSource.createKeyFromUrl(tilesetUrl);
            return (0, internal_1.createRealityTileTreeReference)({
                rdSourceKey: rdSourceKeyCS,
                url: tilesetUrl,
                iModel: this.iModel,
                source: view,
                modelId: this.id,
                tilesetToDbTransform: this.jsonProperties.tilesetToDbTransform,
                classifiers: undefined !== spatialModel ? spatialModel.classifiers : undefined,
                getDisplaySettings,
            });
        }
        return (0, internal_1.createPrimaryTileTreeReference)(view, this);
    }
}
exports.GeometricModelState = GeometricModelState;
/** Represents the front-end state of a [GeometricModel2d]($backend).
 * @public
 * @extensions
 */
class GeometricModel2dState extends GeometricModelState {
    static get className() { return "GeometricModel2d"; }
    constructor(props, iModel, state) {
        super(props, iModel, state);
        this.globalOrigin = core_geometry_1.Point2d.fromJSON(props.globalOrigin);
    }
    get is3d() { return false; }
    get asGeometricModel2d() { return this; }
    toJSON() {
        const val = super.toJSON();
        val.globalOrigin = this.globalOrigin;
        return val;
    }
}
exports.GeometricModel2dState = GeometricModel2dState;
/** Represents the front-end state of a [GeometricModel3d]($backend).
 * @public
 * @extensions
 */
class GeometricModel3dState extends GeometricModelState {
    static get className() { return "GeometricModel3d"; }
    constructor(props, iModel, state) {
        super(props, iModel, state);
        this.isNotSpatiallyLocated = core_bentley_1.JsonUtils.asBool(props.isNotSpatiallyLocated);
        this.isPlanProjection = core_bentley_1.JsonUtils.asBool(props.isPlanProjection);
    }
    toJSON() {
        const val = super.toJSON();
        if (this.isNotSpatiallyLocated)
            val.isNotSpatiallyLocated = true;
        if (this.isPlanProjection)
            val.isPlanProjection = true;
        return val;
    }
    get is3d() { return true; }
    get asGeometricModel3d() { return this; }
    /** If true, then the elements in this GeometricModel3dState are in real-world coordinates and will be in the spatial index. */
    get isSpatiallyLocated() { return !this.isNotSpatiallyLocated; }
}
exports.GeometricModel3dState = GeometricModel3dState;
/** Represents the front-end state of a [SheetModel]($backend).
 * @public
 * @extensions
 */
class SheetModelState extends GeometricModel2dState {
    static get className() { return "SheetModel"; }
}
exports.SheetModelState = SheetModelState;
/** Represents the front-end state of a [SpatialModel]($backend).
 * @public
 * @extensions
 */
class SpatialModelState extends GeometricModel3dState {
    static get className() { return "SpatialModel"; }
    get asSpatialModel() { return this; }
    constructor(props, iModel, state) {
        super(props, iModel, state);
        if (this.isRealityModel)
            this.classifiers = SpatialClassifiersState_1.SpatialClassifiersState.create(this.jsonProperties);
    }
    /** Return true if this is a reality model (represented by a 3d tile set). */
    get isRealityModel() {
        return !!this.jsonProperties.tilesetUrl || !!this.jsonProperties.rdSourceKey || !!this.jsonProperties.orbitGtBlob;
    }
}
exports.SpatialModelState = SpatialModelState;
/** Represents the front-end state of a [PhysicalModel]($backend).
 * @public
 * @extensions
 */
class PhysicalModelState extends SpatialModelState {
    static get className() { return "PhysicalModel"; }
}
exports.PhysicalModelState = PhysicalModelState;
/** Represents the front-end state of a [SpatialLocationModel]($backend).
 * @public
 * @extensions
 */
class SpatialLocationModelState extends SpatialModelState {
    static get className() { return "SpatialLocationModel"; }
}
exports.SpatialLocationModelState = SpatialLocationModelState;
/** Represents the front-end state of a [DrawingModel]($backend).
 * @public
 * @extensions
 */
class DrawingModelState extends GeometricModel2dState {
    static get className() { return "DrawingModel"; }
}
exports.DrawingModelState = DrawingModelState;
/** Represents the front-end state of a [SectionDrawingModel]($backend).
 * @public
 * @extensions
 */
class SectionDrawingModelState extends DrawingModelState {
    static get className() { return "SectionDrawingModel"; }
}
exports.SectionDrawingModelState = SectionDrawingModelState;
//# sourceMappingURL=ModelState.js.map