import React, { useMemo } from "react";

const priceInfoKeys = [
  "originalPrice",
  "discountedPrice",
  "rewardFlag",
  "discountAmount",
  "discountRate",
];

export default function usePriceInfo(priceInfo) {
  const parsedPriceInfo = useMemo(() => {
    const splittedPriceInfo = priceInfo.split("|").map((item) => item.trim());

    const processedPriceInfo = priceInfoKeys.reduce((objcet, key, index) => {
      objcet[key] = splittedPriceInfo[index];
      return objcet;
    }, {});

    const {
      originalPrice,
      discountedPrice,
      rewardFlag,
      discountAmount,
      discountRate,
    } = processedPriceInfo;

    const discountRateNumeric =
      discountRate || Number(discountRate.slice(0, -1));

    return {
      originalPrice,
      discountedPrice,
      rewardFlag,
      discountAmount,
      discountRate,
      discountRateNumeric,
    };
  }, [priceInfo]);

  return parsedPriceInfo;
}
