/**
 * URL construction methods for MenuUtils.
 * Helpers for building API and config URLs.
 */

const urlUtils = {
  getSpecificConfigUrl(nodeName, cloudAPI) {
    const cloudConfigURL = cloudAPI + "/config/" + nodeName + ".json";
    const localConfigURL = "http://localhost:1880/"+ nodeName + "/dependencies/"+ nodeName + "/" + nodeName + "Config.json";

    return { cloudConfigURL, localConfigURL };
  },

  // Helper function to construct a URL from a base and path internal
  constructUrl(base, ...paths) {
    // Remove trailing slash from base and leading slashes from paths
    const sanitizedBase = (base || "").replace(/\/+$/, "");
    const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));

    // Join sanitized base and paths
    const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
    console.log("Base:", sanitizedBase);
    console.log("Paths:", sanitizedPaths);
    console.log("Constructed URL:", url);
    return url;
  },

  //Adjust for API Gateway
  constructCloudURL(base, ...paths) {
    // Remove trailing slash from base and leading slashes from paths
    const sanitizedBase = base.replace(/\/+$/, "");
    const sanitizedPaths = paths.map((path) => path.replace(/^\/+|\/+$/g, ""));
    // Join sanitized base and paths
    const url = `${sanitizedBase}/${sanitizedPaths.join("/")}`;
    return url;
  },
};

module.exports = urlUtils;
