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

const lightLevels = {
  15: ["minecraft:beacon", "minecraft:campfire", "dungeon:fire_stick", "minecraft:froglight", "minecraft:glowstone", "minecraft:lantern", "minecraft:lava_bucket", "minecraft:lit_pumpkin", "minecraft:sea_lantern", "minecraft:shroomlight"],
  12: ["minecraft:candle", "minecraft:torch", "minecraft:soul_campfire", "minecraft:nether_star", "minecraft:soul_lantern"],
  10: ["minecraft:crying_obsidian", "minecraft:glow_lichen", "minecraft:soul_torch"],
  9: ["minecraft:catalyst", "minecraft:enchanting_table", "minecraft:ender_chest", "minecraft:redstone_torch", "minecraft:totem_of_undying"],
  7: ["minecraft:blaze_powder", "minecraft:blaze_rod", "minecraft:experience_bottle", "minecraft:glow_berries", "minecraft:glow_ink_sac", "minecraft:glowstone_dust", "minecraft:magma"],
};

// Combinar todos los ítems de luz en una lista
const allLightItems = Object.values(lightLevels).flat();

system.runInterval(() => {
  for (let player of world.getPlayers()) {
    const equip = player.getComponent(EntityEquippableComponent.componentId);
    const hand = equip?.getEquipment(EquipmentSlot.Mainhand);

    // Eliminar la etiqueta "mainhand" si el jugador no tiene ítems de luz
    if (player.hasTag("mainhand") && !player.hasTag("offhand")) {
      player.removeTag("mainhand");
      for (const lightItem of allLightItems) {
        // Ejecutar función no_light si no tiene el ítem en la mano
        if (!hand || !hand.typeId.includes(lightItem)) {
          player.runCommandAsync("function no_light");
        }
      }
    }

    // Si hay un ítem en la mano principal y no hay ítem en la mano secundaria
    if (hand && !player.hasTag("offhand")) {
      let foundLight = false;

      // Verificar el tipo de ítem y ejecutar el comando correspondiente
      for (const [lightLevel, items] of Object.entries(lightLevels)) {
        if (items.includes(hand.typeId)) {
          player.addTag("mainhand");

          // Ejecutar el comando de acuerdo con el nivel de luz
          switch (parseInt(lightLevel)) {
            case 15:
              player.runCommandAsync("execute as @s positioned ~ ~1 ~ run function light15");
              break;
            case 12:
              player.runCommandAsync("execute as @s positioned ~ ~1 ~ run function light12");
              break;
            case 10:
              player.runCommandAsync("execute as @s positioned ~ ~1 ~ run function light10");
              break;
            case 9:
              player.runCommandAsync("execute as @s positioned ~ ~1 ~ run function light9");
              break;
            case 7:
              player.runCommandAsync("execute as @s positioned ~ ~1 ~ run function light7");
              break;
          }

          foundLight = true;
          break;
        }
      }

      // Si no se encuentra el ítem en las listas, eliminar etiqueta "mainhand"
      if (!foundLight) {
        player.removeTag("mainhand");
      }
    }
  }
});
