{
  name: "Battle Maniac",
  rating: 5,
  flags: {},
  foeDebuffKey(target) {
    return target.uuid || `${target.side?.id || ""}:${target.position}:${target.species?.id || target.name || ""}`;
  },
  applyFoeDebuff(source, target) {
    if (!source || source.fainted || !target || target.fainted) return;
    source.kenpachiBattleManiacDebuffed = source.kenpachiBattleManiacDebuffed || {};
    const key = this.effect.foeDebuffKey(target);
    if (source.kenpachiBattleManiacDebuffed[key] === this.turn) return;
    source.kenpachiBattleManiacDebuffed[key] = this.turn;
    this.boost({ atk: -1 }, target, source, this.dex.abilities.get("battlemaniac"));
  },
  onStart(pokemon) {
    pokemon.kenpachiHitTurn = -1;
    this.add("-ability", pokemon, "Battle Maniac");
    for (const target of pokemon.foes()) this.effect.applyFoeDebuff.call(this, pokemon, target);
  },
  onFoeSwitchIn(target) {
    const source = this.effectState && this.effectState.target;
    this.effect.applyFoeDebuff.call(this, source, target);
  },
  onBoost(boost, target, source, effect) {
    if (boost.atk && boost.atk < 0) delete boost.atk;
  },
  onFlinch(pokemon) {
    return false;
  },
  onBasePowerPriority: 20,
  onBasePower(basePower, pokemon, target, move) {
    if (!pokemon || !pokemon.hasAbility("battlemaniac") || !move || move.category === "Status") return;
    return this.chainModify(0.85);
  },
  onDamagingHit(damage, target, source, move) {
    if (!target || !source || !move || !damage || !target.hasAbility("battlemaniac")) return;
    if (target.kenpachiHitTurn === this.turn) return;
    target.kenpachiHitTurn = this.turn;
    this.boost({ atk: 1 }, target, target, this.dex.abilities.get("battlemaniac"));
    const maxhp = target.maxhp || target.baseMaxhp || 1;
    if (damage >= maxhp / 4) this.boost({ spe: 1 }, target, target, this.dex.abilities.get("battlemaniac"));
  },
  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 === "kenpachimega" || speciesId === "kenpachibankai" || speciesName === "Kenpachi-Mega" || speciesName === "Kenpachi-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Kenpachi" && speciesName !== "Kenpachi" && speciesId !== "kenpachi") return;
    if (target.hp - damage > target.maxhp * 0.50) return;
    return this.effect.awaken.call(this, target);
  },
  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 === "kenpachimega" || speciesId === "kenpachibankai" || speciesName === "Kenpachi-Mega" || speciesName === "Kenpachi-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Kenpachi" && speciesName !== "Kenpachi" && speciesId !== "kenpachi") return;
    if (pokemon.hp > pokemon.maxhp * 0.50) return;
    this.effect.awaken.call(this, pokemon);
  },
  awaken(pokemon) {
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("battlemaniac");
    this.add("-activate", pokemon, "ability: Battle Maniac");
    if (!pokemon.formeChange("Kenpachi-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;
    pokemon.kenpachiHitTurn = -1;
    this.boost({ atk: 1, spe: 1 }, pokemon, pokemon, abilityEffect);
    this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Battle Maniac");
    return 0;
  }
}
