import { adjustDecimals } from "./adjustDecimals";

export const abbreviateNumber = (
	value: number | string,
	locale: string,
	enableThousand?: boolean,
): string => {
	const numValue = typeof value === "string" ? Number.parseFloat(value) : value;

	if (Number.isNaN(numValue)) return value.toString();
	if (numValue >= 1e9) {
		// biome-ignore format:
		return `${(numValue / 1e9).toFixed(1)}B`; // Billion
	}
	if (numValue >= 1e6) {
		// biome-ignore format:
		return `${(numValue / 1e6).toFixed(1)}M`; // Million
	}
	if (numValue >= 1e3 && enableThousand) {
		// biome-ignore format:
		return `${(numValue / 1e3).toFixed(1)}K`; // Thousand
	}
	return adjustDecimals(numValue, 2, locale);
};
