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

// Function to check tags and reset
function checkTagsAndReset() {
    const players = world.getAllPlayers();
    
    // Check if ANY player has the 'alive' tag
    const hasAlivePlayers = players.some(p => p.hasTag('alive'));
    
    // Only proceed if NO players have the 'alive' tag
    if (!hasAlivePlayers) {
        for (const player of players) {
            if (player.hasTag('area_1')) {
                player.runCommand('tag @a add end_lost_normal');
                player.runCommand('tag @a remove area_1');
            }
            if (player.hasTag('area_2')) {
                player.runCommand('tag @a add end_lost_medium');
                player.runCommand('tag @a remove area_2');
            }
            if (player.hasTag('area_3')) {
                player.runCommand('tag @a add end_lost_large');
                player.runCommand('tag @a remove area_3');
            }
        }
    }
}

// Run the check every tick using system.runInterval
system.runInterval(() => {
    checkTagsAndReset();
}, 1);