{
  name: "Katen Kyokotsu",
  rating: 5,
  flags: {},
  gameMoves: { bushogoma: 1, kageoni: 1, takaoni: 1, irooni: 1 },
  onStart(pokemon) {
    pokemon.shunsuiKageoniCounterUsed = false;
    pokemon.shunsuiLastGameMove = "";
    pokemon.shunsuiRepeatGamePenalty = "";
    pokemon.shunsuiGamesWon = pokemon.shunsuiGamesWon || {};
    pokemon.shunsuiGameMaster = !!pokemon.shunsuiGameMaster;
    pokemon.shunsuiFinalGameUsed = !!pokemon.shunsuiFinalGameUsed;
    if (!pokemon.terastallized) pokemon.setType(["Dark", "Flying"]);
    this.add("-ability", pokemon, "Katen Kyokotsu");
  },
  onSwitchOut(pokemon) {
    pokemon.shunsuiKageoniCounterUsed = false;
    pokemon.shunsuiRepeatGamePenalty = "";
    pokemon.shunsuiLastGameMove = "";
    if (!pokemon.terastallized) pokemon.setType(["Dark", "Flying"]);
  },
  onBeforeMove(pokemon, target, move) {
    if (!pokemon || !move || !pokemon.hasAbility("katenkyokotsu")) return;
    const id = String(move.id || "").toLowerCase();
    if (!this.effect.gameMoves[id]) return;
    pokemon.shunsuiRepeatGamePenalty = pokemon.shunsuiLastGameMove === id ? id : "";
    pokemon.shunsuiLastGameMove = id;
    this.add("-activate", pokemon, "ability: Katen Kyokotsu");
  },
  onModifyPriority(priority, pokemon, target, move) {
    if (!pokemon || !move || !pokemon.hasAbility("katenkyokotsu")) return;
    if (!pokemon.shunsuiGameMaster) return;
    const id = String(move.id || "").toLowerCase();
    if (!this.effect.gameMoves[id]) return;
    return priority + 1;
  },
  onModifyMove(move, pokemon, target) {
    if (!pokemon || !move || !pokemon.hasAbility("katenkyokotsu")) return;
    const id = String(move.id || "").toLowerCase();
    if (!this.effect.gameMoves[id]) return;
    move.ignoreEvasion = true;
  },
  onBasePowerPriority: 30,
  onBasePower(basePower, pokemon, target, move) {
    if (!pokemon || !move || !pokemon.hasAbility("katenkyokotsu")) return;
    const id = String(move.id || "").toLowerCase();
    if (!this.effect.gameMoves[id]) return;
    const won = pokemon.shunsuiGamesWon || {};
    const score = Math.min(4, Object.keys(won).length);
    let mult = 1 + score * 0.1;
    if (pokemon.shunsuiGameMaster) mult += 0.1;
    if (pokemon.shunsuiRepeatGamePenalty === id) mult *= pokemon.shunsuiGameMaster ? 0.85 : 0.75;
    if (mult !== 1) return this.chainModify(mult);
  },
  onSourceModifyDamagePriority: -4,
  onSourceModifyDamage(damage, source, target, move) {
    if (!target || !source || source === target || !move) return;
    if (!target.hasAbility("katenkyokotsu")) return;
    if (!move.flags || !move.flags.contact) return;
    if (target.shunsuiKageoniCounterUsed) return;
    return this.chainModify(target.shunsuiGameMaster ? 0.5 : 0.65);
  },
  onDamage(damage, target, source, effect) {
    if (!target || !target.hasAbility("katenkyokotsu")) return;
    if (!source || source === target) return;
    if (!effect || effect.effectType !== "Move") return;
    if (target.shunsuiFinalGameUsed) return;
    if (damage < target.hp) return;
    target.shunsuiFinalGameUsed = true;
    target.shunsuiGameMaster = true;
    if (!target.terastallized) target.setType(["Dark", "Ghost"]);
    this.add("-activate", target, "ability: Katen Kyokotsu");
    source.addVolatile("shadowmark", target, this.dex.moves.get("shadowmark"));
    source.addVolatile("confusion", target);
    const desired = Math.max(1, Math.floor(target.maxhp / 2));
    if (target.hp > desired) return target.hp - desired;
    this.heal(desired - target.hp, target, target, this.dex.abilities.get("katenkyokotsu"));
    return false;
  },
  onDamagingHit(damage, target, source, move) {
    if (!target || !source || source === target || !move || !damage) return;
    if (!target.hasAbility("katenkyokotsu")) return;
    if (!move.flags || !move.flags.contact) return;
    if (target.shunsuiKageoniCounterUsed) return;
    target.shunsuiKageoniCounterUsed = true;
    if (!target.terastallized) target.setType(["Dark", "Ghost"]);
    this.add("-activate", target, "ability: Katen Kyokotsu");
    source.addVolatile("shadowmark", target, this.dex.moves.get("shadowmark"));
    this.damage(Math.max(1, Math.floor(source.maxhp / 8)), source, target, this.dex.abilities.get("katenkyokotsu"));
    const heal = target.shunsuiGameMaster ? Math.floor(target.maxhp / 6) : Math.floor(target.maxhp / 8);
    this.heal(Math.max(1, heal), target, target, this.dex.abilities.get("katenkyokotsu"));
  }
}
