import { TicksPerSecond, world, system, Player } from "@minecraft/server";
import { ActionFormData } from "@minecraft/server-ui";

function showForm(player, form, callback) {
  form.show(player).then(result => {
    if (result.canceled) return;
    callback(result.selection);
  });
}

function showTeleportMenu(player) {
  const candidatos = world.getPlayers()
    .filter(p => p.hasTag("allow_teleportation"));

  if (candidatos.length === 0) {
    player.sendMessage("titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.teleport_players\"}]}`");
    return;
  }

  const form = new ActionFormData()
    .title({ translate: "item.dungeon:teleport_player_potion.name" })
    .body({ translate: "dungeon.langcode.teleport_players_select" });

  // Un botón por cada candidato
  for (const tgt of candidatos) {
    form.button(tgt.nameTag);
  }

  showForm(player, form, selection => {
    const target = candidatos[selection];
    player.runCommand(`playsound mob.endermen.portal @a[r=4] ~ ~ ~ 1 1.3`);
    player.runCommand(`tp @s ${target.nameTag}`);
  });
}

system.beforeEvents.startup.subscribe((initEvent) => {
 initEvent.itemComponentRegistry.registerCustomComponent("otherworldly_winter:north_pole_staff", {
  onUse: ({ source }) => {
    // Mensaje si no trae munición
    source.runCommand(
      `execute unless entity @s[hasitem={item=dungeon:luminarite}] run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_10"}]}`
    );

    // Sonido si sí trae munición
    source.runCommand(
      `execute if entity @s[hasitem={item=dungeon:luminarite}] run playsound random.bow @a[r=8] ~ ~ ~ 0.5 1.35`
    );

    // Para cada mob cercano con family=monster, invocar proyectil y encerrarlo en hielo
    source.runCommand(
      `execute if entity @s[hasitem={item=dungeon:luminarite}] as @e[type=!player,family=monster,r=8] at @s run summon dungeon:magic_ball ~ ~ ~ ~ ~ minecraft:player_proyectile`
    );
    source.runCommand(
      `execute if entity @s[hasitem={item=dungeon:luminarite}] as @e[type=!player,family=monster,r=8] at @s run fill ~-1 ~ ~-1 ~1 ~2 ~1 otherworldly_winter:snow keep`
    );

    const hasAmmoBuff = source.hasTag("ammo_buff");

    // Determinar si debe consumir luminarite
    let consume = true;
    if (hasAmmoBuff) {
      const chance = Math.random(); // 0.0–0.999
      consume = chance <= 0.6;
    }

    if (consume) {
      source.runCommand(`clear @s dungeon:luminarite 0 10`);
    }
  },
});
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:spider_staff", {
    onUse: ({ source }) => {
      source.runCommand(
        `execute unless entity @s[hasitem={item=dungeon:luminarite}]  run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_10"}]}`
      );
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run playsound random.bow @a[r=8] ~ ~ ~ 0.5 1.35`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run summon dungeon:spider_boss ~ ~ ~ ~ ~ minecraft:event_minion_player`);
      source.runCommand(`tag @e[family=otherworldly_spiders,r=8] add dfriend_spider`);

      const hasAmmoBuff = source.hasTag("ammo_buff");

      // Determinar si debe consumir luminarite
      let consume = true;
      if (hasAmmoBuff) {
        const chance = Math.random(); // 0.0–0.999
        consume = chance <= 0.6;
      }

      if (consume) {
        source.runCommand(`clear @s dungeon:luminarite 0 10`);
      }
    },
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:ancient_spider_staff", {
    onUse: ({ source }) => {
      source.runCommand(
        `execute unless entity @s[hasitem={item=dungeon:luminarite}]  run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_5"}]}`
      );
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run playsound random.bow @a[r=8] ~ ~ ~ 0.5 1.35`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run summon dungeon:cavern_spider ~ ~ ~ ~ ~ minecraft:event_minion_player`);

      const hasAmmoBuff = source.hasTag("ammo_buff");

      // Determinar si debe consumir luminarite
      let consume = true;
      if (hasAmmoBuff) {
        const chance = Math.random(); // 0.0–0.999
        consume = chance <= 0.6;
      }

      if (consume) {
        source.runCommand(`clear @s dungeon:luminarite 0 5`);
      }
    },
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:spiderboss_bag", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:spiderboss_bag 0 1`);
      source.runCommand(`effect @s absorption 120 8 false`);
      source.runCommand(`tag @a add difficulty_phase4`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s[r=4] ~~~ 1 0.4`);
      source.runCommand(`give @s dungeon:ancient_ingot 24`);
      source.runCommand(`give @s dungeon:ancient_apple 16`);
      source.runCommand(`give @s dungeon:spider_staff 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:toxic_spider_fang", {
    onUse: ({ source }) => {
      source.runCommand(`function toxic_spider_fang`);
      source.runCommand(`summon dungeon:spider_boss ~ ~ ~ ~ ~ minecraft:entity_spawned`);
      source.runCommand(`clear @s dungeon:toxic_spider_fang 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:hornet_staff", {
    onUse: ({ source }) => {
      source.runCommand(
        `execute unless entity @s[hasitem={item=dungeon:luminarite}]  run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_5"}]}`
      );
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run playsound random.bow @a[r=8] ~ ~ ~ 0.5 1.35`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run summon dungeon:minion_hornet`);

      const hasAmmoBuff = source.hasTag("ammo_buff");

      // Determinar si debe consumir luminarite
      let consume = true;
      if (hasAmmoBuff) {
        const chance = Math.random(); // 0.0–0.999
        consume = chance <= 0.6;
      }

      if (consume) {
        source.runCommand(`clear @s dungeon:luminarite 0 5`);
      }
    },
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:glimmerite_hammer", {
    onUse: ({ source }) => {
      source.runCommand(`effect @s[r=6] absorption 20 0 false`);
      source.runCommand(`effect @a[r=6] regeneration 10 0 false`);

    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:cloud_staff", {
    onUse: ({ source }) => {
      source.runCommand(
        `execute unless entity @s[hasitem={item=dungeon:luminarite}]  run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required"}]}`
      );
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run playsound random.bow @a[r=8] ~ ~ ~ 0.5 1.35`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite}] run summon dungeon:minion_cloud`);

      // Comprobar si tiene ammo_buff
      const hasAmmoBuff = source.hasTag("ammo_buff");

      // Determinar si debe consumir luminarite
      let consume = true;
      if (hasAmmoBuff) {
        const chance = Math.random(); // 0.0–0.999
        consume = chance <= 0.6;
      }

      if (consume) {
        source.runCommand(`clear @s dungeon:luminarite 0 1`);
      }
    },
  });

  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:gauntlet", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:gauntlet 0 1`);
      source.runCommand(`effect @s strength 240 2 true`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s[r=4] ~~~ 1 0.4`);
      source.runCommand(`effect @s slowness 240 0 true`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:lumiboth_bag", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:lumiboth_bag 0 1`);
      source.runCommand(`effect @s absorption 120 6 false`);
      source.runCommand(`tag @a add difficulty_phase3`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s[r=4] ~~~ 1 0.4`);
      source.runCommand(`summon dungeon:lumimoth ~ ~ ~ ~ ~ minecraft:tamed_moth`);
      source.runCommand(`give @s dungeon:flyswatter 1`);
      source.runCommand(`give @s dungeon:titanium_ore_item 1`);
      source.runCommand(`give @s dungeon:chitin 10`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:bone_bag", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:bone_bag 0 1`);
      source.runCommand(`effect @s absorption 120 2 false`);
      source.runCommand(`tag @a add difficulty_phase1`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s[r=4] ~~~ 1 0.4`);
      source.runCommand(`give @s dungeon:harrotrophy_hard`);
      source.runCommand(`give @s dungeon:ancient_battleaxe`);
      source.runCommand(`give @s dungeon:ancient_skull 4`);
      source.runCommand(`give @s dungeon:bone 32`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:pumpking_bag", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:pumpking_bag 0 1`);
      source.runCommand(`effect @s absorption 120 2 false`);
      source.runCommand(`tag @a add difficulty_phase1`);
      source.runCommand(`give @s dungeon:pumpkintrophy_hard`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s`);
      source.runCommand(`give @s dungeon:fire_stick`);
      source.runCommand(`give @s dungeon:pumpkin_charge 4 0`);
      source.runCommand(`give @s dungeon:zombie_horse_spawn_egg`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:magic_bag", {
    onUse: ({ source }) => {
      source.runCommand(`clear @s dungeon:magic_bag 0 1`);
      source.runCommand(`give @s dungeon:silvirum_pickaxe`);
      source.runCommand(`tag @a add difficulty_phase2`);
      source.runCommand(`give @s dungeon:soultrophy_hard`);
      source.runCommand(`effect @s absorption 120 4 false`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @s`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:ultimate_heart", {
    onUse: ({ source }) => {
      source.runCommand(`scoreboard objectives add testh2 dummy`);
      source.runCommand(`titleraw @s[scores={testh=0..5}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.heart2\"}]}`);
      source.runCommand(`effect @s[scores={testh=6..}] regeneration 10 0 false`);
      source.runCommand(`scoreboard players add @s[scores={testh=6..}] testh2 1`);
      source.runCommand(`effect @s[tag=hp14] regeneration 10 1`);
      source.runCommand(`execute at @s run playsound beacon.ambient @a[scores={testh=6..},r=1]`);
      source.runCommand(`clear @s[scores={testh=6..}] dungeon:ultimate_heart 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:heart", {
    onUse: ({ source }) => {
      source.runCommand(`scoreboard objectives add testh dummy`);
      source.runCommand(`scoreboard players add @s testh 0`);
      source.runCommand(`effect @s[scores={testh=0..2}] regeneration 4 0  false`);
      source.runCommand(`titleraw @s[scores={testh=6..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.heart\"}]}`);
      source.runCommand(`scoreboard players add @s[scores={testh=0..6}] testh 1`);
      source.runCommand(`effect @s[scores={testh=3..5}] regeneration 2 0`);
      source.runCommand(`execute at @s[scores={testh=0..5}] run playsound mob.wither.spawn @a[r=2]`);
      source.runCommand(`clear @s[scores={testh=0..6}] dungeon:heart 0 1`);

    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:skull", {
    onUse: ({ source }) => {
      source.runCommand(`function harrotext`);
      source.runCommand(`summon dungeon:harrowing ~-5 ~-4 ~5`);
      source.runCommand(`clear @s dungeon:skull 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:lumimoth_boss", {
    onUse: ({ source }) => {
      source.runCommand(`function lumimoth_boss`);
      source.runCommand(`summon dungeon:lumimoth ~ ~-3 ~ ~ ~ minecraft:entity_spawned`);
      source.runCommand(`clear @s dungeon:lumimoth_boss 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:spine", {
    onUse: ({ source }) => {
      source.runCommand(`function braintext`);
      source.runCommand(`summon dungeon:zombie_horse ~-4 ~ ~ ~ ~ minecraft:event_pumpkin Ridle`);
      source.runCommand(`clear @s dungeon:pumpkin_boss 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:bone_relic", {
    onUse: ({ source }) => {
      source.runCommand(`effect @s resistance 240 1 true`);
      source.runCommand(`effect @s slowness 240 0 true`);
      source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
      source.runCommand(`clear @s dungeon:bone_relic 0 1`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:petrifyn", {
    onUse: ({ source }) => {
      const hasAmmoBuff = source.hasTag("ammo_buff");

      let shouldConsume = true;
      if (hasAmmoBuff) {
        shouldConsume = Math.random() <= 0.6;
      }

      if (!shouldConsume) return;

      source.runCommand(`execute unless entity @s[hasitem={item=dungeon:luminarite,quantity=15..}]  run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_8"}]}`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run tag @s[tag=!stone] add stone_player`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run effect @s regeneration 8 3 true`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run scoreboard objectives add stonetest dummy`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run scoreboard players reset @s stonetest`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run effect @e[family=monster,r=6] slowness 10 255 false`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=15..}] run clear @s dungeon:luminarite 0 15`);
    },
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:cobalt_hammer", {
    onUse: ({ source }) => {
      source.runCommand(`tp @e[type=item,c=4,r=14] @s`);
      source.runCommand(`effect @e[family=mob,r=4] weakness 10 2 false`);

    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("dungeon:punisher", {
    onUse: ({ source }) => {
      const hasAmmoBuff = source.hasTag("ammo_buff");

      let shouldConsume = true;
      if (hasAmmoBuff) {
        shouldConsume = Math.random() <= 0.6;
      }

      if (!shouldConsume) return;

      // Si tiene luminarite
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=5..}] run playsound mob.wither.spawn @s ~ ~ ~ 0.5 1.35`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=5..}] run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.minion"}]}`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=5..}] run summon dungeon:minion_skull`);
      source.runCommand(`execute if entity @s[hasitem={item=dungeon:luminarite,quantity=5..}] run clear @s dungeon:luminarite 0 5`);
      // Si no tiene luminarita, mensaje
      source.runCommand(`execute unless entity @s[hasitem={item=dungeon:luminarite,quantity=5..}] run titleraw @s actionbar {"rawtext":[{"translate":"dungeon.langcode.luminarite_required_5"}]}`);
    }
  });
  initEvent.itemComponentRegistry.registerCustomComponent("otherworldly_winter:food", {
    onConsume: ({ itemStack, source }) => {
      switch (itemStack.typeId) {
        case "dungeon:green_apple":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 1000 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          break;
        case "dungeon:ancient_apple":
          source.addEffect("regeneration", 2000, {
            amplifier: 0,
            showParticles: true
          });
          source.addEffect("absorption", 6000, {
            amplifier: 1,
            showParticles: true
          });
          source.addEffect("resistance", 2000, {
            amplifier: 1,
            showParticles: true
          });
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 10000 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          break;
        case "dungeon:crimson_edge_potion":
          // source.runCommand(`say §a[DEBUG] Poción Crimson activada`);

          const CRIMSON_DURATION = TicksPerSecond * 60;
          const BLEED_TAG = "crimson_bleed";
          const BLEED_TIME = TicksPerSecond * 5;
          let angle = 0;
          const radius = 0.8;
          const particleHeight = 1.0; // altura media del aura
          const bleedingEntities = new Map();

          const bleedInterval = system.runInterval(() => {
            for (const [id, data] of bleedingEntities) {
              const { entity, ticks } = data;

              if (!entity || entity.isDead) {
                bleedingEntities.delete(id);
                continue;
              }

              let hasTag = false;
              try {
                hasTag = entity.hasTag(BLEED_TAG);
              } catch (e) {
                bleedingEntities.delete(id);
                continue;
              }

              if (!hasTag) {
                bleedingEntities.delete(id);
                continue;
              }

              if (ticks >= BLEED_TIME) {
                entity.removeTag(BLEED_TAG);
                bleedingEntities.delete(id);
                continue;
              }

              const dmg = 1 + Math.floor(ticks / TicksPerSecond);
              try {
                entity.runCommand(`damage @s ${dmg} magic`);
                entity.dimension.spawnParticle("minecraft:critical_hit_emitter", {
                  x: entity.location.x, y: entity.location.y + 1.4, z: entity.location.z
                });
              } catch (e) {
                // source.runCommand(`say §c[ERROR daño] ${e}`);
              }

              data.ticks += TicksPerSecond * 0.25;
            }
          }, TicksPerSecond * 0.25);

          const visualParticleInterval = system.runInterval(() => {
            angle += Math.PI / 12; // gira un poco cada tick (30°)

            for (let i = 0; i < 6; i++) {
              const theta = angle + (i * (Math.PI * 2)) / 6; // 6 partículas distribuidas en círculo

              const x = source.location.x + Math.cos(theta) * radius;
              const y = source.location.y + particleHeight;
              const z = source.location.z + Math.sin(theta) * radius;

              try {
                source.dimension.spawnParticle("minecraft:redstone_wire_dust_particle", { x, y, z });
              } catch (e) { }
            }
          }, TicksPerSecond * 0.1); // actualiza rápido para dar efecto suave


          const hurtListener = world.afterEvents.entityHurt.subscribe(({ damageSource, hurtEntity, damage }) => {
            if (damage <= 0 || !damageSource || !hurtEntity) return;

            const attacker = damageSource.damagingEntity;
            if (!(attacker instanceof Player)) return;
            if (attacker.id !== source.id) return;

            // source.runCommand(`say §e[DEBUG] Golpeaste a ${hurtEntity.typeId}`);

            if (hurtEntity.hasTag(BLEED_TAG)) {
              // source.runCommand(`say §7[DEBUG] Ya tenía sangrado`);
              return;
            }

            hurtEntity.addTag(BLEED_TAG);
            bleedingEntities.set(hurtEntity.id, {
              entity: hurtEntity,
              ticks: 0
            });

            // source.runCommand(`say §d[DEBUG] Sangrado aplicado`);
          });

          system.runTimeout(() => {
            system.clearRun(bleedInterval);
            system.clearRun(visualParticleInterval);
            world.afterEvents.entityHurt.unsubscribe(hurtListener);
            // source.runCommand(`say §8[DEBUG] Efecto Crimson terminado`);
          }, CRIMSON_DURATION);

          break;
        case "dungeon:teleport_player_potion":
          // Solo si el jugador tiene permiso
          if (source.hasTag("allow_teleportation")) {
            showTeleportMenu(source);
          } else {
            // Mensaje al jugador si no tiene la etiqueta
            source.runCommand("titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.allow_teleportation\"}]}");
          }
          break;
        case "dungeon:bandage":
          source.addEffect("instant_health", TicksPerSecond * 0.1, { amplifier: 0 });
          break;
        case "dungeon:hunter_potion": {
          source.runCommand(`playsound random.orb @s ~ ~ ~ 1 1.5`);

          const HUNTER_DURATION = TicksPerSecond * 120;
          const startTime = system.currentTick;

          const interval = system.runInterval(() => {
            if (system.currentTick - startTime >= HUNTER_DURATION) {
              system.clearRun(interval);
              return;
            }

            const nearbyEntities = source.dimension.getEntities({
              location: source.location,
              maxDistance: 48,
              families: ["monster"],
              excludeTags: ["hunter_exempt"]
            });

            for (const mob of nearbyEntities) {
              const { x: mobX, y: mobY, z: mobZ } = mob.location;
              const { x: playerX, y: playerY, z: playerZ } = source.location;

              // Número de partículas intermedias entre el jugador y el mob
              const particleCount = 10;

              for (let i = 1; i <= particleCount; i++) {
                const t = i / particleCount;

                const x = playerX + (mobX - playerX) * t;
                const y = playerY + (mobY - playerY) * t + 1.2;
                const z = playerZ + (mobZ - playerZ) * t;

                source.dimension.spawnParticle("minecraft:redstone_wire_dust_particle", { x, y, z });
              }
            }
          }, TicksPerSecond * 0.5); // cada 0.5 segundos

          break;
        }
        case "dungeon:teleport_potion": {
          const dimension = source.dimension;
          const center = source.location;
          const maxRadius = 1000;

          function getRandomXZ(center, radius) {
            const angle = Math.random() * 2 * Math.PI;
            const r = Math.sqrt(Math.random()) * radius;
            const x = Math.floor(center.x + r * Math.cos(angle));
            const z = Math.floor(center.z + r * Math.sin(angle));
            return { x, z };
          }

          // Elegir coordenadas aleatorias XZ y Y fija dentro de rango seguro
          const { x, z } = getRandomXZ(center, maxRadius);
          const y = Math.floor(Math.random() * (319 - (-50) + 1)) + (-50);

          const destination = { x: x + 0.5, y: y, z: z + 0.5 };

          source.runCommand(`playsound mob.endermen.portal @a[r=4] ~ ~ ~ 1 1.3`);
          source.teleport(destination, {
            dimension,
            rotation: source.rotation,
            keepVelocity: false,
          });

          break;
        }
        case "dungeon:levitation_potion":
          source.addEffect("levitation", TicksPerSecond * 10, { amplifier: 10 });
          break;
        case "dungeon:return_potion":
          // Obtener el punto de aparición del jugador
          const spawnPoint = source.getSpawnPoint(); // Método para obtener el punto de aparición
          if (spawnPoint) {
            const { x, y, z } = spawnPoint; // Extraer coordenadas del punto de aparición

            source.runCommand(`playsound mob.shulker.teleport @a[r=4] ~ ~ ~ 1 1.2`);
            // Teletransportar al jugador al punto de aparición
            source.teleport({ x, y, z }, {
              dimension: source.dimension, // Utiliza la dimensión actual del jugador
              rotation: source.rotation, // Mantener orientación
              keepVelocity: false, // Detener movimiento
            });
          } else {
            // Mensaje al jugador si no tiene un punto de aparición configurado
            source.runCommand("titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.return_potion\"}]}");
          }
          break;
        case "dungeon:haste_potion2":
          source.addEffect("haste", TicksPerSecond * 90, { amplifier: 2 });
          break;
        case "dungeon:haste_potion":
          source.addEffect("haste", TicksPerSecond * 180, { amplifier: 1 });
          break;
        case "dungeon:poison_inmunity":
          source.runCommand(`tag @s add poison_inmunity`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
          break;
        case "dungeon:wither_inmunity":
          source.runCommand(`tag @s add wither_inmunity`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
          break;
        case "dungeon:mining_fatigue_inmunity":
          source.runCommand(`tag @s add fatigueinmunity`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
          break;
        case "dungeon:weakness_inmunity":
          source.runCommand(`tag @s add weakness_inmunity`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
          break;
        case "dungeon:slowness_inmunity":
          source.runCommand(`tag @s add slowness_inmunity`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @a[r=4]`);
          break;
        case "dungeon:melon_redpie":
          source.addEffect("mining_fatigue", 300, {
            amplifier: 0,
            showParticles: true
          });
          source.runCommand(`scoreboard players reset @s stonetest`);  // Eliminar contador de petrificación
          source.runCommand(`inputpermission set @s camera enabled`);  // Habilitar la camara
          source.runCommand(`inputpermission set @s movement enabled`);  // Habilitar el movimiento
          source.runCommand(`tag @s remove stone_player`);  // Eliminar la petrificación
          source.runCommand(`tag @s remove stone`);  // Eliminar la petrificación por gorgona
          source.runCommand(`tag @s remove poison_inmunity`);  // Eliminar la veneno
          source.runCommand(`tag @s remove wither_inmunity`);  // Eliminar la wither
          source.runCommand(`tag @s remove fatigueinmunity`);  // Eliminar la fatiga minera
          source.runCommand(`tag @s remove weakness_inmunity`);  // Eliminar la debilidad
          source.runCommand(`tag @s remove slowness_inmunity`);  // Eliminar la lentitud
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear31\"}]}`);
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 10000 `);
          source.runCommand(`scoreboard players reset @s tombstest`);
          source.runCommand(`tag @s remove cursedcementery`);
          source.runCommand(`fog @s remove cemetery`);
          source.runCommand(`scoreboard objectives add redpie dummy`);
          source.runCommand(`scoreboard players reset @s redpie`);
          source.runCommand(`tag @s remove dcursed`);
          source.runCommand(`tag @s remove cemetery`);
          source.runCommand(`tag @s remove punisher`);
          source.runCommand(`tag @s remove punishment`);
          source.runCommand(`scoreboard players add @s redpie 1200`);
          source.runCommand(`execute at @s run playsound mob.wither.spawn @s`);
          source.addEffect("hunger", TicksPerSecond * 2, { amplifier: 255 });
          source.addEffect("nausea", TicksPerSecond * 10, { amplifier: 0 });
          break;
        case "otherworldly_winter:candy_cane":
          source.addEffect("speed", 30, {
            amplifier: 2,
            showParticles: true
          });
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 300 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 50, { amplifier: 0 });
          break;
        case "otherworldly_winter:candy":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 10 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 5, { amplifier: 0 });
          break;
        case "otherworldly_winter:candy2":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 10 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("speed", 30, {
            amplifier: 2,
            showParticles: true
          });
          break;
        case "otherworldly_winter:christmas_stew":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 7500 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("haste", TicksPerSecond * 130, { amplifier: 1 });
          source.addEffect("slowness", TicksPerSecond * 630, { amplifier: 1 });
          source.addEffect("resistance", 620, {
            amplifier: 0,
            showParticles: true
          });
          break;
        case "otherworldly_winter:eggnog":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 6000 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 75, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 130, { amplifier: 0 });
          break;
        case "dungeon:magic_seeds":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}]  insomnio 150 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          break;
        case "otherworldly_winter:gingerbread_cookie":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 35, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 0 });
          break;
        case "otherworldly_winter:gingerbread_cookie2":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("absorption", TicksPerSecond * 35, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 0 });
          break;
        case "otherworldly_winter:gingerbread_cookie3":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("speed", TicksPerSecond * 65, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 0 });
          break;
        case "otherworldly_winter:gingerbread_cookie4":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("strength", TicksPerSecond * 35, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 0 });
          break;
        case "otherworldly_winter:hot_chocolate":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 2500 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 15, { amplifier: 4 });
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 1 });
          source.addEffect("speed", TicksPerSecond * 30, { amplifier: 1 });
          break;
        case "otherworldly_winter:ice_cream":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("regeneration", TicksPerSecond * 15, { amplifier: 0 });
          source.addEffect("resistance", TicksPerSecond * 60, { amplifier: 0 });
          break;
        case "otherworldly_winter:ice_cream2":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("resistance", TicksPerSecond * 60, { amplifier: 0 });
          source.addEffect("speed", TicksPerSecond * 30, { amplifier: 1 });
          break;
        case "otherworldly_winter:ice_cream3":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("resistance", TicksPerSecond * 120, { amplifier: 0 });
          source.addEffect("slowness", TicksPerSecond * 80, { amplifier: 0 });
          break;
        case "otherworldly_winter:ice_cream4":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("resistance", TicksPerSecond * 60, { amplifier: 0 });
          source.addEffect("speed", TicksPerSecond * 120, { amplifier: 0 });
          source.addEffect("regeneration", TicksPerSecond * 10, { amplifier: 0 });
          break;
        case "otherworldly_winter:ice_cream5":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("resistance", TicksPerSecond * 20, { amplifier: 1 });
          source.addEffect("regeneration", TicksPerSecond * 6, { amplifier: 0 });
          break;
        case "otherworldly_winter:ice_cream6":
          source.runCommand(`execute as @s[scores={insomnio=1..}]  run scoreboard players remove @s[scores={insomnio=1..}] insomnio 100 `);
          source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
          source.addEffect("resistance", TicksPerSecond * 30, { amplifier: 0 });
          source.addEffect("absorption", TicksPerSecond * 60, { amplifier: 0 });
          break;
        case "otherworldly_winter:gift_common":

          const randomChoice = Math.floor(Math.random() * 8); // Genera un número aleatorio entre 0 y 7.

          switch (randomChoice) {
            case 0:
              // Opción 1: Reduce el miedo
              source.runCommand(`scoreboard players remove @s[scores={insomnio=1..}] insomnio 25200`);
              source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
              break;

            case 1:
              // Opción 2: Otorga un efecto positivo
              source.addEffect("speed", TicksPerSecond * 30, { amplifier: 1 });
              source.addEffect("jump_boost", TicksPerSecond * 30, { amplifier: 1 });
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift\"}]}`);
              break;

            case 2:
              // Opción 3: Aparece un mob hostil cerca
              source.runCommand(`summon otherworldly_winter:elf ~ ~1 ~ ~ ~ minecraft:entity_spawned`);
              break;
            case 3:
              // Opción 4: Invoca un evento atmosférico
              source.runCommand(`weather thunder`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift2\"}]}`);
              break;
            case 4:
              // Opción 5: Otorga un regalo misterioso
              source.runCommand(`give @s minecraft:golden_apple 1`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift3\"}]}`);
              break;
            case 5:
              // Opción 6: Otorga un ponche de huevo
              source.runCommand(`give @s otherworldly_winter:eggnog`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 6:
              // Opción 7: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 7:
              // Opción 8: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            default:
              // Opción por defecto, en caso de errores
              source.runCommand(`titleraw @s actionbar {"rawtext":[{"text":"§7???"}]}`);
              break;
          }
          break;
        case "otherworldly_winter:gift_rare":
          const randomChoice2 = Math.floor(Math.random() * 9); // Genera un número aleatorio entre 0 y 8.

          switch (randomChoice2) {
            case 0:
              // Opción 1: Reduce el miedo
              source.runCommand(`scoreboard players remove @s[scores={insomnio=1..}] insomnio 35200`);
              source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
              break;

            case 1:
              // Opción 2: Otorga un efecto positivo
              source.addEffect("water_breathing", TicksPerSecond * 30, { amplifier: 0 });
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift\"}]}`);
              break;

            case 2:
              // Opción 3: Aparece un mob hostil cerca
              source.runCommand(`summon otherworldly_winter:elf ~ ~1 ~ ~ ~ minecraft:entity_spawned`);
              break;
            case 3:
              // Opción 4: Otorga Xp
              source.runCommand(`xp 10l @s`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.raregift\"}]}`);
              break;
            case 4:
              // Opción 5: Otorga un regalo misterioso
              source.runCommand(`give @s minecraft:golden_apple 2`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift3\"}]}`);
              break;
            case 5:
              // Opción 6: Otorga un helado
              source.runCommand(`give @s otherworldly_winter:ice_cream`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 6:
              // Opción 7: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 7:
              // Opción 8: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 8:
              // Opción 9: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            default:
              // Opción por defecto, en caso de errores
              source.runCommand(`titleraw @s actionbar {"rawtext":[{"text":"§7???"}]}`);
              break;
          }
          break;
        case "otherworldly_winter:gift_mythical":
          const randomChoice3 = Math.floor(Math.random() * 16); // Genera un número aleatorio entre 0 y 15.

          switch (randomChoice3) {
            case 0:
              // Opción 1: Reduce el miedo
              source.runCommand(`scoreboard players remove @s[scores={insomnio=1..}] insomnio 65200`);
              source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
              break;

            case 1:
              // Opción 2: Otorga un efecto positivo
              source.addEffect("strength", TicksPerSecond * 300, { amplifier: 0 });
              source.addEffect("resistance", TicksPerSecond * 300, { amplifier: 0 });
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift\"}]}`);
              break;

            case 2:
              // Opción 3: Aparece un mob hostil cerca
              source.runCommand(`summon otherworldly_winter:elf_friend ~ ~1 ~ ~ ~ minecraft:entity_spawned`);
              break;
            case 3:
              // Opción 4: Otorga Xp
              source.runCommand(`xp 60l @s`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.raregift\"}]}`);
              break;
            case 4:
              // Opción 5: Otorga un regalo misterioso
              source.runCommand(`give @s minecraft:golden_apple 20`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift3\"}]}`);
              break;
            case 5:
              // Opción 6: Otorga un helado
              source.runCommand(`give @s otherworldly_winter:ice_cream`);
              source.runCommand(`give @s otherworldly_winter:ice_cream2`);
              source.runCommand(`give @s otherworldly_winter:ice_cream3`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 6:
              // Opción 7: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie3`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 7:
              // Opción 8: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie4`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 8:
              // Opción 9: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:eggnog`);
              source.runCommand(`give @s otherworldly_winter:eggnog`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 9:
              // Opción 10: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 10:
              // Opción 11: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 11:
              // Opción 12: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:moonstone`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 12:
              // Opción 13: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:bone_relic`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 13:
              // Opción 14: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:bone_relic`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 14:
              // Opción 15: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:bone_relic`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 15:
              // Opción 15: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:slowness_inmunity`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            default:
              // Opción por defecto, en caso de errores
              source.runCommand(`titleraw @s actionbar {"rawtext":[{"text":"§7???"}]}`);
              break;
          }
          break;
        case "otherworldly_winter:gift_legendary":
          const randomChoice4 = Math.floor(Math.random() * 16); // Genera un número aleatorio entre 0 y 15.

          switch (randomChoice4) {
            case 0:
              // Opción 1: Reduce el miedo
              source.runCommand(`scoreboard players remove @s[scores={insomnio=1..}] insomnio 85200`);
              source.runCommand(`execute if entity @s[scores={insomnio=1..}] run titleraw @s[scores={insomnio=1..}] actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.fear26\"}]}`);
              break;

            case 1:
              // Opción 2: Otorga un efecto positivo
              source.addEffect("strength", TicksPerSecond * 300, { amplifier: 1 });
              source.addEffect("resistance", TicksPerSecond * 300, { amplifier: 1 });
              source.addEffect("absorption", TicksPerSecond * 300, { amplifier: 1 });
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift\"}]}`);
              break;

            case 2:
              // Opción 3: Aparece un mob cerca
              source.runCommand(`summon otherworldly_winter:elf_friend ~ ~1 ~ ~ ~ minecraft:entity_spawned`);
              source.runCommand(`summon otherworldly_winter:elf_friend ~ ~1 ~ ~ ~ minecraft:entity_spawned`);
              break;
            case 3:
              // Opción 4: Otorga Xp
              source.runCommand(`xp 100l @s`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.raregift\"}]}`);
              break;
            case 4:
              // Opción 5: Otorga un regalo misterioso
              source.runCommand(`give @s minecraft:golden_apple 80`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.commongift3\"}]}`);
              break;
            case 5:
              // Opción 6: Otorga un helado
              source.runCommand(`give @s otherworldly_winter:ice_cream`);
              source.runCommand(`give @s otherworldly_winter:ice_cream2`);
              source.runCommand(`give @s otherworldly_winter:ice_cream3`);
              source.runCommand(`give @s otherworldly_winter:ice_cream4`);
              source.runCommand(`give @s otherworldly_winter:ice_cream5`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 6:
              // Opción 7: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie3`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie3`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie3`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie3`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 7:
              // Opción 8: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie4`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie4`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie2`);
              source.runCommand(`give @s otherworldly_winter:gingerbread_cookie4`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 8:
              // Opción 9: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:eggnog`);
              source.runCommand(`give @s otherworldly_winter:hot_chocolate`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 9:
              // Opción 10: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`give @s otherworldly_winter:candy2`);
              source.runCommand(`give @s otherworldly_winter:candy`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 10:
              // Opción 11: Otorga un dulce
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`give @s otherworldly_winter:christmas_stew`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.christmasitem\"}]}`);
              break;
            case 11:
              // Opción 12: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:moonstone`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 12:
              // Opción 13: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:bone_relic`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 13:
              // Opción 14: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:gauntlet`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 14:
              // Opción 15: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:haste_potion2`);
              source.runCommand(`give @s dungeon:haste_potion2`);
              source.runCommand(`give @s dungeon:haste_potion2`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            case 15:
              // Opción 15: Otorga un objeto especial 
              source.runCommand(`give @s dungeon:slowness_inmunity`);
              source.runCommand(`give @s dungeon:weakness_inmunity`);
              source.runCommand(`execute if entity @s run titleraw @s actionbar {\"rawtext\":[{\"translate\":\"dungeon.langcode.mythical\"}]}`);
              break;
            default:
              // Opción por defecto, en caso de errores
              source.runCommand(`titleraw @s actionbar {"rawtext":[{"text":"§7???"}]}`);
              break;
          }
          break;
      };
    },
  });
});
