/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Tiles
 */
import { assert } from "@itwin/core-bentley";
import { Feature, FeatureTable } from "@itwin/core-common";
import { IModelApp } from "../../IModelApp";
import { MapTile, RealityTileLoader, TileLoadPriority } from "../internal";
/** Specialization of map tile loader that includes terrain geometry with map imagery draped on it.
 * @internal
 */
export class MapTileLoader extends RealityTileLoader {
    get priority() { return TileLoadPriority.Terrain; }
    get clipLowResolutionTiles() { return true; }
    get isContentUnbounded() { return true; }
    isTileAvailable(quadId) { return this.terrainProvider.isTileAvailable(quadId); }
    constructor(_iModel, _modelId, _groundBias, _terrainProvider) {
        super();
        this._iModel = _iModel;
        this._modelId = _modelId;
        this._groundBias = _groundBias;
        this._terrainProvider = _terrainProvider;
        this._applyLights = false;
        this.featureTable = new FeatureTable(0xffff, this._modelId);
        this.featureTable.insert(new Feature(this._modelId));
    }
    getFeatureIndex(layerModelId) {
        return this.featureTable.insert(new Feature(layerModelId));
    }
    get maxDepth() { return this._terrainProvider.maxDepth; }
    get minDepth() { return 0; }
    get terrainProvider() { return this._terrainProvider; }
    getRequestChannel(_tile) {
        // ###TODO use hostname from url - but so many layers to go through to get that...
        return IModelApp.tileAdmin.channels.getForHttp("itwinjs-imagery");
    }
    async requestTileContent(tile, isCanceled) {
        assert(tile instanceof MapTile);
        try {
            const data = await this.terrainProvider.requestMeshData({ tile, isCanceled });
            return undefined !== data ? { data } : undefined;
        }
        catch (_) {
            return undefined;
        }
    }
    forceTileLoad(tile) {
        return this._terrainProvider.forceTileLoad(tile);
    }
    async loadTileContent(tile, data, system, isCanceled) {
        assert("data" in data);
        isCanceled = isCanceled ?? (() => !tile.isLoading);
        const mesh = await this._terrainProvider.readMesh({ data: data.data, isCanceled, tile });
        if (!mesh || isCanceled())
            return {};
        const projection = tile.getProjection(tile.heightRange);
        const terrainGeometry = system.createTerrainMesh(mesh, projection.transformFromLocal, true);
        return {
            contentRange: projection.transformFromLocal.multiplyRange(projection.localRange),
            terrain: { mesh, renderGeometry: terrainGeometry },
        };
    }
    loadPolyfaces() {
        assert(false, "load polyFaces not implmented for map tiles");
    }
    getChildHeightRange(quadId, rectangle, parent) {
        return this._terrainProvider.getChildHeightRange(quadId, rectangle, parent);
    }
    async loadChildren(_tile) {
        assert(false); // children are generated synchronously in MapTile....
        return undefined;
    }
}
//# sourceMappingURL=MapTileLoader.js.map