import { DEFAULT_CURRENCY, EURO_CURRENCY } from "../constants/app";

export type ProductPriceComponentProps = {
  amount: string | number;
  useEuroCurrency?: boolean;
};

export default function ProductPriceComponent({
  amount,
  useEuroCurrency,
}: ProductPriceComponentProps) {
  if (amount == null || amount === undefined) {
    return <></>;
  }

  const numAmount = Number(amount);

  // Check if the number has decimals
  const hasDecimals = numAmount % 1 !== 0;

  // Format: show decimals only if they exist, max 2 decimal places
  const formattedAmount = hasDecimals
    ? numAmount.toFixed(2).replace(/\.?0+$/, "")
    : numAmount.toString();

  return (
    <>
      {useEuroCurrency ? EURO_CURRENCY : DEFAULT_CURRENCY}
      {formattedAmount}
    </>
  );
}
