using adas_core.Domain.Enums;
using adas_core.Domain.Utils;

namespace adas_core.Domain.Models;

public class CodeAction
{
    public ActionsEnum.ResourceAction Action { get; set; }
    public required Code Code { get; set; }

    public static List<Code>? Apply(List<CodeAction> actions)
    {
        return Apply(null, actions);
    }

    public static List<Code>? Apply(List<Code>? source, List<CodeAction>? actions)
    {
        source ??= [];
        (actions ?? []).ForEach(s =>
        {
            switch (s.Action)
            {
                case ActionsEnum.ResourceAction.Add:
                    if (!source.Contains(s.Code)) source.Add(s.Code);
                    break;
                case ActionsEnum.ResourceAction.Delete:
                    if (source.Contains(s.Code)) source.Remove(s.Code);
                    break;

                case ActionsEnum.ResourceAction.Update:
                    if (source.Contains(s.Code)) source[source.IndexOf(s.Code)] = s.Code;
                    else source.Add(s.Code);
                    break;
            }
        });
        return CollectionsUtils.NullIfEmpty(source);
    }
}