"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var manifest_exports = {};
__export(manifest_exports, {
  getAppManifest: () => getAppManifest,
  getFullFileNameofAssetsFromManifest: () => getFullFileNameofAssetsFromManifest
});
module.exports = __toCommonJS(manifest_exports);
var import_http_client = require("../../../communication/http-client/index.js");
var import_url = require("../../../utils/url.js");
var import_micro_frontend = require("../../../utils/micro-frontend/index.js");
var import_log_records = require("../../../utils/log-records.js");
const getUnVersionedManifestPath = (path) => path.replace(/\/\d+\.\d+/, "/latest");
const getAppManifest = async ({
  hostUrl,
  manifestPath
}) => {
  if (!hostUrl || !manifestPath)
    throw new Error(
      "Unable to get app manifest. hostUrl and manifestPath are required."
    );
  const url = new URL(
    `${manifestPath.replace(/\/?$/, "/")}manifest.json`,
    hostUrl
  );
  const getUnVersionedManifestJson = () => {
    const unVersionedManifestPath = getUnVersionedManifestPath(manifestPath);
    if (manifestPath !== unVersionedManifestPath) {
      return getAppManifest({
        hostUrl,
        manifestPath: getUnVersionedManifestPath(manifestPath)
      });
    }
    throw new Error("manifest.json is not available for the application");
  };
  try {
    const response = await (0, import_http_client.getHTTPClient)().get(
      (0, import_url.removeDoubleSlash)(url.href)
    );
    const { headers } = response;
    if (headers["content-type"]?.includes?.("application/json"))
      return response.data;
    return await getUnVersionedManifestJson();
  } catch (err) {
    return getUnVersionedManifestJson();
  }
};
const getFullFileNameofAssetsFromManifest = (manifest, assetNames = []) => assetNames.reduce((assets, assetName) => {
  const fullFileName = manifest[assetName];
  if (fullFileName) assets.push(fullFileName);
  else {
    const logRecord = import_log_records.logRecords.ASSET_NOT_FOUND_IN_MANIFEST(assetName);
    (0, import_micro_frontend.getLogger)().error(logRecord);
    throw new Error(logRecord.message);
  }
  return assets;
}, []);
