/*---------------------------------------------------------------------------------------------
* 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 { BentleyError, Logger } from "@itwin/core-bentley";
import { RealityDataFormat, RealityDataProvider } from "@itwin/core-common";
import { FrontendLoggerCategory } from "./common/FrontendLoggerCategory";
import { CesiumIonAssetProvider, ContextShareProvider, getCesiumAssetUrl } from "./tile/internal";
import { RealityDataSourceTilesetUrlImpl } from "./RealityDataSourceTilesetUrlImpl";
import { RealityDataSourceContextShareImpl } from "./RealityDataSourceContextShareImpl";
import { RealityDataSourceCesiumIonAssetImpl } from "./RealityDataSourceCesiumIonAssetImpl";
import { IModelApp } from "./IModelApp";
const loggerCategory = FrontendLoggerCategory.RealityData;
/**
 * Reality Data Operation error
 * @alpha
 */
export class RealityDataError extends BentleyError {
    constructor(errorNumber, message, getMetaData) {
        super(errorNumber, message, getMetaData);
    }
}
/** Utility functions for RealityDataSource
 * @beta
 */
export 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 : RealityDataFormat.fromUrl(tilesetUrl);
        if (CesiumIonAssetProvider.isProviderUrl(tilesetUrl)) {
            const provider = RealityDataProvider.CesiumIonAsset;
            let cesiumIonAssetKey = { provider, format, id: CesiumIonAssetProvider.osmBuildingId }; // default OSM building
            // Parse URL to extract possible asset id and key if provided
            const cesiumAsset = 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 (ContextShareProvider.isProviderUrl(tilesetUrl)) {
            const info = 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 : 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 = 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 : 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 : 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 !== 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 = getCesiumAssetUrl(osmAssetId, requestKey);
        return { provider: RealityDataProvider.CesiumIonAsset, format: 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.realityDataSourceProviders.find(key.provider);
        if (!provider) {
            Logger.logWarning(loggerCategory, `RealityDataSourceProvider "${key.provider}" is not registered`);
            return undefined;
        }
        return provider.createRealityDataSource(key, iTwinId);
    }
    RealityDataSource.fromKey = fromKey;
})(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 [[IModelAp.startup]].
 * @alpha
 */
export class RealityDataSourceProviderRegistry {
    /** @internal */
    constructor() {
        this._providers = new Map();
        this.register(RealityDataProvider.CesiumIonAsset, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceCesiumIonAssetImpl.createFromKey(key, iTwinId),
        });
        this.register(RealityDataProvider.TilesetUrl, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceTilesetUrlImpl.createFromKey(key, iTwinId),
        });
        this.register(RealityDataProvider.ContextShare, {
            createRealityDataSource: async (key, iTwinId) => RealityDataSourceContextShareImpl.createFromKey(key, iTwinId),
        });
        this.register(RealityDataProvider.OrbitGtBlob, {
            // ###TODO separate TilesetUrlImpl
            createRealityDataSource: async (key, iTwinId) => 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);
    }
}
//# sourceMappingURL=RealityDataSource.js.map