export function calculateCurrentCharges(
	currentCharges: number,
	maxCharges: number,
	lastUpdate: Date,
	cooldownMs: number
): number {
	if (currentCharges >= maxCharges) return currentCharges;

	const timeSinceLastUpdate = Date.now() - lastUpdate.getTime();
	const chargesGenerated = Math.floor(timeSinceLastUpdate / cooldownMs);

	return Math.min(maxCharges, currentCharges + chargesGenerated);
}

export function getNextChargeTime(
	currentCharges: number,
	maxCharges: number,
	lastUpdate: Date,
	cooldownMs: number
): Date | null {
	if (currentCharges >= maxCharges) return null;

	const timeSinceLastUpdate = Date.now() - lastUpdate.getTime();
	const timeUntilNextCharge = cooldownMs - (timeSinceLastUpdate % cooldownMs);

	return new Date(Date.now() + timeUntilNextCharge);
}

export function getChargesRegenPercentage(
	currentCharges: number,
	maxCharges: number,
	lastUpdate: Date,
	cooldownMs: number
): number {
	if (currentCharges >= maxCharges) return 0;

	const timeSinceLastUpdate = Date.now() - lastUpdate.getTime();
	const progress = (timeSinceLastUpdate % cooldownMs) / cooldownMs;

	return progress * 100;
}
