{
  name: "Senbonzakura",
  rating: 5,
  flags: {},
  onStart(pokemon) {
    pokemon.byakuyaUtsusemiUsed = false;
    this.add("-ability", pokemon, "Senbonzakura");
  },
  onModifyMove(move, pokemon) {
    if (!pokemon || !move) return;
    if (move.type === "Grass" || move.type === "Fairy" || ["senbonzakura", "senka"].includes(move.id)) {
      if (move.flags?.contact) delete move.flags.contact;
      move.flags = {...move.flags, distance: 1};
    }
  },
  onBasePowerPriority: 20,
  onBasePower(basePower, pokemon, target, move) {
    if (!pokemon || !pokemon.hasAbility("senbonzakura") || !move) return;
    if (move.type === "Grass" || move.type === "Fairy" || ["senbonzakura", "senka"].includes(move.id)) return this.chainModify(1.3);
  },
  onSourceModifyDamagePriority: -5,
  onSourceModifyDamage(damage, source, target, move) {
    if (!target || !source || !move || !target.hasAbility("senbonzakura")) return;
    if (target.byakuyaUtsusemiUsed) return;
    if (!this.checkMoveMakesContact(move, source, target)) return;
    target.byakuyaUtsusemiUsed = true;
    this.add("-activate", target, "ability: Senbonzakura");
    this.boost({spe: 1}, target, target, null, true);
    return this.chainModify(0.5);
  },
  onDamagingHit(damage, target, source, move) {
    if (!target || !source || source === target || !move || !target.hasAbility("senbonzakura")) return;
    if (!this.checkMoveMakesContact(move, source, target)) return;
    this.damage(Math.max(1, Math.floor((source.maxhp || source.baseMaxhp || 1) / 16)), source, target, this.dex.abilities.get("senbonzakura"));
  },
  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 === "byakuyamega" || speciesId === "byakuyabankai" || speciesName === "Byakuya-Mega" || speciesName === "Byakuya-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Byakuya" && speciesName !== "Byakuya" && speciesId !== "byakuya") return;
    if (target.hp - damage > target.maxhp * 0.30) 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 === "byakuyamega" || speciesId === "byakuyabankai" || speciesName === "Byakuya-Mega" || speciesName === "Byakuya-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Byakuya" && speciesName !== "Byakuya" && speciesId !== "byakuya") return;
    if (pokemon.hp > pokemon.maxhp * 0.30) return;
    this.effect.awaken.call(this, pokemon);
  },
  awaken(pokemon) {
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("senbonzakura");
    this.add("-activate", pokemon, "ability: Senbonzakura");
    if (!pokemon.formeChange("Byakuya-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.byakuyaUtsusemiUsed = true;
    pokemon.byakuyaKageyoshiGuardUsed = false;
    pokemon.byakuyaHakuteikenUsed = false;
    pokemon.byakuyaHakuteikenSpent = false;
    this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Senbonzakura");
    return 0;
  },
  onSwitchOut(pokemon) {
    pokemon.byakuyaUtsusemiUsed = false;
  }
}
