using System;
using System.Collections.Generic;

namespace StellaOps.Feedser.Merge.Options;

internal static class AdvisoryPrecedenceTable
{
    public static IReadOnlyDictionary<string, int> Merge(
        IReadOnlyDictionary<string, int> defaults,
        AdvisoryPrecedenceOptions? options)
    {
        if (defaults is null)
        {
            throw new ArgumentNullException(nameof(defaults));
        }

        if (options?.Ranks is null || options.Ranks.Count == 0)
        {
            return defaults;
        }

        var merged = new Dictionary<string, int>(defaults, StringComparer.OrdinalIgnoreCase);
        foreach (var kvp in options.Ranks)
        {
            if (string.IsNullOrWhiteSpace(kvp.Key))
            {
                continue;
            }

            merged[kvp.Key.Trim()] = kvp.Value;
        }

        return merged;
    }
}
