"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RealityDataSourceProviderRegistry = exports.RealityDataSource = exports.RealityDataError = void 0;
/*---------------------------------------------------------------------------------------------
* 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 IModelApp_1 = require("./IModelApp");
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 [[IModelAp.startup]].
 * @alpha
 */
class RealityDataSourceProviderRegistry {
    /** @internal */
    constructor() {
        this._providers = new Map();
        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;
//# sourceMappingURL=RealityDataSource.js.map