import { EntityDamageCause, EquipmentSlot, GameMode, system, world } from "@minecraft/server";
import { sleep } from "./libraries/utils";
import { DamageItem } from "./modules/Durability";
const maxY = 24;
const cooldown = new Map();
system.runInterval(() => {
    const players = world.getPlayers();
    for (const player of players) {
        if (!player.isValid() || cooldown.has(player.id))
            continue;
        if (player.location.y >= maxY || player.matches({ gameMode: GameMode.creative })) {
            player.runCommand('fog @a[r=1.5] remove "true_gm:smog_fog"');
            continue;
        }
        player.runCommand('fog @s push "true_gm:smog" "true_gm:smog_fog"');
        const equip = player.getComponent('equippable');
        const head = equip.getEquipment(EquipmentSlot.Head);
        if (head?.typeId.includes('gas_mask'))
            continue;
        player.applyDamage(mapNumberToValue(player.location.y, -64, maxY, 6, 1), { cause: EntityDamageCause.drowning });
        cooldown.set(player.id, system.runTimeout(() => cooldown.delete(player.id), mapNumberToValue(player.location.y, -64, maxY, 10, 45)));
    }
}, 5);
system.runInterval(async () => {
    const players = world.getPlayers({ gameMode: GameMode.survival });
    for (const player of players) {
        if (player.location.y >= maxY)
            continue;
        const equip = player.equippable;
        const mask = equip.getEquipment(EquipmentSlot.Head);
        if (mask?.typeId !== 'true:leather_gas_mask')
            continue;
        DamageItem(player, EquipmentSlot.Head);
        await sleep(1);
    }
}, 20 * 20);
function mapNumberToValue(number, inputMin, inputMax, outputMin, outputMax) {
    const normalizedValue = (number - inputMin) / (inputMax - inputMin);
    return Math.round(outputMin + Math.max(0, Math.min(1, normalizedValue)) * (outputMax - outputMin));
}
