"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var micro_frontend_exports = {};
__export(micro_frontend_exports, {
  getLogger: () => getLogger,
  getMicroFrontEndAppConfig: () => getMicroFrontEndAppConfig,
  isGuest: () => isGuest,
  isHost: () => isHost,
  isStandAloneGuest: () => isStandAloneGuest
});
module.exports = __toCommonJS(micro_frontend_exports);
var import_lodash = __toESM(require("lodash"));
var import_guest = require("./guest.js");
var import_host = require("./host.js");
var import_config = require("../app-config/config.js");
var import_helpers = require("../helpers.js");
var import_types2 = require("./types.js");
const isHost = () => import_host.CMicroAppHost.isInitialized();
const isGuest = () => import_guest.CMicroAppGuest.isInitialized();
const isStandAloneGuest = () => isGuest() && window.top === window.self;
const getLogger = () => isHost() ? import_host.CMicroAppHost.getInstance().getLogger() : import_guest.CMicroAppGuest.getInstance().getLogger();
const isProduction = (mode) => {
  if ((0, import_helpers.isCIBuild)()) return true;
  if (mode) return mode === import_types2.LaunchMode.PRODUCTION;
  return true;
};
const getVersionedPath = (path) => {
  const { systemVersion } = import_host.CMicroAppHost.getInstance().getProps();
  const majorMinorVersion = systemVersion.match(/^(?:\d+\.\d+)*/g);
  return path.replace(
    /{SYSTEM_VERSION}/,
    majorMinorVersion && majorMinorVersion[0] || "latest"
  );
};
const getMicroFrontEndAppConfig = (appInfo) => {
  const microFEAppConfig = (0, import_config.getAppConfigValue)(
    `microFrontendApps.${appInfo.id}`
  );
  if (!microFEAppConfig)
    throw new Error(
      `unable to locate application configuraiton for ${appInfo.id} under microFrontendApps section of app.config.json`
    );
  const { mode = import_types2.LaunchMode.PRODUCTION } = microFEAppConfig;
  const envConfig = isProduction(mode) ? microFEAppConfig.production : microFEAppConfig.development;
  if (microFEAppConfig.production) delete microFEAppConfig.production;
  if (microFEAppConfig.development) delete microFEAppConfig.development;
  const microFEConfig = import_lodash.default.merge(
    appInfo,
    {
      mode: "production",
      hostUrl: "./",
      manifestPath: "./{SYSTEM_VERSION}",
      isJsModule: false
    },
    microFEAppConfig,
    envConfig
  );
  microFEConfig.hostUrl = (0, import_helpers.convertRelativeToAbsoluteUrl)(
    microFEConfig.hostUrl.replace(/\/?$/, "/")
  );
  microFEConfig.manifestPath = getVersionedPath(microFEConfig.manifestPath);
  return microFEConfig;
};
