{
  name: "Blind Justice",
  rating: 4.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 === "tosenmega" || speciesId === "tosenbankai" || speciesName === "Tosen-Mega" || speciesName === "Tosen-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Tosen" && speciesName !== "Tosen" && speciesId !== "tosen") return;
    if (target.hp - damage > target.maxhp * 0.30) return;
    target.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("blindjustice");
    this.add("-activate", target, "ability: Blind Justice");
    if (!target.formeChange("Tosen-Bankai", abilityEffect, true)) {
      target.bleachCaptainAwakened = false;
      return;
    }
    target.canMegaEvo = null;
    if (!target.hasAbility("sensorydeprivation")) {
      if (target.setAbility) target.setAbility("sensorydeprivation", target, abilityEffect, true);
      if (!target.hasAbility("sensorydeprivation")) target.ability = "sensorydeprivation";
    }
    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: Blind Justice");
    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 === "tosenmega" || speciesId === "tosenbankai" || speciesName === "Tosen-Mega" || speciesName === "Tosen-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Tosen" && speciesName !== "Tosen" && speciesId !== "tosen") return;
    if (pokemon.hp > pokemon.maxhp * 0.30) return;
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("blindjustice");
    this.add("-activate", pokemon, "ability: Blind Justice");
    if (!pokemon.formeChange("Tosen-Bankai", abilityEffect, true)) {
      pokemon.bleachCaptainAwakened = false;
      return;
    }
    pokemon.canMegaEvo = null;
    if (!pokemon.hasAbility("sensorydeprivation")) {
      if (pokemon.setAbility) pokemon.setAbility("sensorydeprivation", pokemon, abilityEffect, true);
      if (!pokemon.hasAbility("sensorydeprivation")) pokemon.ability = "sensorydeprivation";
    }
    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: Blind Justice");
  },
  onStart(pokemon) {
    this.add("-ability", pokemon, "Blind Justice");
    if (!pokemon.blindJusticeEvasionUsed) {
      pokemon.blindJusticeEvasionUsed = true;
      this.boost({ evasion: 1 }, pokemon, pokemon, this.dex.abilities.get("blindjustice"));
    }
    if (pokemon?.boosts?.accuracy < 0) {
      const abilityEffect = this.dex.abilities.get("blindjustice");
      this.boost({ accuracy: -pokemon.boosts.accuracy }, pokemon, pokemon, abilityEffect, true);
    }
  },
  onTryBoost(boost, target, source, effect) {
    if (!target || !target.hasAbility("blindjustice")) return;
    if (boost.accuracy && boost.accuracy < 0) {
      delete boost.accuracy;
      this.add("-activate", target, "ability: Blind Justice");
    }
  },
  onTryAddVolatile(status, target, source, effect) {
    if (!target || !target.hasAbility("blindjustice") || !status) return;
    const statusId = String(status.id || status.name || "").toLowerCase();
    if (statusId === "confusion") {
      this.add("-immune", target, "[from] ability: Blind Justice");
      return null;
    }
  },
  onModifyMove(move, pokemon, target) {
    if (!move || !pokemon || !pokemon.hasAbility("blindjustice")) return;
    move.ignoreEvasion = true;
  },
  onBasePowerPriority: 20,
  onBasePower(basePower, attacker, defender, move) {
    if (!attacker || !move || !attacker.hasAbility("blindjustice")) return;
    if (move.flags?.sound) return this.chainModify(1.3);
  }
}
