{
    onStart(pokemon) {
      const item = pokemon.getItem();
      const hasDarmanitanite = item?.id === "darmanitanitez";
      if (hasDarmanitanite) {
        pokemon.isDynamaxed = false;
        pokemon.canDynamax = false;
        pokemon.canTerastallize = false;
      }
    },
    onResidualOrder: 29,
    onResidual(pokemon) {
      if (pokemon.baseSpecies.baseSpecies !== "Darmanitan" || pokemon.transformed) {
        return;
      }
      if (pokemon.hp <= pokemon.maxhp / 2 && !["Zen", "Galar-Zen"].includes(pokemon.species.forme)) {
        pokemon.addVolatile("zenmode");
        const item = pokemon.getItem();
        const hasDarmanitanite = item?.id === "darmanitanitez";
        const megaSpecies = this.dex.species.get("darmanitan-mega-zen");
        if (hasDarmanitanite && megaSpecies && megaSpecies.exists) {
          if (pokemon.terastallized) {
            pokemon.terastallized = null;
            pokemon.setType(pokemon.baseSpecies.types);
            this.add(
              "-start",
              pokemon,
              "typechange",
              pokemon.getTypes().join("/"),
              "[silent]"
            );
          }
          if (pokemon.volatiles["dynamax"]) {
            pokemon.removeVolatile("dynamax");
            pokemon.isDynamaxed = false;
            pokemon.canDynamax = false;
            pokemon.maxhp = pokemon.baseMaxhp;
            if (pokemon.hp > pokemon.maxhp) pokemon.hp = pokemon.maxhp;
            this.add("-heal", pokemon, pokemon.getHealth, "[silent]");
          }
  
          pokemon.canTerastallize = false;
          pokemon.canDynamax = false;
          pokemon.maxMoves = null;
          pokemon.canMegaEvo = megaSpecies.name || "Darmanitan-Mega-Zen";
        }
      } else if (pokemon.hp > pokemon.maxhp / 2 && ["Zen", "Galar-Zen"].includes(pokemon.species.forme)) {
        pokemon.addVolatile("zenmode");
        pokemon.removeVolatile("zenmode");
      }
    },
    onEnd(pokemon) {
      if (!pokemon.volatiles["zenmode"] || !pokemon.hp)
        return;
      pokemon.transformed = false;
      delete pokemon.volatiles["zenmode"];
      if (pokemon.species.baseSpecies === "Darmanitan" && pokemon.species.battleOnly) {
        pokemon.formeChange(pokemon.species.battleOnly, this.effect, false, "[silent]");
      }
    },
    condition: {
      onStart(pokemon) {
        if (!pokemon.species.name.includes("Galar")) {
          if (pokemon.species.id !== "darmanitanzen")
            pokemon.formeChange("Darmanitan-Zen");
        } else {
          if (pokemon.species.id !== "darmanitangalarzen")
            pokemon.formeChange("Darmanitan-Galar-Zen");
        }
      },
      onEnd(pokemon) {
        if (["Zen", "Galar-Zen"].includes(pokemon.species.forme)) {
          pokemon.formeChange(pokemon.species.battleOnly);
        }
      }
    },
    flags: { failroleplay: 1, noreceiver: 1, noentrain: 1, notrace: 1, failskillswap: 1, cantsuppress: 1 },
    name: "Zen Mode",
    rating: 0,
    num: 161
}