
using System;
using System.Collections.Generic;
using System.Linq;

namespace StateCorrelations.Sample
{
    static class TestHelper
    {
        /// <summary>
        /// Mimics a database call that returns all the matching <see cref="ItemShippedEmailState"/> for the respective <see cref="ItemShippedEmailState.Identity"/>
        /// </summary>
        public static LoadMatchingStates<ItemShippedEmailState, ItemShippedEmailState.Identity> Load(IEnumerable<ItemShippedEmailState> savedStates)
            => identities => StatebyIdentity(
                identities,
                savedStates,
                ItemShippedEmailState.Identity.For);

        public static IReadOnlyDictionary<TIdentity, Folded<TState>> StatebyIdentity<TIdentity, TState>(
            IEnumerable<TIdentity> identities,
            IEnumerable<TState> states,
            Func<TState, TIdentity> stateIdentity)
        {
            return states
                .Select(s => new {State = s, Identity = stateIdentity(s)})
                .Where(x => identities.Any(i => i.Equals(x.Identity)))
                .ToDictionary(s => s.Identity, s => new Folded<TState> { SagaData = s.State });
        }
    }
}