// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace TypeAgent.Common;

public static class DictionaryExtensions
{
    public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary)
    {
        return dictionary is null || dictionary.Count == 0;
    }

    public static TValue GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default)
    {
        return dictionary.TryGetValue(key, out TValue? value) ? value : defaultValue;
    }

    // Gets values in the same order as the keys
    public static IEnumerable<TValue> GetValues<TKey, TValue>(
        this IDictionary<TKey, TValue> dictionary,
        IEnumerable<TKey> keysInOrder
    )
    {
        ArgumentVerify.ThrowIfNull(keysInOrder, nameof(keysInOrder));

        foreach (TKey key in keysInOrder)
        {
            yield return dictionary[key];
        }
    }
}
