{
  name: "Iron Loyalty",
  rating: 4,
  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 === "komamuramega" || speciesId === "komamurabankai" || speciesName === "Komamura-Mega" || speciesName === "Komamura-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Komamura" && speciesName !== "Komamura" && speciesId !== "komamura") return;
    if (target.hp - damage > target.maxhp * 0.30) return;
    target.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("ironloyalty");
    this.add("-activate", target, "ability: Iron Loyalty");
    if (!target.formeChange("Komamura-Bankai", abilityEffect, true)) {
      target.bleachCaptainAwakened = false;
      return;
    }
    target.canMegaEvo = null;
    if (!target.hasAbility("giantavatar")) {
      if (target.setAbility) target.setAbility("giantavatar", target, abilityEffect, true);
      if (!target.hasAbility("giantavatar")) target.ability = "giantavatar";
    }
    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: Iron Loyalty");
    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 === "komamuramega" || speciesId === "komamurabankai" || speciesName === "Komamura-Mega" || speciesName === "Komamura-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Komamura" && speciesName !== "Komamura" && speciesId !== "komamura") return;
    if (pokemon.hp > pokemon.maxhp * 0.30) return;
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("ironloyalty");
    this.add("-activate", pokemon, "ability: Iron Loyalty");
    if (!pokemon.formeChange("Komamura-Bankai", abilityEffect, true)) {
      pokemon.bleachCaptainAwakened = false;
      return;
    }
    pokemon.canMegaEvo = null;
    if (!pokemon.hasAbility("giantavatar")) {
      if (pokemon.setAbility) pokemon.setAbility("giantavatar", pokemon, abilityEffect, true);
      if (!pokemon.hasAbility("giantavatar")) pokemon.ability = "giantavatar";
    }
    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: Iron Loyalty");
  },
  onTryBoost(boost, target, source, effect) {
    if (!target || !target.hasAbility("ironloyalty")) return;
    if (boost.atk && boost.atk < 0) {
      delete boost.atk;
      this.add("-activate", target, "ability: Iron Loyalty");
    }
  },
  onDragOutPriority: 1,
  onDragOut(pokemon) {
    this.add("-activate", pokemon, "ability: Iron Loyalty");
    return null;
  }
}
