{"version":3,"file":"getPriceObjects.cjs","sources":["../../../../src/components/lib/getPriceObjects.ts"],"sourcesContent":["/*\nThis function getPriceObjects calculates the price details based on user location and promo offers.\n*/\n\nimport { formatNumberWithCommas } from \"./helpFunction\";\nimport { CURRENCY_SYMBOL } from \"./utils\";\n\ninterface PriceTier {\n  [currency: string]: string;\n}\n\ninterface ProductObject {\n  price_tier?: PriceTier;\n  [key: string]: any;\n}\n\ninterface LocationState {\n  ip_address?: string;\n  currency?: string;\n}\n\ninterface PromoState {\n  id?: string | number;\n  percentage?: string;\n  [key: string]: any;\n}\n\ninterface PriceObjectsResult {\n  currency: string;\n  price: string;\n  formatted_price?: number;\n  discounted_price?: string;\n  promo: PromoState;\n  symbol: string;\n  is_promo_running: boolean;\n  ip_address?: string;\n}\n\ninterface GetPriceObjectsProps {\n  selectCurrentPromo: () => PromoState;\n  selectUserLocation: () => LocationState;\n  useSelector: <T>(selector: () => T) => T;\n}\n\n/**\n * Calculates price details based on user location and promotional offers\n * @param objects - Product object containing pricing information\n * @param props - Functions for accessing Redux state\n * @returns Object with currency, price, formatted_price, symbol, and promo details\n */\nconst getPriceObjects = (\n  objects: ProductObject,\n  props: GetPriceObjectsProps\n): PriceObjectsResult => {\n  const { selectCurrentPromo, selectUserLocation, useSelector } = props;\n  const location = useSelector(selectUserLocation);\n  const promo = useSelector(selectCurrentPromo);\n\n  // Support dollars and Naira\n  let price: string = \"FREE\";\n  let formatted_price: number | undefined;\n  let discounted_price: string | undefined;\n  let symbol: string = \"\";\n  const ip_address = location?.ip_address;\n  let currency: string = location?.currency === \"NGN\" ? \"NGN\" : \"USD\";\n\n  // Temporarily accept Naira\n  currency = \"NGN\";\n\n  try {\n    // Get currency, price, symbol\n    if (objects.price_tier) {\n      symbol = CURRENCY_SYMBOL[currency as keyof typeof CURRENCY_SYMBOL];\n      price = symbol + objects.price_tier[currency];\n      formatted_price = parseFloat(\n        objects.price_tier[currency].replaceAll(\",\", \"\")\n      );\n\n      if (promo.percentage) {\n        const discounts = (formatted_price * parseInt(promo.percentage)) / 100;\n        formatted_price = Math.floor(formatted_price - discounts);\n        price = `${symbol}${formatNumberWithCommas(formatted_price)}`;\n        discounted_price = symbol + formatNumberWithCommas(discounts);\n      }\n    }\n  } catch (e) {\n    console.log(\"Couldn't getPriceObjects: \", e);\n  }\n\n  const is_promo_running = price !== \"FREE\" && Boolean(promo.id);\n\n  return {\n    currency,\n    price,\n    formatted_price,\n    discounted_price,\n    promo,\n    symbol,\n    is_promo_running,\n    ip_address,\n  };\n};\n\nexport default getPriceObjects;\n"],"names":["objects","props","formatted_price","discounted_price","selectCurrentPromo","selectUserLocation","useSelector","location","promo","price","symbol","ip_address","currency","price_tier","CURRENCY_SYMBOL","parseFloat","replaceAll","percentage","discounts","parseInt","Math","floor","concat","formatNumberWithCommas","e","console","log","is_promo_running","Boolean","id"],"mappings":"iJAkDwB,SACtBA,EACAC,GAEA,IAMIC,EACAC,EAPIC,EAAwDH,EAAxDG,mBAAoBC,EAAoCJ,EAApCI,mBAAoBC,EAAgBL,EAAhBK,YAC1CC,EAAWD,EAAYD,GACvBG,EAAQF,EAAYF,GAGtBK,EAAgB,OAGhBC,EAAiB,GACfC,EAAaJ,aAAAA,EAAAA,EAAUI,WACzBC,EAAmBL,SAAAA,EAAUK,SAGjCA,EAAW,MAEX,IAEE,GAAIZ,EAAQa,aAEVJ,GADAC,EAASI,EAAAA,gBAAgBF,IACRZ,EAAQa,WAAWD,GACpCV,EAAkBa,WAChBf,EAAQa,WAAWD,GAAUI,WAAW,IAAK,KAG3CR,EAAMS,YAAY,CACpB,IAAMC,EAAahB,EAAkBiB,SAASX,EAAMS,YAAe,IACnEf,EAAkBkB,KAAKC,MAAMnB,EAAkBgB,GAC/CT,EAAK,GAAAa,OAAMZ,GAAMY,OAAGC,EAAsBA,uBAACrB,IAC3CC,EAAmBO,EAASa,yBAAuBL,EACpD,CAEJ,CAAC,MAAOM,GACPC,QAAQC,IAAI,6BAA8BF,EAC3C,CAID,MAAO,CACLZ,SAAAA,EACAH,MAAAA,EACAP,gBAAAA,EACAC,iBAAAA,EACAK,MAAAA,EACAE,OAAAA,EACAiB,iBATiC,SAAVlB,GAAoBmB,QAAQpB,EAAMqB,IAUzDlB,WAAAA,EAEJ"}