﻿using System;
using System.Reactive.Linq;

using MoreCollections;
using static MoreCollections.Utility;

namespace LiveLinq.Dictionary
{
    public static partial class Extensions
    {
        public static IDictionaryChanges<TKeyResult, TValueResult> Select<TKeySource, TValueSource, TKeyResult, TValueResult>(
            this IDictionaryChanges<TKeySource, TValueSource> source,
            Func<TKeySource, TValueSource, IKeyValuePair<TKeyResult, TValueResult>> selector)
        {
            return source.SelectMany((key, value) => Observable.Return(Utility.DictionaryAdd(selector(key, value))).ToLiveLinq());
        }

        public static IDictionaryChanges<TKeySource, TValueResult> SelectValue<TKeySource, TValueSource, TValueResult>(
            this IDictionaryChanges<TKeySource, TValueSource> source,
            Func<TKeySource, TValueSource, TValueResult> selector)
        {
            return source.SelectMany((key, value) => Observable.Return(Utility.DictionaryAdd(KeyValuePair(key, selector(key, value)))).ToLiveLinq());
        }

        public static IDictionaryChanges<TKeyResult, TValueSource> SelectKey<TKeySource, TValueSource, TKeyResult>(
            this IDictionaryChanges<TKeySource, TValueSource> source,
            Func<TKeySource, TValueSource, TKeyResult> selector)
        {
            return source.SelectMany((key, value) => Observable.Return(Utility.DictionaryAdd(KeyValuePair(selector(key, value), value))).ToLiveLinq());
        }
    }
}
