import { world } from "@minecraft/server";

/*─── 1. regeneración ──────────────────────────────────────────────*/
export function regeneration() {
  const rules = world.gameRules;
  if (!rules?.naturalRegeneration) return;

  for (const player of world.getPlayers()) {
    const health = player.getComponent("minecraft:health");
    if (!health) continue;

    const current = health.currentValue;
    if (current <= 0) continue;

    const max = health.effectiveMax;
    if (current >= max) continue;

    const saturationComp = player.getComponent("minecraft:player.saturation");
    const hungerComp = player.getComponent("minecraft:player.hunger");
    if (!saturationComp || !hungerComp) continue;

    if (hungerComp.currentValue <= 19.5) continue;

    // Efectos negativos
    const hasNegativeEffect =
      player.getEffect("poison") ||
      player.getEffect("fatal_poison") ||
      player.getEffect("wither");

    // Fuego
    const onFireComp = player.getComponent("minecraft:onfire");
    const isOnFire = onFireComp && onFireComp.onFireTicksRemaining > 0;

    const healAmount =
      (hasNegativeEffect || isOnFire) ? 0.25 : 1;

    health.setCurrentValue(Math.min(current + healAmount, max));
    saturationComp.setCurrentValue(
      Math.max(0, saturationComp.currentValue - 1.65)
    );
  }
}
