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

namespace VCommon.DataModel
{
    public static class TreeCopy
    {
        public static IEnumerable<TTo> CreateFrom<TFrom, TTo, TKey, TValue>(
            IEnumerable<TFrom> source,
            Func<TKey, TValue, IEnumerable<TTo>, TTo> create,
            Func<TFrom, TKey> keySelector,
            Func<TFrom, TValue> valueSelector,
            Func<TFrom, IEnumerable<TFrom>> childSelector)
        {
            return source.Select(p =>
                create(
                    keySelector(p),
                    valueSelector(p),
                    CreateFrom(childSelector(p), create, keySelector, valueSelector, childSelector)
                )
            );
        }


    }
}