{"version":3,"sources":["../src/index.ts","../src/getMfeConfig.ts"],"sourcesContent":["export {\n  getMfeConfig,\n  type MfeConfigs,\n  type DefaultMfeConfig,\n} from \"./getMfeConfig\"\n","import { merge } from \"merge-anything\"\nimport type { ValidConfigForOrganizationsInCommerceLayer } from \"./schema/types\"\n\n/**\n * Represents a type that can be null or undefined, making it optional in use.\n * @template T The type that is being made nullable.\n */\ntype NullableType<T> = T | null | undefined\n\n/**\n * Params used by the getConfig function\n */\ninterface ConfigParams {\n  /**\n   * Language code (e.g., 'en', 'fr') used to dynamically replace the `:lang` placeholder in URLs.\n   */\n  lang?: NullableType<string>\n  /**\n   * Organization slug used to replace the `:slug` placeholder in URLs.\n   */\n  slug?: NullableType<string>\n  /**\n   * Access token string used to replace the `:access_token` placeholder in URLs.\n   */\n  accessToken?: NullableType<string>\n  /**\n   * Unique identifier for an order used to replace the `:order_id` placeholder in URLs.\n   */\n  orderId?: NullableType<string>\n  /**\n   * Order token string used to replace the `:token` placeholder in URLs.\n   */\n  token?: NullableType<string>\n  /**\n   * Unique identifier for an SKU list used to replace the `:sku_list_id` placeholder in URLs.\n   */\n  skuListId?: NullableType<string>\n  /**\n   * Unique identifier for an SKU used to replace the `:sku_id` placeholder in URLs.\n   */\n  skuId?: NullableType<string>\n}\n\ninterface GetMfeConfigProps {\n  /**\n   * `config` attribute of the organization\n   */\n  jsonConfig?: { mfe?: MfeConfigs }\n  /**\n   *  Market identifier for fetching specific configuration overrides. (`market:id:hashid`)\n   */\n  market?: string\n  /**\n   * Parameters for replacing URL placeholders.\n   */\n  params?: ConfigParams\n}\n\nexport type MfeConfigs = NonNullable<\n  ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]\n>\n\nexport type DefaultMfeConfig = NonNullable<\n  NonNullable<ValidConfigForOrganizationsInCommerceLayer[\"mfe\"]>[\"default\"]\n>\n\n/**\n * Retrieves and merges the default organization configuration with market-specific overrides based on the provided market identifier.\n * Placeholder values in the configuration URLs can be replaced with actual values from the `params`.\n *\n * @param jsonConfig The complete configuration object of the organization.\n * @param market The market identifier used to get market-specific configuration overrides.\n * @param params The object containing replacement values for URL placeholders, such as language and access token.\n * @returns The merged configuration object for the specified market, or null if no configuration is found.\n */\nexport function getMfeConfig({\n  jsonConfig,\n  market,\n  params,\n}: GetMfeConfigProps): DefaultMfeConfig | null {\n  if (jsonConfig?.mfe == null) {\n    return null\n  }\n\n  const defaultConfig = jsonConfig?.mfe?.default ?? {}\n  const overrideConfig = market != null ? (jsonConfig?.mfe[market] ?? {}) : {}\n\n  // Replace placeholders in all string values within the object\n  function replacePlaceholders(config: DefaultMfeConfig): DefaultMfeConfig {\n    const replacedConfig = JSON.stringify(config)\n      .replace(/:lang/g, params?.lang ?? \":lang\")\n      .replace(/:slug/g, params?.slug ?? \":slug\")\n      .replace(/:token/g, params?.token ?? \":token\")\n      .replace(/:access_token/g, params?.accessToken ?? \":access_token\")\n      .replace(/:order_id/g, params?.orderId ?? \":order_id\")\n      .replace(/:sku_list_id/g, params?.skuListId ?? \":sku_list_id\")\n      .replace(/:sku_id/g, params?.skuId ?? \":sku_id\")\n\n    return JSON.parse(replacedConfig)\n  }\n\n  const mergedConfig: DefaultMfeConfig = merge(\n    JSON.parse(JSON.stringify(defaultConfig)),\n    overrideConfig,\n  )\n  return replacePlaceholders(mergedConfig)\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAsB,0BA2Ef,SAASC,EAAa,CAC3B,WAAAC,EACA,OAAAC,EACA,OAAAC,CACF,EAA+C,CA/E/C,IAAAC,EAAAC,EAAAC,EAgFE,IAAIL,GAAA,YAAAA,EAAY,MAAO,KACrB,OAAO,KAGT,IAAMM,GAAgBF,GAAAD,EAAAH,GAAA,YAAAA,EAAY,MAAZ,YAAAG,EAAiB,UAAjB,KAAAC,EAA4B,CAAC,EAC7CG,EAAiBN,GAAU,MAAQI,EAAAL,GAAA,YAAAA,EAAY,IAAIC,KAAhB,KAAAI,EAA2B,CAAC,EAAK,CAAC,EAG3E,SAASG,EAAoBC,EAA4C,CAxF3E,IAAAN,EAAAC,EAAAC,EAAAK,EAAAC,EAAAC,EAAAC,EAyFI,IAAMC,EAAiB,KAAK,UAAUL,CAAM,EACzC,QAAQ,UAAUN,EAAAD,GAAA,YAAAA,EAAQ,OAAR,KAAAC,EAAgB,OAAO,EACzC,QAAQ,UAAUC,EAAAF,GAAA,YAAAA,EAAQ,OAAR,KAAAE,EAAgB,OAAO,EACzC,QAAQ,WAAWC,EAAAH,GAAA,YAAAA,EAAQ,QAAR,KAAAG,EAAiB,QAAQ,EAC5C,QAAQ,kBAAkBK,EAAAR,GAAA,YAAAA,EAAQ,cAAR,KAAAQ,EAAuB,eAAe,EAChE,QAAQ,cAAcC,EAAAT,GAAA,YAAAA,EAAQ,UAAR,KAAAS,EAAmB,WAAW,EACpD,QAAQ,iBAAiBC,EAAAV,GAAA,YAAAA,EAAQ,YAAR,KAAAU,EAAqB,cAAc,EAC5D,QAAQ,YAAYC,EAAAX,GAAA,YAAAA,EAAQ,QAAR,KAAAW,EAAiB,SAAS,EAEjD,OAAO,KAAK,MAAMC,CAAc,CAClC,CAEA,IAAMC,KAAiC,SACrC,KAAK,MAAM,KAAK,UAAUT,CAAa,CAAC,EACxCC,CACF,EACA,OAAOC,EAAoBO,CAAY,CACzC","names":["index_exports","__export","getMfeConfig","__toCommonJS","import_merge_anything","getMfeConfig","jsonConfig","market","params","_a","_b","_c","defaultConfig","overrideConfig","replacePlaceholders","config","_d","_e","_f","_g","replacedConfig","mergedConfig"]}