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

function manageInvisibleTags() {
    const seaLevel = 63;
    const query = { 
        families: ["monster"]
    };

    for (const entity of world.getDimension("overworld").getEntities(query)) {
        if (!entity.location) continue;
        const { y } = entity.location;
        const hasTag = entity.hasTag("aria_ah_invisible_buff");

        if (y > seaLevel && !hasTag) {
            entity.addTag("aria_ah_invisible_buff");
            entity.addEffect("invisibility", 12 * 20, { 
                amplifier: 0, 
                showParticles: true 
            });
            //entity.runCommand("playsound aria_ah.dead_breath"); // Debugging line
        } else if (y <= seaLevel && hasTag) {
            entity.removeTag("aria_ah_invisible_buff");
            entity.removeEffect("invisibility"); // Correct removal method
        }
    }
}

system.afterEvents.scriptEventReceive.subscribe((event) => {
    if (event.id === "aria_ah:tag_mobs_invisible") {
        manageInvisibleTags();
    }
});
