import type { Currency } from "../lib/formatters";
import { useSetting } from "./useSettings";

const VALID_CURRENCIES: Currency[] = [
	"USD",
	"EUR",
	"GBP",
	"JPY",
	"CAD",
	"AUD",
];

const CURRENCY_MARKET_MAP: Record<string, string> = {
	EUR: "EU",
	USD: "US",
	GBP: "UK",
	JPY: "JP",
	CAD: "CA",
	AUD: "AU",
};

export interface CurrencyContext {
	currency: Currency;
	market: string;
	showConversions: boolean;
}

export function useCurrency(): CurrencyContext {
	const { data: currencyData } = useSetting("currency");
	const { data: showConversionsData } = useSetting("showConversions");

	const currency: Currency =
		currencyData && VALID_CURRENCIES.includes(currencyData as Currency)
			? (currencyData as Currency)
			: "USD";

	return {
		currency,
		market: CURRENCY_MARKET_MAP[currency] ?? currency,
		showConversions: showConversionsData === "true",
	};
}
