{
  name: "Sogyo no Kotowari",
  rating: 5,
  flags: {},
  onStart(pokemon) {
    this.add("-ability", pokemon, "Sogyo no Kotowari");
  },
  isUkitakeBankai(pokemon) {
    const speciesId = (pokemon.species?.id || "").toLowerCase();
    const speciesName = String(pokemon.species?.name || "");
    return pokemon.bleachCaptainAwakened || speciesId === "ukitakemega" || speciesId === "ukitakebankai" || speciesName === "Ukitake-Mega" || speciesName === "Ukitake-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai");
  },
  onDamagePriority: -100,
  onDamage(damage, target, source, effect) {
    if (!target || target.fainted || !target.hp || typeof damage !== "number") return;
    if (target.hasAbility("sogyonokotowari") && this.effect.isUkitakeBankai(target) && !target.ukitakeBankaiLethalReflectUsed && source && source !== target && !source.fainted && target.hp - damage <= 0) {
      const effectType = String(effect?.effectType || "");
      const category = String(effect?.category || "");
      if (effectType === "Move" || category && category !== "Status") {
        target.ukitakeBankaiLethalReflectUsed = true;
        const abilityEffect = this.dex.abilities.get("sogyonokotowari");
        this.add("-activate", target, "ability: Sogyo no Kotowari");
        if (effect?.type) target.ukitakeStoredType = effect.type;
        this.damage(Math.max(1, Math.floor(damage)), source, target, abilityEffect);
        return 0;
      }
    }
    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 === "ukitakemega" || speciesId === "ukitakebankai" || speciesName === "Ukitake-Mega" || speciesName === "Ukitake-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
    if (baseSpeciesName !== "Ukitake" && speciesName !== "Ukitake" && speciesId !== "ukitake") return;
    if (target.hp - damage > target.maxhp * 0.30) return;
    return this.effect.awaken.call(this, target);
  },
  onSourceModifyDamagePriority: -5,
  onSourceModifyDamage(damage, source, target, move) {
    if (!target || !source || !move) return;
    if (!target.hasAbility("sogyonokotowari")) return;
    if (move.category !== "Special") return;
    this.add("-activate", target, "ability: Sogyo no Kotowari");
    return this.chainModify(0.75);
  },
  onDamagingHit(damage, target, source, move) {
    if (!target || target.fainted || !source || !move) return;
    if (!target.hasAbility("sogyonokotowari")) return;
    if (move.category !== "Special") return;
    target.ukitakeStoredType = move.type || "Psychic";
    target.addVolatile("reishicharge", source, move);
  },
  onBasePowerPriority: 20,
  onBasePower(basePower, pokemon, target, move) {
    if (!pokemon || !move) return;
    if (!pokemon.hasAbility("sogyonokotowari")) return;
    if (!pokemon.volatiles?.reishicharge) return;
    if (move.category !== "Special") return;
    if (String(move.id || "").toLowerCase() === "twinfishreversal") return;
    return this.chainModify(1.5);
  },
  onAfterMoveSecondarySelf(pokemon, target, move) {
    if (!pokemon || !move) return;
    if (!pokemon.hasAbility("sogyonokotowari")) return;
    if (!pokemon.volatiles?.reishicharge) return;
    if (move.category !== "Special") return;
    pokemon.removeVolatile("reishicharge");
    pokemon.ukitakeStoredType = null;
  },
  onResidualOrder: 28,
  onResidual(pokemon) {
    if (!pokemon || pokemon.fainted || !pokemon.hp) return;
    if (!pokemon.hasAbility("sogyonokotowari")) return;
    if (!pokemon.bleachCaptainAwakened && !pokemon.transformed) {
      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 || "");
      const alreadyBankai = speciesId === "ukitakemega" || speciesId === "ukitakebankai" || speciesName === "Ukitake-Mega" || speciesName === "Ukitake-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai");
      const isBaseUkitake = baseSpeciesName === "Ukitake" || speciesName === "Ukitake" || speciesId === "ukitake";
      if (!alreadyBankai && isBaseUkitake && pokemon.hp <= pokemon.maxhp * 0.30) {
        this.effect.awaken.call(this, pokemon);
        return;
      }
    }
    if (pokemon.hp > pokemon.maxhp / 2) return;
    if (pokemon.volatiles?.reishicharge) return;
    const abilityEffect = this.dex.abilities.get("sogyonokotowari");
    this.add("-activate", pokemon, "ability: Sogyo no Kotowari");
    this.damage(Math.max(1, Math.floor(pokemon.maxhp / 16)), pokemon, pokemon, abilityEffect);
  },
  awaken(pokemon) {
    pokemon.bleachCaptainAwakened = true;
    const abilityEffect = this.dex.abilities.get("sogyonokotowari");
    this.add("-activate", pokemon, "ability: Sogyo no Kotowari");
    if (!pokemon.formeChange("Ukitake-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.ukitakeStoredType = null;
    if (pokemon.volatiles?.reishicharge) pokemon.removeVolatile("reishicharge");
    this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Sogyo no Kotowari");
    return 0;
  }
}
