Source: utils/universal/ogc/generateLayerId.js

import { generateShortId } from "../core/generateShortId.js";

/**
 * Helper function to create the ID for the catalog
 * 
 * @param {string} serviceType 
 * @param {string} url 
 * @param {string} layerName 
 */
export async function generateLayerId(serviceType, url, layerName) {
    url = url.toLowerCase().split("?")[0];
    if (url.endsWith("/")) url = url.slice(0, -1);
    serviceType = serviceType.toLowerCase();
    layerName = layerName.toLowerCase();

    // Truncate the default path used by the geoserver, because all service types
    // can be requested from all URLs. Having multiple endpoint URLs
    // creates the issue of generating different IDs for the same layer
    const truncatedPaths = ["ows", "wms", "wfs", "wcs"];
    for (let i = 0; i < truncatedPaths.length; ++i) {
        if (url.endsWith(truncatedPaths[i])) {
            url = url.slice(0, -3);
            break;
        }
    }

    return await generateShortId(serviceType + url + layerName);
}