namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "core_enforcer")]
public class CoreEnforcer : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var battleData = target.BattleData;
        if (battleData == null)
            return;
        var turnChoices = battleData.Battle.PreviousTurnChoices.Last();
        var currentChoiceIndex = turnChoices.IndexOf(move.MoveChoice);
        if (currentChoiceIndex == -1 || !turnChoices.Take(currentChoiceIndex)
                .Any(choice => choice is IMoveChoice or IItemChoice && choice.User == target))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }
        target.SuppressAbility();
    }
}