/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { Guid } from "@itwin/core-bentley";
import { RealityDataFormat, RealityDataProvider } from "@itwin/core-common";
/**
 * This class provide methods used to interpret url to Project Wise Context Share (RealityDataProvider.ContextShare)
 */
export class ContextShareProvider {
    /** Return true if this is a supported url to this service provider */
    static isProviderUrl(tilesetUrl) {
        // Try to extract realityDataId from URL and if not possible, use the url as the key
        let attUrl;
        try {
            attUrl = new URL(tilesetUrl);
        }
        catch {
            // Not a valid URL for Context share
            return false;
        }
        // If api.bentley.com/realitydata or api.bentley.com/reality-management is used, it is context share
        if (tilesetUrl.toLowerCase().includes("api.bentley.com/realitydata") || tilesetUrl.toLowerCase().includes("api.bentley.com/reality-management/reality-data"))
            return true;
        // detect if it is a RDS url
        const formattedUrl1 = attUrl.pathname.replace(/~2F/g, "/").replace(/\\/g, "/");
        if (formattedUrl1) {
            const urlParts1 = formattedUrl1.split("/").map((entry) => entry.replace(/%2D/g, "-"));
            let partOffset1 = 0;
            urlParts1.find((value, index) => {
                if (value === "Repositories") {
                    partOffset1 = index;
                    return true;
                }
                return false;
            });
            const isRDSUrl = (urlParts1[partOffset1] === "Repositories") && (urlParts1[partOffset1 + 1].match("S3MXECPlugin--*") !== null) && (urlParts1[partOffset1 + 2] === "S3MX");
            return isRDSUrl;
        }
        return false;
    }
    /** Return true if this is a supported url to this service provider */
    static getInfoFromUrl(tilesetUrl) {
        const invalidUrlInfo = { provider: RealityDataProvider.TilesetUrl, format: RealityDataFormat.ThreeDTile, id: tilesetUrl, iTwinId: undefined };
        let attUrl;
        try {
            attUrl = new URL(tilesetUrl);
        }
        catch {
            // Not a valid URL and not equal, probably $cesiumAsset
            return invalidUrlInfo;
        }
        // If api.bentley.com/realitydata or api.bentley.com/reality-management is used, it is context share
        if (tilesetUrl.toLowerCase().includes("api.bentley.com/realitydata") || tilesetUrl.toLowerCase().includes("api.bentley.com/reality-management/reality-data")) {
            const lcTilesetUrl = tilesetUrl.toLowerCase();
            // NOTICE: We assume it is a ThreeDTile BUT this could technically be a point cloud (OPC).
            // This method was used in typical workflow where format was always ThreeDTile and is here for legacy support.
            // We don't want to make a call to RDS to resolve format since this method must not be async (it is used in workflow that are not async)
            const format = RealityDataFormat.ThreeDTile;
            let indexId = -1;
            let indexProjectId = -1;
            if (tilesetUrl.toLowerCase().includes("reality-management/reality-data")) {
                indexId = lcTilesetUrl.indexOf("reality-management/reality-data/") + 32; // length of "reality-management/reality-data/" = 32;
                indexProjectId = lcTilesetUrl.indexOf("itwinid=") + 8; // length of "itwinid=" = 8;
            }
            else if (tilesetUrl.toLowerCase().includes("realitydata")) {
                indexId = lcTilesetUrl.indexOf("realitydata/") + 12; // length of "realitydata/" = 12;
                indexProjectId = lcTilesetUrl.indexOf("projectid=") + 10; // length of "projectid=" = 10;
            }
            const id = lcTilesetUrl.substring(indexId, Guid.empty.length + indexId);
            let projectId;
            if (indexProjectId && indexProjectId > 0)
                projectId = lcTilesetUrl.substring(indexProjectId, Guid.empty.length + indexProjectId);
            const apimContextShareKey = { provider: RealityDataProvider.ContextShare, format, id, iTwinId: projectId };
            return apimContextShareKey;
        }
        // detect if it is a RDS url
        const formattedUrl1 = attUrl.pathname.replace(/~2F/g, "/").replace(/\\/g, "/");
        if (formattedUrl1) {
            const urlParts1 = formattedUrl1.split("/").map((entry) => entry.replace(/%2D/g, "-"));
            let partOffset1 = 0;
            urlParts1.find((value, index) => {
                if (value === "Repositories") {
                    partOffset1 = index;
                    return true;
                }
                return false;
            });
            const isOPC = attUrl.pathname.match(".opc*") !== null;
            const isRDSUrl = (urlParts1[partOffset1] === "Repositories") && (urlParts1[partOffset1 + 1].match("S3MXECPlugin--*") !== null) && (urlParts1[partOffset1 + 2] === "S3MX");
            let projectId;
            const projectIdSection = urlParts1.find((val) => val.includes("--"));
            if (projectIdSection)
                projectId = projectIdSection.split("--")[1];
            // Make sure the url to compare are REALITYMESH3DTILES url, otherwise, compare the url directly
            if (isRDSUrl || isOPC) {
                // Make sure the reality data id are the same
                const guid1 = urlParts1.find(Guid.isGuid);
                if (guid1 !== undefined) {
                    const provider = RealityDataProvider.ContextShare;
                    const format = isOPC ? RealityDataFormat.OPC : RealityDataFormat.ThreeDTile;
                    const contextShareKey = { provider, format, id: guid1, iTwinId: projectId };
                    return contextShareKey;
                }
            }
        }
        // Not a valid URL and not equal, probably $cesiumAsset
        return invalidUrlInfo;
    }
    static getInfoFromBlobUrl(blobUrl) {
        let format = RealityDataFormat.ThreeDTile;
        let provider = RealityDataProvider.TilesetUrl;
        const url = new URL(blobUrl);
        // If we cannot interpret that url pass in parameter we just fallback to old implementation
        if (!url.pathname)
            return { provider, format, id: blobUrl };
        // const accountName   = url.hostname.split(".")[0];
        let containerName = "";
        if (url.pathname) {
            const pathSplit = url.pathname.split("/");
            containerName = pathSplit[1];
        }
        // const blobFileName  = `/${pathSplit[2]}`;
        // const sasToken      = url.search.substring(1);
        const isOPC = url.pathname.match(".opc*") !== null;
        provider = RealityDataProvider.ContextShare;
        format = isOPC ? RealityDataFormat.OPC : RealityDataFormat.ThreeDTile;
        const contextShareKey = { provider, format, id: containerName };
        return contextShareKey;
    }
}
//# sourceMappingURL=ContextShareProvider.js.map