{
  name: "Helix Nebula",
  rating: 4,
  num: -21001,

  onSwitchIn(pokemon) {
    const foe = pokemon.side.foe?.active?.find(p => p && !p.fainted);
    if (!foe) return;

    const types = foe.getTypes?.();
    if (!types || !types.length) return;

    pokemon.setType(types, pokemon, this.effect);
    this.add("-ability", pokemon, "Helix Nebula");
    this.add("-start", pokemon, "typechange", types.join("/"), "[from] ability: Helix Nebula");
  },

  onDamage(damage, target, source, effect) {
    if (!source || source.fainted) return;
    if (!effect || effect.effectType !== "Move") return;
    if (effect.category === "Status") return;

    if (damage < target.hp) return;
    if (target.abilityState.helixNebulaWish) return;
    target.abilityState.helixNebulaWish = true;

    const healAmount = Math.floor(target.maxhp / 2);

    let applied = false;

    if (typeof target.addSlotCondition === "function") {
      applied = !!target.addSlotCondition("wish", target);
      if (applied) {
        const side = target.side;
        const slot = target.position;
        const wishData = side?.slotConditions?.[slot]?.wish;
        if (wishData) {
          wishData.source = target;
          wishData.hp = healAmount;
          if (wishData.duration == null) wishData.duration = 2;
          if (wishData.time == null) wishData.time = 2;
          if (wishData.turns == null) wishData.turns = 2;
        }
      }
    } else if (typeof target.side?.addSlotCondition === "function") {
      applied = !!target.side.addSlotCondition(target, "wish");
      if (applied) {
        const side = target.side;
        const slot = target.position;
        const wishData = side?.slotConditions?.[slot]?.wish;
        if (wishData) {
          wishData.source = target;
          wishData.hp = healAmount;
          if (wishData.duration == null) wishData.duration = 2;
          if (wishData.time == null) wishData.time = 2;
          if (wishData.turns == null) wishData.turns = 2;
        }
      }
    } else if (typeof target.side?.addSideCondition === "function") {
      applied = !!target.side.addSideCondition("wish", target);
      const wishData = target.side?.sideConditions?.wish;
      if (wishData) {
        wishData.source = target;
        wishData.hp = healAmount;
        if (wishData.duration == null) wishData.duration = 2;
        if (wishData.time == null) wishData.time = 2;
        if (wishData.turns == null) wishData.turns = 2;
      }
    }

    if (applied) {
      this.add("-ability", target, "Helix Nebula");
      this.add("-message", `${target.name} made a wish!`);
    }
  }
}
