{
  name: "Purranormal",
  rating: 4,
  num: -1012,
  flags: {},

  onStart(pokemon) {
    this.effectState.enabled = pokemon.hp >= pokemon.maxhp / 2;
    if (!this.effectState.enabled) return;

    const teamTypesList = [];
    for (const ally of pokemon.side.pokemon) {
      if (!ally?.baseSpecies?.types) continue;
      teamTypesList.push(new Set(ally.baseSpecies.types));
    }

    if (teamTypesList.length === 0) return;

    let sharedTypes = new Set(teamTypesList[0]);
    for (const typeSet of teamTypesList.slice(1)) {
      sharedTypes = new Set([...sharedTypes].filter(type => typeSet.has(type)));
    }

    const hasSharedType = sharedTypes.size > 0;

    const uniqueTypes = new Set();
    for (const typeSet of teamTypesList) {
      for (const type of typeSet) {
        uniqueTypes.add(type);
      }
    }

    this.effectState.hasSharedType = hasSharedType;
    this.effectState.uniqueCount = uniqueTypes.size;
    this.effectState.statFocus = pokemon.storedStats.spe > pokemon.storedStats.spa ? "spe" : "spa";

    this.add("-ability", pokemon, "Purranormal");
    if (hasSharedType) {
      this.add("-message", `${pokemon.name} has channeled the power of its allies!`);
    } else if (this.effectState.statFocus === "spe") {
      this.add("-message", `${pokemon.name}'s Speed rose due to Purranormal!`);
    } else {
      this.add("-message", `${pokemon.name}'s Special Attack rose due to Purranormal!`);
    }
  },

  onResidual(pokemon) {
    const isActive = pokemon.hp >= pokemon.maxhp / 2;
    if (this.effectState.enabled && !isActive) {
      this.effectState.enabled = false;
      this.add("-message", `${pokemon.name}'s Purranormal boost faded!`);
    } else if (!this.effectState.enabled && isActive) {
      this.effectState.enabled = true;
      this.add("-message", `${pokemon.name}'s Purranormal power returns!`);
    }
  },

  onModifySpe(spe, pokemon) {
    if (!this.effectState.enabled || this.effectState.statFocus !== "spe") return;
    const amount = this.effectState.hasSharedType
      ? 1.5
      : 1 + Math.min(0.5, this.effectState.uniqueCount * 0.05);
    return this.chainModify(amount);
  },

  onModifySpA(spa, pokemon) {
    if (!this.effectState.enabled || this.effectState.statFocus !== "spa") return;
    const amount = this.effectState.hasSharedType
      ? 1.3
      : 1 + Math.min(0.3, this.effectState.uniqueCount * 0.03);
    return this.chainModify(amount);
  }
}
