using eSuite.Core.Miscellaneous;

namespace e_suite.API.Common.models;

public static class CustomFieldValueExtensions
{
    public static List<CustomFieldValues> ToCustomFieldsValues(this IEnumerable<CustomFieldValues> values)
    {
        var items = new Dictionary<GeneralIdRef, CustomFieldValues>();
        foreach (var value in values)
        {
            if (!items.ContainsKey(value.Id))
            {
                var newCustomFieldValues = new CustomFieldValues
                {
                    Id = value.Id
                };
                newCustomFieldValues.Values.AddRange(value.Values);

                items.Add(value.Id, newCustomFieldValues);
            }
        }

        var result = new List<CustomFieldValues>();
        result.AddRange(items.Values);
        return result;
    }
}