import JSON_LABELS from "@/constants/levelLabel.json";

const LABELS = JSON_LABELS as {[key: string]: string};

export const levelCalculator = (experience: number) => {
	const sqrt = Math.sqrt(experience);
	const labelLevel =
		Object.keys(LABELS)
			.reverse()
			.filter((x) => parseInt(x) <= Math.floor(sqrt))
			.shift() || Object.keys(LABELS).reverse().shift()!;

	return {
		currentLevel: Math.floor(sqrt),
		nextLevel: Math.ceil(sqrt),
		percentage: Math.floor((sqrt - Math.floor(sqrt)) * 100),
		nextLevelExperience: Math.pow(Math.ceil(sqrt), 2),
		label: LABELS[labelLevel],
	};
};
