using adas_core.Domain.Enums; using adas_core.Domain.Utils; namespace adas_core.Domain.Models; /// /// Represents a code action that encapsulates an operation or transformation to be performed on code. /// public class CodeAction { public ActionsEnum.ResourceAction Action { get; set; } public required Code Code { get; set; } /// /// Applies a list of items and returns the resulting list, using the default (null) context by delegating to the contextual overload. /// /// The list of code actions to apply. /// A with the applied changes, or null if no codes are produced. public static List? Apply(List actions) { return Apply(null, actions); } /// /// Applies a list of code actions to the source list, handling addition, deletion, and update operations while avoiding duplicates. /// Returns null if the resulting source list is empty. /// /// The list of codes to modify. If null, an empty list is used as the starting point. /// The list of code actions to apply. If null, the source list is returned unchanged. /// The modified list of codes, or null if the list is empty after applying the actions. public static List? Apply(List? source, List? 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); } }