"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Google3dTilesProvider = exports.RealityDataSourceProviderRegistry = exports.RealityDataSource = exports.RealityDataError = void 0;
exports.getGoogle3dTilesUrl = getGoogle3dTilesUrl;
/*---------------------------------------------------------------------------------------------
* 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
 */
const core_bentley_1 = require("@itwin/core-bentley");
const core_common_1 = require("@itwin/core-common");
const FrontendLoggerCategory_1 = require("./common/FrontendLoggerCategory");
const internal_1 = require("./tile/internal");
const RealityDataSourceTilesetUrlImpl_1 = require("./RealityDataSourceTilesetUrlImpl");
const RealityDataSourceContextShareImpl_1 = require("./RealityDataSourceContextShareImpl");
const RealityDataSourceCesiumIonAssetImpl_1 = require("./RealityDataSourceCesiumIonAssetImpl");
const RealityDataSourceGoogle3dTilesImpl_1 = require("./internal/RealityDataSourceGoogle3dTilesImpl");
const IModelApp_1 = require("./IModelApp");
const GoogleMapsDecorator_1 = require("./internal/GoogleMapsDecorator");
const loggerCategory = FrontendLoggerCategory_1.FrontendLoggerCategory.RealityData;
/**
 * Reality Data Operation error
 * @alpha
 */
class RealityDataError extends core_bentley_1.BentleyError {
    constructor(errorNumber, message, getMetaData) {
        super(errorNumber, message, getMetaData);
    }
}
exports.RealityDataError = RealityDataError;
/** Utility functions for RealityDataSource
 * @beta
 */
var RealityDataSource;
(function (RealityDataSource) {
    /** Create a RealityDataSourceKey from a tilesetUrl.
     * @param tilesetUrl the reality data attachment url
     * @param inputProvider identify the RealityDataProvider if known, otherwise function will try to extract it from the tilesetUrl
     * @param inputFormat identify the RealityDataFormat if known, otherwise function will try to extract it from the tilesetUrl
     * @returns the RealityDataSourceKey that uniquely identify a reality data for a provider
     */
    function createKeyFromUrl(tilesetUrl, inputProvider, inputFormat) {
        let format = inputFormat ? inputFormat : core_common_1.RealityDataFormat.fromUrl(tilesetUrl);
        if (internal_1.CesiumIonAssetProvider.isProviderUrl(tilesetUrl)) {
            const provider = core_common_1.RealityDataProvider.CesiumIonAsset;
            let cesiumIonAssetKey = { provider, format, id: internal_1.CesiumIonAssetProvider.osmBuildingId }; // default OSM building
            // Parse URL to extract possible asset id and key if provided
            const cesiumAsset = internal_1.CesiumIonAssetProvider.parseCesiumUrl(tilesetUrl);
            if (cesiumAsset) {
                cesiumIonAssetKey = RealityDataSource.createCesiumIonAssetKey(cesiumAsset.id, cesiumAsset.key);
            }
            return cesiumIonAssetKey;
        }
        // Try to extract realityDataId from URL and if not possible, use the url as the key
        if (internal_1.ContextShareProvider.isProviderUrl(tilesetUrl)) {
            const info = internal_1.ContextShareProvider.getInfoFromUrl(tilesetUrl);
            const provider = inputProvider ? inputProvider : info.provider;
            format = inputFormat ? inputFormat : info.format;
            const contextShareKey = { provider, format, id: info.id, iTwinId: info.iTwinId };
            return contextShareKey;
        }
        // default to tileSetUrl
        const provider2 = inputProvider ? inputProvider : core_common_1.RealityDataProvider.TilesetUrl;
        const urlKey = { provider: provider2, format, id: tilesetUrl };
        return urlKey;
    }
    RealityDataSource.createKeyFromUrl = createKeyFromUrl;
    /** @alpha - was used for a very specific case of point cloud (opc) attachment that should not be made public */
    function createKeyFromBlobUrl(blobUrl, inputProvider, inputFormat) {
        const info = internal_1.ContextShareProvider.getInfoFromBlobUrl(blobUrl);
        const format = inputFormat ? inputFormat : info.format;
        const provider = inputProvider ? inputProvider : info.provider;
        const contextShareKey = { provider, format, id: info.id };
        return contextShareKey;
    }
    RealityDataSource.createKeyFromBlobUrl = createKeyFromBlobUrl;
    /** @alpha - OrbitGtBlobProps is alpha */
    function createKeyFromOrbitGtBlobProps(orbitGtBlob, inputProvider, inputFormat) {
        const format = inputFormat ? inputFormat : core_common_1.RealityDataFormat.OPC;
        if (orbitGtBlob.blobFileName && orbitGtBlob.blobFileName.toLowerCase().startsWith("http")) {
            return RealityDataSource.createKeyFromBlobUrl(orbitGtBlob.blobFileName, inputProvider, format);
        }
        else if (orbitGtBlob.rdsUrl) {
            return RealityDataSource.createKeyFromUrl(orbitGtBlob.rdsUrl, inputProvider, format);
        }
        const provider = inputProvider ? inputProvider : core_common_1.RealityDataProvider.OrbitGtBlob;
        const id = `${orbitGtBlob.accountName}:${orbitGtBlob.containerName}:${orbitGtBlob.blobFileName}:?${orbitGtBlob.sasToken}`;
        return { provider, format, id };
    }
    RealityDataSource.createKeyFromOrbitGtBlobProps = createKeyFromOrbitGtBlobProps;
    /** @alpha - OrbitGtBlobProps is alpha */
    function createOrbitGtBlobPropsFromKey(rdSourceKey) {
        if (rdSourceKey.provider !== core_common_1.RealityDataProvider.OrbitGtBlob)
            return undefined;
        const splitIds = rdSourceKey.id.split(":");
        const sasTokenIndex = rdSourceKey.id.indexOf(":?");
        const sasToken = rdSourceKey.id.substring(sasTokenIndex + 2);
        const orbitGtBlob = {
            accountName: splitIds[0],
            containerName: splitIds[1],
            blobFileName: splitIds[2],
            sasToken,
        };
        return orbitGtBlob;
    }
    RealityDataSource.createOrbitGtBlobPropsFromKey = createOrbitGtBlobPropsFromKey;
    /** @internal - Is used by "fdt attach cesium asset" keyin*/
    function createCesiumIonAssetKey(osmAssetId, requestKey) {
        const id = (0, internal_1.getCesiumAssetUrl)(osmAssetId, requestKey);
        return { provider: core_common_1.RealityDataProvider.CesiumIonAsset, format: core_common_1.RealityDataFormat.ThreeDTile, id };
    }
    RealityDataSource.createCesiumIonAssetKey = createCesiumIonAssetKey;
    /** Return an instance of a RealityDataSource from a source key.
     * There will aways be only one reality data RealityDataSource for a corresponding reality data source key.
     * @alpha
     */
    async function fromKey(key, iTwinId) {
        const provider = IModelApp_1.IModelApp.realityDataSourceProviders.find(key.provider);
        if (!provider) {
            core_bentley_1.Logger.logWarning(loggerCategory, `RealityDataSourceProvider "${key.provider}" is not registered`);
            return undefined;
        }
        return provider.createRealityDataSource(key, iTwinId);
    }
    RealityDataSource.fromKey = fromKey;
})(RealityDataSource || (exports.RealityDataSource = RealityDataSource = {}));
/** A registry of [[RealityDataSourceProvider]]s identified by their unique names. The registry can be accessed via [[IModelApp.realityDataSourceProviders]].
 * It includes a handful of built-in providers for sources like Cesium ION, ContextShare, OrbitGT, and arbitrary public-accessible URLs.
 * Any number of additional providers can be registered. They should typically be registered just after [[IModelApp.startup]].
 * @beta
 */
class RealityDataSourceProviderRegistry {
    _providers = new Map();
    /** @internal */
    constructor() {
        this.register(core_common_1.RealityDataProvider.CesiumIonAsset, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceCesiumIonAssetImpl_1.RealityDataSourceCesiumIonAssetImpl.createFromKey(key, iTwinId),
        });
        this.register(core_common_1.RealityDataProvider.TilesetUrl, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceTilesetUrlImpl_1.RealityDataSourceTilesetUrlImpl.createFromKey(key, iTwinId),
        });
        this.register(core_common_1.RealityDataProvider.ContextShare, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceContextShareImpl_1.RealityDataSourceContextShareImpl.createFromKey(key, iTwinId),
        });
        this.register(core_common_1.RealityDataProvider.OrbitGtBlob, {
            // ###TODO separate TilesetUrlImpl
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceTilesetUrlImpl_1.RealityDataSourceTilesetUrlImpl.createFromKey(key, iTwinId),
        });
    }
    /** Register `provider` to produce [[RealityDataSource]]s for the specified provider `name`. */
    register(name, provider) {
        this._providers.set(name, provider);
    }
    /** Look up the provider registered by the specified `name`. */
    find(name) {
        return this._providers.get(name);
    }
}
exports.RealityDataSourceProviderRegistry = RealityDataSourceProviderRegistry;
/**
 * Will provide Google Photorealistic 3D Tiles (in 3dTile format).
 * A valid API key or getAuthToken fuction must be supplied when creating this provider.
 * To use this provider, you must register it with [[IModelApp.realityDataSourceProviders]].
 * Example usage:
 * ```ts
 * [[include:GooglePhotorealistic3dTiles_providerApiKey]]
 * ```
 * @see [Google Photorealistic 3D Tiles]($docs/learning/frontend/GooglePhotorealistic3dTiles.md)
 * @beta
 */
class Google3dTilesProvider {
    /** Google Map Tiles API Key used to access Google 3D Tiles. */
    _apiKey;
    /** Function that returns an OAuth token for authenticating with Google 3D Tiles. This token is expected to not contain the "Bearer" prefix. */
    _getAuthToken;
    /** Decorator for Google Maps logos. */
    _decorator;
    /** Enables cached decorations for this provider. @see [[ViewportDecorator.useCachedDecorations]] */
    useCachedDecorations = true;
    async createRealityDataSource(key, iTwinId) {
        if (!this._apiKey && !this._getAuthToken) {
            core_bentley_1.Logger.logError(loggerCategory, "Either an API key or getAuthToken function are required to create a Google3dTilesProvider.");
            return undefined;
        }
        return RealityDataSourceGoogle3dTilesImpl_1.RealityDataSourceGoogle3dTilesImpl.createFromKey(key, iTwinId, this._apiKey, this._getAuthToken);
    }
    constructor(options) {
        this._apiKey = options.apiKey;
        this._getAuthToken = options.getAuthToken;
        this._decorator = new GoogleMapsDecorator_1.GoogleMapsDecorator(options.showCreditsOnScreen ?? true);
    }
    /**
     * Initialize the Google 3D Tiles reality data source provider by activating its decorator, which consists of loading the correct Google Maps logo.
     * @returns `true` if the decorator was successfully activated, otherwise `false`.
     */
    async initialize() {
        const isActivated = await this._decorator.activate("satellite");
        if (!isActivated) {
            const msg = "Failed to activate decorator";
            core_bentley_1.Logger.logError(loggerCategory, msg);
            throw new core_bentley_1.BentleyError(core_bentley_1.BentleyStatus.ERROR, msg);
        }
        return isActivated;
    }
    decorate(_context) {
        this._decorator.decorate(_context);
    }
    async addAttributions(cards, vp) {
        const copyrightMap = (0, GoogleMapsDecorator_1.getCopyrights)(vp);
        // Only add another logo card if the tiles have copyright
        if (copyrightMap.size > 0) {
            // Order by most occurances to least
            // See https://developers.google.com/maps/documentation/tile/create-renderer#display-attributions
            const sortedCopyrights = [...copyrightMap.entries()].sort((a, b) => b[1] - a[1]);
            let copyrightMsg = "Data provided by:<br><ul>";
            copyrightMsg += sortedCopyrights.map(([key]) => `<li>${key}</li>`).join("");
            copyrightMsg += "</ul>";
            const iconSrc = document.createElement("img");
            iconSrc.src = `${IModelApp_1.IModelApp.publicPath}images/GoogleMaps_Logo_Gray.svg`;
            iconSrc.style.padding = "10px 10px 5px 10px";
            cards.appendChild(IModelApp_1.IModelApp.makeLogoCard({
                iconSrc,
                iconWidth: 98,
                heading: "Google Photorealistic 3D Tiles",
                notice: copyrightMsg
            }));
        }
    }
}
exports.Google3dTilesProvider = Google3dTilesProvider;
/** Returns the URL used for retrieving Google Photorealistic 3D Tiles.
 * @beta
 */
function getGoogle3dTilesUrl() {
    return "https://tile.googleapis.com/v1/3dtiles/root.json";
}
//# sourceMappingURL=RealityDataSource.js.map