{
    num: 502,
    accuracy: true,
    basePower: 0,
    category: "Status",
    name: "Ally Switch",
    pp: 15,
    priority: 1,
    flags: { metronome: 1 },
    onPrepareHit(pokemon) {
        return pokemon.addVolatile("allyswitch");
    },
    onHit(pokemon) {
        let success = true;
        if (this.format.gameType !== "doubles" && this.format.gameType !== "triples")
            success = false;
        if (pokemon.side.active.length === 3 && pokemon.position === 1)
            success = false;
        const newPosition = pokemon.position === 0 ? pokemon.side.active.length - 1 : 0;
        if (!pokemon.side.active[newPosition])
            success = false;
        if (pokemon.side.active[newPosition].fainted)
            success = false;
        if (!success) {
            this.add("-fail", pokemon, "move: Ally Switch");
            this.attrLastMove("[still]");
            return this.NOT_FAIL;
        }
        this.swapPosition(pokemon, newPosition, "[from] move: Ally Switch");
    },
    condition: {
        duration: 2,
        counterMax: 729,
        onStart() {
            this.effectState.counter = 3;
        },
        onRestart(pokemon) {
            const counter = this.effectState.counter || 1;
            this.debug("Ally Switch success chance: " + Math.round(100 / counter) + "%");
            const success = this.randomChance(1, counter);
            if (!success) {
                delete pokemon.volatiles["allyswitch"];
                return false;
            }
            if (this.effectState.counter < this.effect.counterMax) {
                this.effectState.counter *= 3;
            }
            this.effectState.duration = 2;
        }
    },
    secondary: null,
    target: "self",
    type: "Psychic",
    zMove: { boost: { spe: 2 } },
    contestType: "Clever"
}
