"use client";

import * as React from "react";

import { useTranslation } from "@/hooks/useTranslation";

/**
 * SAR amount formatting, localized and memoized per language.
 *
 * Render the result through `<Typography variant="number">` (or `price`) —
 * that's what forces the numeric font and `dir="ltr"`, which compound amounts
 * need inside Arabic copy (design RTL rule 5).
 */
export function usePriceFormat() {
  const { t, language } = useTranslation();

  return React.useMemo(() => {
    const amount = new Intl.NumberFormat(language, {
      minimumFractionDigits: 0,
      maximumFractionDigits: 2,
    });
    const currency = t("common.currencySAR");

    return {
      /** e.g. "125 SAR" */
      formatPrice: (value: number) => `${amount.format(value)} ${currency}`,
      /** e.g. "80 – 150 SAR" — one currency label, not two */
      formatPriceRange: (min: number, max: number) =>
        `${amount.format(min)} – ${amount.format(max)} ${currency}`,
    };
  }, [language, t]);
}
