{
  name: "Cruel Research",
  rating: 4,
  flags: {},
  mayuriKeys(pokemon) {
    if (!pokemon) return [];
    const keys = [];
    const sideId = pokemon.side?.id || "";
    const slot = pokemon.side?.pokemon?.indexOf ? pokemon.side.pokemon.indexOf(pokemon) : -1;
    if (pokemon.uuid) keys.push(`uuid:${pokemon.uuid}`);
    if (pokemon.name) keys.push(`name:${pokemon.name}`);
    if (pokemon.set?.name) keys.push(`setname:${pokemon.set.name}`);
    if (pokemon.fullname) keys.push(`fullname:${pokemon.fullname}`);
    if (slot >= 0) keys.push(`slot:${sideId}:${slot}`);
    keys.push(String(pokemon.uuid || pokemon.name || ""));
    return [...new Set(keys.filter(Boolean))];
  },
  markMayuriLiquefactionUsed(pokemon) {
    if (!pokemon?.side) return;
    pokemon.side.mayuriLiquefactionUsed = pokemon.side.mayuriLiquefactionUsed || {};
    for (const key of this.effect.mayuriKeys.call(this, pokemon)) pokemon.side.mayuriLiquefactionUsed[key] = true;
  },
  hasMayuriLiquefactionUsed(pokemon) {
    if (!pokemon?.side?.mayuriLiquefactionUsed) return false;
    const records = pokemon.side.mayuriLiquefactionUsed;
    return this.effect.mayuriKeys.call(this, pokemon).some(key => records[key]);
  },
  isBaseMayuri(pokemon) {
    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 === "mayurimega" || speciesId === "mayuribankai" || speciesId === "mayuriliquefied" || speciesName === "Mayuri-Mega" || speciesName === "Mayuri-Bankai" || speciesName === "Mayuri-Liquefied" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return false;
    return baseSpeciesName === "Mayuri" || speciesName === "Mayuri" || speciesId === "mayuri";
  },
  armMayuriAwaitingBankai(pokemon) {
    if (!pokemon || pokemon.bleachCaptainAwakened || pokemon.transformed || pokemon.bleachCaptainLiquefied) return false;
    if (!this.effect.isBaseMayuri.call(this, pokemon)) return false;
    if (!pokemon.bleachCaptainLiquefactionUsed && !pokemon.bleachCaptainAwaitingBankai && !this.effect.hasMayuriLiquefactionUsed.call(this, pokemon)) return false;
    this.effect.markMayuriLiquefactionUsed.call(this, pokemon);
    pokemon.bleachCaptainLiquefactionUsed = true;
    pokemon.bleachCaptainAwaitingBankai = true;
    return true;
  },
  condition: {
    onStart(pokemon, source, sourceEffect) {
      const abilityEffect = this.dex?.abilities?.get("cruelresearch") || sourceEffect;
      if (!pokemon.formeChange || !pokemon.formeChange("Mayuri-Liquefied", abilityEffect, true)) return false;
      const sideId = pokemon.side?.id || "";
      const slot = pokemon.side?.pokemon?.indexOf ? pokemon.side.pokemon.indexOf(pokemon) : -1;
      const keys = [
        pokemon.uuid ? `uuid:${pokemon.uuid}` : "",
        pokemon.name ? `name:${pokemon.name}` : "",
        pokemon.set?.name ? `setname:${pokemon.set.name}` : "",
        pokemon.fullname ? `fullname:${pokemon.fullname}` : "",
        slot >= 0 ? `slot:${sideId}:${slot}` : "",
        String(pokemon.uuid || pokemon.name || "")
      ].filter(Boolean);
      pokemon.side.mayuriLiquefactionUsed = pokemon.side.mayuriLiquefactionUsed || {};
      for (const key of [...new Set(keys)]) pokemon.side.mayuriLiquefactionUsed[key] = true;
      pokemon.bleachCaptainLiquefactionUsed = true;
      pokemon.bleachCaptainLiquefied = true;
      pokemon.bleachCaptainAwaitingBankai = false;
      pokemon.bleachCaptainLiquefiedTurns = 2;
      pokemon.hp = pokemon.maxhp;
      this.add("-activate", pokemon, "ability: Cruel Research");
      this.add("-start", pokemon, "mayuriliquefied");
      this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Cruel Research");
    },
    onBeforeMovePriority: 99,
    onBeforeMove(pokemon) {
      if (!pokemon.bleachCaptainLiquefied) return;
      this.add("cant", pokemon, "mayuriliquefied");
      return false;
    },
    onDamagePriority: 1000,
    onDamage(damage, target, source, effect) {
      if (!target || target.fainted || !target.hp || typeof damage !== "number" || damage <= 0) return;
      if (target.bleachCaptainLiquefied) return 0;
      if (!target.bleachCaptainAwaitingBankai || 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 === "mayurimega" || speciesId === "mayuribankai" || speciesName === "Mayuri-Mega" || speciesName === "Mayuri-Bankai" || speciesName.endsWith("-Mega") || speciesName.endsWith("-Bankai")) return;
      if (baseSpeciesName !== "Mayuri" && speciesName !== "Mayuri" && speciesId !== "mayuri") return;
      if (target.hp - damage > target.maxhp * 0.30) return;
      const abilityEffect = this.dex?.abilities?.get("cruelresearch") || effect;
      target.bleachCaptainAwakened = true;
      target.bleachCaptainLiquefied = false;
      target.bleachCaptainAwaitingBankai = false;
      delete target.bleachCaptainLiquefiedTurns;
      if (!target.formeChange || !target.formeChange("Mayuri-Bankai", abilityEffect, true)) {
        target.bleachCaptainAwakened = false;
        target.bleachCaptainAwaitingBankai = true;
        return target.hp > 1 ? target.hp - 1 : 0;
      }
      target.canMegaEvo = null;
      if (!target.hasAbility("goldenpoisonwomb")) {
        if (target.setAbility) target.setAbility("goldenpoisonwomb", target, abilityEffect, true);
        if (!target.hasAbility("goldenpoisonwomb")) target.ability = "goldenpoisonwomb";
      }
      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);
      target.maxhp = target.volatiles?.["dynamax"] ? 2 * target.baseMaxhp : target.baseMaxhp;
      target.hp = target.maxhp;
      if (target.volatiles?.cruelresearch) delete target.volatiles.cruelresearch;
      this.add("-heal", target, target.getHealth, "[from] ability: Golden Poison Womb");
      return 0;
    },
    onSetStatus(status, target, source, effect) {
      if (!target.bleachCaptainLiquefied) return;
      this.add("-immune", target, "[from] ability: Cruel Research");
      return false;
    },
    onTryAddVolatile(status, target, source, effect) {
      if (!target.bleachCaptainLiquefied) return;
      if (!status || status.id === "cruelresearch") return;
      this.add("-immune", target, "[from] ability: Cruel Research");
      return null;
    },
    onResidualOrder: 29,
    onResidual(pokemon) {
      if (!pokemon || pokemon.fainted || !pokemon.hp || !pokemon.bleachCaptainLiquefied) return;
      const abilityEffect = this.dex?.abilities?.get("cruelresearch");
      const turns = pokemon.bleachCaptainLiquefiedTurns ?? 1;
      if (turns > 1) {
        pokemon.bleachCaptainLiquefiedTurns = turns - 1;
        pokemon.hp = pokemon.maxhp;
        return;
      }
      pokemon.bleachCaptainLiquefied = false;
      pokemon.bleachCaptainAwaitingBankai = true;
      pokemon.bleachCaptainLiquefactionUsed = true;
      delete pokemon.bleachCaptainLiquefiedTurns;
      if (String(pokemon.species?.name || "") === "Mayuri-Liquefied" && pokemon.formeChange) {
        pokemon.formeChange("Mayuri", abilityEffect, true);
      }
      if (!pokemon.hasAbility("cruelresearch")) {
        if (pokemon.setAbility) pokemon.setAbility("cruelresearch", pokemon, abilityEffect, true);
        if (!pokemon.hasAbility("cruelresearch")) pokemon.ability = "cruelresearch";
      }
      pokemon.hp = pokemon.maxhp;
      this.add("-end", pokemon, "mayuriliquefied");
      this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Cruel Research");
    },
    onEnd(pokemon) {
      const abilityEffect = this.dex?.abilities?.get("cruelresearch");
      if (pokemon.bleachCaptainLiquefied && String(pokemon.species?.name || "") === "Mayuri-Liquefied" && pokemon.formeChange) {
        pokemon.formeChange("Mayuri", abilityEffect, true);
        pokemon.hp = pokemon.maxhp;
        this.add("-end", pokemon, "mayuriliquefied");
        this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Cruel Research");
      }
      pokemon.bleachCaptainLiquefied = false;
      delete pokemon.bleachCaptainLiquefiedTurns;
      if (!pokemon.bleachCaptainAwakened && pokemon.bleachCaptainLiquefactionUsed) {
        pokemon.bleachCaptainLiquefactionUsed = true;
        pokemon.bleachCaptainAwaitingBankai = true;
      }
    }
  },
  onStart(pokemon) {
    this.effect.armMayuriAwaitingBankai.call(this, pokemon);
  },
  onDamagePriority: 1000,
  onDamage(damage, target, source, effect) {
    if (!target || target.fainted || !target.hp || typeof damage !== "number" || damage <= 0) return;
    if (!target.hasAbility("cruelresearch") || target.bleachCaptainAwakened || target.bleachCaptainLiquefied || target.transformed || target.volatiles?.cruelresearch) return;
    if (!this.effect.isBaseMayuri.call(this, target)) return;
    const abilityEffect = this.dex.abilities.get("cruelresearch");
    const liquefactionUsed = target.bleachCaptainLiquefactionUsed || this.effect.hasMayuriLiquefactionUsed.call(this, target) || target.bleachCaptainAwaitingBankai;
    if (liquefactionUsed) {
      this.effect.armMayuriAwaitingBankai.call(this, target);
    }
    if (!liquefactionUsed) {
      if (target.hp - damage > 0) return;
      target.bleachCaptainLiquefactionUsed = true;
      this.effect.markMayuriLiquefactionUsed.call(this, target);
      if (!target.addVolatile("cruelresearch", target, abilityEffect)) return target.hp > 1 ? target.hp - 1 : 0;
      return 0;
    }
    if (target.hp - damage > target.maxhp * 0.30) return;
    target.bleachCaptainAwakened = true;
    target.bleachCaptainAwaitingBankai = false;
    delete target.bleachCaptainLiquefiedTurns;
    if (!target.formeChange || !target.formeChange("Mayuri-Bankai", abilityEffect, true)) {
      target.bleachCaptainAwakened = false;
      target.bleachCaptainAwaitingBankai = true;
      return target.hp > 1 ? target.hp - 1 : 0;
    }
    target.canMegaEvo = null;
    if (!target.hasAbility("goldenpoisonwomb")) {
      if (target.setAbility) target.setAbility("goldenpoisonwomb", target, abilityEffect, true);
      if (!target.hasAbility("goldenpoisonwomb")) target.ability = "goldenpoisonwomb";
    }
    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);
    target.maxhp = target.volatiles?.["dynamax"] ? 2 * target.baseMaxhp : target.baseMaxhp;
    target.hp = target.maxhp;
    if (target.volatiles?.cruelresearch) delete target.volatiles.cruelresearch;
    this.add("-heal", target, target.getHealth, "[from] ability: Golden Poison Womb");
    return 0;
  },
  onResidualOrder: 29,
  onResidual(pokemon) {
    if (!pokemon || pokemon.fainted || !pokemon.hp) return;
    const abilityEffect = this.dex.abilities.get("cruelresearch");
    if (pokemon.bleachCaptainLiquefied && !pokemon.volatiles?.cruelresearch && !pokemon.volatiles?.mayuriliquefied) {
      pokemon.bleachCaptainLiquefied = false;
      pokemon.bleachCaptainAwaitingBankai = true;
      pokemon.bleachCaptainLiquefactionUsed = true;
      delete pokemon.bleachCaptainLiquefiedTurns;
      if (String(pokemon.species?.name || "") === "Mayuri-Liquefied" && pokemon.formeChange) {
        pokemon.formeChange("Mayuri", abilityEffect, true);
      }
      if (!pokemon.hasAbility("cruelresearch")) {
        if (pokemon.setAbility) pokemon.setAbility("cruelresearch", pokemon, abilityEffect, true);
        if (!pokemon.hasAbility("cruelresearch")) pokemon.ability = "cruelresearch";
      }
      pokemon.hp = pokemon.maxhp;
      this.add("-end", pokemon, "mayuriliquefied");
      this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Cruel Research");
      return;
    }
    if (!pokemon.hasAbility("cruelresearch") || pokemon.bleachCaptainAwakened || pokemon.bleachCaptainLiquefied || pokemon.transformed || pokemon.volatiles?.cruelresearch) return;
    if (!this.effect.isBaseMayuri.call(this, pokemon)) return;
    const liquefactionUsed = pokemon.bleachCaptainLiquefactionUsed || this.effect.hasMayuriLiquefactionUsed.call(this, pokemon) || pokemon.bleachCaptainAwaitingBankai;
    if (liquefactionUsed) {
      this.effect.armMayuriAwaitingBankai.call(this, pokemon);
    }
    if (!liquefactionUsed || pokemon.hp > pokemon.maxhp * 0.30) return;
    pokemon.bleachCaptainAwakened = true;
    pokemon.bleachCaptainAwaitingBankai = false;
    delete pokemon.bleachCaptainLiquefiedTurns;
    if (!pokemon.formeChange || !pokemon.formeChange("Mayuri-Bankai", abilityEffect, true)) {
      pokemon.bleachCaptainAwakened = false;
      pokemon.bleachCaptainAwaitingBankai = true;
      return;
    }
    pokemon.canMegaEvo = null;
    if (!pokemon.hasAbility("goldenpoisonwomb")) {
      if (pokemon.setAbility) pokemon.setAbility("goldenpoisonwomb", pokemon, abilityEffect, true);
      if (!pokemon.hasAbility("goldenpoisonwomb")) pokemon.ability = "goldenpoisonwomb";
    }
    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);
    pokemon.maxhp = pokemon.volatiles?.["dynamax"] ? 2 * pokemon.baseMaxhp : pokemon.baseMaxhp;
    pokemon.hp = pokemon.maxhp;
    if (pokemon.volatiles?.cruelresearch) delete pokemon.volatiles.cruelresearch;
    this.add("-heal", pokemon, pokemon.getHealth, "[from] ability: Golden Poison Womb");
  },
  onSwitchOut(pokemon) {
    if (!pokemon || pokemon.bleachCaptainAwakened) return;
    if (!pokemon.bleachCaptainLiquefactionUsed && !pokemon.bleachCaptainAwaitingBankai && !this.effect.hasMayuriLiquefactionUsed.call(this, pokemon)) return;
    this.effect.markMayuriLiquefactionUsed.call(this, pokemon);
    if (!pokemon.bleachCaptainLiquefied && this.effect.isBaseMayuri.call(this, pokemon)) {
      pokemon.bleachCaptainLiquefactionUsed = true;
      pokemon.bleachCaptainAwaitingBankai = true;
    }
  }
}
