/*---------------------------------------------------------------------------------------------
* 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 { compareStrings, Guid } from "@itwin/core-bentley";
import { Tile, TileLoadPriority, TileTree, TileTreeReference } from "./internal";
import { Range3d, Transform } from "@itwin/core-geometry";
import { IModelApp } from "../IModelApp";
class GraphicTile extends Tile {
    constructor(tree, graphic) {
        const range = new Range3d();
        graphic.unionRange(range);
        super({
            contentId: tree.id,
            range,
            maximumSize: 1,
        }, tree);
        this.setGraphic(graphic);
    }
    _loadChildren(resolve) {
        resolve(undefined);
    }
    async requestContent() {
        return Promise.resolve(this.tree.id);
    }
    async readContent() {
        return {};
    }
    get channel() {
        return IModelApp.tileAdmin.channels.getForHttp("render-graphic-tile");
    }
}
class GraphicTree extends TileTree {
    constructor(args) {
        super({
            iModel: args.iModel,
            id: args.id,
            modelId: args.modelId,
            location: Transform.createIdentity(),
            priority: TileLoadPriority.Primary,
        });
        this._rootTile = new GraphicTile(this, args.graphic);
        this._viewFlagOverrides = args.viewFlags ?? {};
    }
    get rootTile() { return this._rootTile; }
    // ###TODO rm TileTree.is3d - only iModel tiles care about it.
    get is3d() { return true; }
    get maxDepth() { return undefined; }
    get viewFlagOverrides() { return this._viewFlagOverrides; }
    _selectTiles(args) {
        args.markUsed(this.rootTile);
        return [this.rootTile];
    }
    prune() { }
    draw(args) {
        const tiles = this.selectTiles(args);
        for (const tile of tiles)
            tile.drawGraphics(args);
        args.drawGraphics();
    }
}
class Supplier {
    compareTileTreeIds(lhs, rhs) {
        return compareStrings(lhs.id, rhs.id);
    }
    async createTileTree(id) {
        return Promise.resolve(new GraphicTree(id));
    }
}
const supplier = new Supplier();
class GraphicRef extends TileTreeReference {
    constructor(args) {
        super();
        this._owner = args.iModel.tiles.getTileTreeOwner(args, supplier);
        this._modelId = args.modelId;
        this._getToolTip = args.getToolTip;
    }
    get treeOwner() { return this._owner; }
    canSupplyToolTip(hit) {
        return undefined !== this._getToolTip && this._modelId === hit.modelId;
    }
    async getToolTip(hit) {
        if (this._getToolTip && this._modelId === hit.modelId) {
            return this._getToolTip(hit);
        }
        return undefined;
    }
}
/** @internal */
export function tileTreeReferenceFromRenderGraphic(args) {
    return new GraphicRef({ ...args, id: Guid.createValue() });
}
//# sourceMappingURL=RenderGraphicTileTree.js.map