export type WeightUnit = "g" | "oz" | "lb" | "kg";

const GRAMS_PER_OZ = 28.3495;
const GRAMS_PER_LB = 453.592;
const GRAMS_PER_KG = 1000;

export function formatWeight(
	grams: number | null | undefined,
	unit: WeightUnit = "g",
): string {
	if (grams == null) return "--";
	switch (unit) {
		case "g":
			return `${Math.round(grams)}g`;
		case "oz":
			return `${(grams / GRAMS_PER_OZ).toFixed(1)} oz`;
		case "lb":
			return `${(grams / GRAMS_PER_LB).toFixed(2)} lb`;
		case "kg":
			return `${(grams / GRAMS_PER_KG).toFixed(2)} kg`;
	}
}

export type Currency = "USD" | "EUR" | "GBP" | "JPY" | "CAD" | "AUD";

const CURRENCY_SYMBOLS: Record<Currency, string> = {
	USD: "$",
	EUR: "€",
	GBP: "£",
	JPY: "¥",
	CAD: "CA$",
	AUD: "A$",
};

export function formatPrice(
	cents: number | null | undefined,
	currency: Currency = "USD",
): string {
	if (cents == null) return "--";
	const symbol = CURRENCY_SYMBOLS[currency];
	if (currency === "JPY") {
		return `${symbol}${Math.round(cents / 100)}`;
	}
	return `${symbol}${(cents / 100).toFixed(2)}`;
}
