/*
----------------------------------
Creator: Mafly
Discord:
https://discord.gg/BUFxackwWb
Youtube: MaFly
https://www.youtube.com/c/MaFly16

Website:
https://mcblibrary.com/user/mafly
----------------------------------
*/

const cooldowns = new Map();

function applyCooldown(playerName, cooldownKey, cooldownTime) {
  const currentTime = new Date().getTime();
  const lastActionTime = cooldowns.get(`${playerName}:${cooldownKey}`) || 0;
  const timeDifference = currentTime - lastActionTime;

  if (timeDifference < cooldownTime * 1000) {
    const remainingTime = Math.ceil(
      (cooldownTime * 1000 - timeDifference) / 1000
    );
    return { active: true, remainingTime };
  }

  cooldowns.set(`${playerName}:${cooldownKey}`, currentTime);
  return { active: false };
}

function clearCooldown(playerName, cooldownKey) {
  cooldowns.delete(`${playerName}:${cooldownKey}`);
}

export { applyCooldown, clearCooldown };
