{
  name: "Two-Step Death",
  rating: 5,
  flags: {},
  onDamagePriority: -100,
  onDamage(damage, target, source, effect) {
    if (!target || target.fainted || !target.hp || typeof damage !== "number") return;
    if (target.bleachCaptainAwakened || target.transformed) return;
    const speciesId = (target.species?.id || "").toLowerCase();
    const speciesName = String(target.species?.name || "");
    const baseSpeciesName = String(target.baseSpecies?.baseSpecies || target.species?.baseSpecies || target.baseSpecies?.name || speciesName || "");
    if (speciesId === "soifonmega" || speciesId === "soifonbankai" || speciesName === "Soifon-Mega" || speciesName === "Soifon-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Soifon" && speciesName !== "Soifon" && speciesId !== "soifon") return;
    if (target.hp - damage > target.maxhp * 0.30) return;
    target.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("twostepdeath");
    this.add("-activate", target, "ability: Two-Step Death");
    if (!target.formeChange("Soifon-Bankai", abilityEffect, true)) {
      target.bleachCaptainAwakened = false;
      return;
    }
    target.canMegaEvo = null;
    const hpBaseStat = target.species.baseStats?.hp ?? target.species.baseStats?.["hp"] ?? 1;
    const hpIV = target.set?.ivs?.hp ?? 31;
    const hpEV = target.set?.evs?.hp ?? 0;
    target.baseMaxhp = Math.floor((Math.floor(2 * hpBaseStat + hpIV + Math.floor(hpEV / 4) + 100) * target.level) / 100 + 10);
    const newMaxHP = target.volatiles?.["dynamax"] ? 2 * target.baseMaxhp : target.baseMaxhp;
    target.maxhp = newMaxHP;
    target.hp = newMaxHP;
    this.add("-heal", target, target.getHealth, "[from] ability: Two-Step Death");
    return 0;
  },
  onResidualOrder: 29,
  onResidual(pokemon) {
    if (!pokemon || pokemon.fainted || !pokemon.hp) return;
    if (pokemon.bleachCaptainAwakened || pokemon.transformed) return;
    const speciesId = (pokemon.species?.id || "").toLowerCase();
    const speciesName = String(pokemon.species?.name || "");
    const baseSpeciesName = String(pokemon.baseSpecies?.baseSpecies || pokemon.species?.baseSpecies || pokemon.baseSpecies?.name || speciesName || "");
    if (speciesId === "soifonmega" || speciesId === "soifonbankai" || speciesName === "Soifon-Mega" || speciesName === "Soifon-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Soifon" && speciesName !== "Soifon" && speciesId !== "soifon") return;
    if (pokemon.hp > pokemon.maxhp * 0.30) return;
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("twostepdeath");
    this.add("-activate", pokemon, "ability: Two-Step Death");
    if (!pokemon.formeChange("Soifon-Bankai", abilityEffect, true)) {
      pokemon.bleachCaptainAwakened = false;
      return;
    }
    pokemon.canMegaEvo = null;
    const hpBaseStat = pokemon.species.baseStats?.hp ?? pokemon.species.baseStats?.["hp"] ?? 1;
    const hpIV = pokemon.set?.ivs?.hp ?? 31;
    const hpEV = pokemon.set?.evs?.hp ?? 0;
    pokemon.baseMaxhp = Math.floor((Math.floor(2 * hpBaseStat + hpIV + Math.floor(hpEV / 4) + 100) * pokemon.level) / 100 + 10);
    const newMaxHP = pokemon.volatiles?.["dynamax"] ? 2 * pokemon.baseMaxhp : pokemon.baseMaxhp;
    pokemon.maxhp = newMaxHP;
    pokemon.hp = newMaxHP;
    this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Two-Step Death");
  },
  onStart(pokemon) {
    this.add("-ability", pokemon, "Two-Step Death");
  },
  onAfterMoveSecondarySelf(source, target, move) {
    if (!source || !target || !move || move.category === "Status") return;
    if (!move.flags?.contact || move.id === "suzumebachi") return;
    if (source.fainted || target.fainted) return;
    const mark = target.volatiles?.homonka;
    if (mark && mark.source === source) {
      target.removeVolatile("homonka");
      this.add("-activate", source, "ability: Two-Step Death");
      this.damage(Math.max(1, target.maxhp || target.baseMaxhp || 1), target, source, this.dex.abilities.get("twostepdeath"));
      return;
    }
    if (mark) target.removeVolatile("homonka");
    target.addVolatile("homonka", source, move);
  },
  onSwitchOut(pokemon) {
    for (const side of this.sides || []) {
      const mons = [...(side.active || []), ...(side.pokemon || [])];
      for (const mon of mons) {
        if (mon?.volatiles?.homonka?.source === pokemon) mon.removeVolatile("homonka");
      }
    }
  },
  onFaint(pokemon) {
    for (const side of this.sides || []) {
      const mons = [...(side.active || []), ...(side.pokemon || [])];
      for (const mon of mons) {
        if (mon?.volatiles?.homonka?.source === pokemon) mon.removeVolatile("homonka");
      }
    }
  },
  onEnd(pokemon) {
    for (const side of this.sides || []) {
      const mons = [...(side.active || []), ...(side.pokemon || [])];
      for (const mon of mons) {
        if (mon?.volatiles?.homonka?.source === pokemon) mon.removeVolatile("homonka");
      }
    }
  }
}
