namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "bug_bite")]
public class BugBite : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var user = move.User;
        var battleData = user.BattleData;
        if (battleData == null)
            return;

        var targetHeldItem = target.HeldItem;

        if (targetHeldItem is not { Category: ItemCategory.Berry } || !target.TryStealHeldItem(out targetHeldItem))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        _ = target.ForceSetHeldItem(null);
        targetHeldItem.RunItemScript(battleData.Battle.Library.ScriptResolver, user, user, battleData.Battle,
            move.Battle.EventHook);
    }
}