<commit_before>namespace AgileObjects.AgileMapper.ObjectPopulation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using Configuration;
    using Extensions;
    using Members;

    internal static class DerivedComplexTypeMappingsFactory
    {
        public static Expression CreateFor(IObjectMappingData declaredTypeMappingData)
        {
            var declaredTypeMapperData = declaredTypeMappingData.MapperData;

            if (declaredTypeMapperData.Context.IsForDerivedType || declaredTypeMapperData.HasSameSourceAsParent())
            {
                return Constants.EmptyExpression;
            }

            var derivedSourceTypes = GetDerivedTypes(declaredTypeMapperData.SourceType, declaredTypeMapperData);
            var derivedTargetTypes = GetDerivedTargetTypesIfNecessary(declaredTypeMappingData);
            var derivedTypePairs = GetTypePairsFor(declaredTypeMapperData, declaredTypeMapperData);

            if (derivedSourceTypes.None() && derivedTargetTypes.None() && derivedTypePairs.None())
            {
                return Constants.EmptyExpression;
            }

            var derivedTypeMappingExpressions = new List<Expression>();

            bool declaredTypeHasUnconditionalTypePair;

            AddDeclaredSourceTypeMappings(
                derivedTypePairs,
                declaredTypeMappingData,
                derivedTypeMappingExpressions,
                out declaredTypeHasUnconditionalTypePair);

            if (declaredTypeHasUnconditionalTypePair && derivedSourceTypes.None())
            {
                return derivedTypeMappingExpressions.First();
            }

            var typedObjectVariables = new List<ParameterExpression>();

            AddDerivedSourceTypeMappings(
                derivedSourceTypes,
                declaredTypeMappingData,
                typedObjectVariables,
                derivedTypeMappingExpressions);

            AddDerivedTargetTypeMappings(
                declaredTypeMappingData,
                derivedTargetTypes,
                derivedTypeMappingExpressions);

            return typedObjectVariables.Any()
                ? Expression.Block(typedObjectVariables, derivedTypeMappingExpressions)
                : Expression.Block(derivedTypeMappingExpressions);
        }

        private static ICollection<Type> GetDerivedTypes(Type parentType, IMemberMapperData mapperData)
            => mapperData.MapperContext.DerivedTypes.GetTypesDerivedFrom(parentType).ToArray();

        private static ICollection<Type> GetDerivedTargetTypesIfNecessary(IObjectMappingData mappingData)
        {
            if (mappingData.IsRoot && !mappingData.MappingContext.RuleSet.RootHasPopulatedTarget)
            {
                return Enumerable<Type>.EmptyArray;
            }

            return GetDerivedTypes(mappingData.MapperData.TargetType, mappingData.MapperData);
        }

        private static void AddDeclaredSourceTypeMappings(
            IEnumerable<DerivedTypePair> derivedTypePairs,
            IObjectMappingData declaredTypeMappingData,
            ICollection<Expression> derivedTypeMappingExpressions,<commit_msg>Skipping derived target type checks when mapping to new enumerable elements<commit_after>namespace AgileObjects.AgileMapper.ObjectPopulation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using Configuration;
    using Extensions;
    using Members;

    internal static class DerivedComplexTypeMappingsFactory
    {
        public static Expression CreateFor(IObjectMappingData declaredTypeMappingData)
        {
            var declaredTypeMapperData = declaredTypeMappingData.MapperData;

            if (declaredTypeMapperData.Context.IsForDerivedType || declaredTypeMapperData.HasSameSourceAsParent())
            {
                return Constants.EmptyExpression;
            }

            var derivedSourceTypes = GetDerivedTypes(declaredTypeMapperData.SourceType, declaredTypeMapperData);
            var derivedTargetTypes = GetDerivedTargetTypesIfNecessary(declaredTypeMappingData);
            var derivedTypePairs = GetTypePairsFor(declaredTypeMapperData, declaredTypeMapperData);

            if (derivedSourceTypes.None() && derivedTargetTypes.None() && derivedTypePairs.None())
            {
                return Constants.EmptyExpression;
            }

            var derivedTypeMappingExpressions = new List<Expression>();

            bool declaredTypeHasUnconditionalTypePair;

            AddDeclaredSourceTypeMappings(
                derivedTypePairs,
                declaredTypeMappingData,
                derivedTypeMappingExpressions,
                out declaredTypeHasUnconditionalTypePair);

            if (declaredTypeHasUnconditionalTypePair && derivedSourceTypes.None())
            {
                return derivedTypeMappingExpressions.First();
            }

            var typedObjectVariables = new List<ParameterExpression>();

            AddDerivedSourceTypeMappings(
                derivedSourceTypes,
                declaredTypeMappingData,
                typedObjectVariables,
                derivedTypeMappingExpressions);

            AddDerivedTargetTypeMappings(
                declaredTypeMappingData,
                derivedTargetTypes,
                derivedTypeMappingExpressions);

            return typedObjectVariables.Any()
                ? Expression.Block(typedObjectVariables, derivedTypeMappingExpressions)
                : Expression.Block(derivedTypeMappingExpressions);
        }

        private static ICollection<Type> GetDerivedTypes(Type parentType, IMemberMapperData mapperData)
            => mapperData.MapperContext.DerivedTypes.GetTypesDerivedFrom(parentType).ToArray();

        private static ICollection<Type> GetDerivedTargetTypesIfNecessary(IObjectMappingData mappingData)
        {
            if (mappingData.MapperData.Context.IsForNewElement)
            {
                return Enumerable<Type>.EmptyArray;
            }

            if (mappingData.IsRoot && !mappingData.MappingContext.RuleSet.RootHasPopulatedTarget)
            {
                return Enumerable<Type>.EmptyArray;
            }

            return GetDerivedTypes(mappingData.MapperData.TargetType, mappingData.MapperData);
        }

        private static void AddDeclaredSourceTypeMappings(
            IEnumerable<DerivedTypePair> derivedTypePairs,
            IObjectMappingData declaredTypeMappingData,
            ICollection<Expression> derivedTypeMappingExpressions,
{'fraction_non_alphanumeric': 0.056217030896759605, 'fraction_numerical': 0.0, 'mean_word_length': 2.537313432835821, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532271', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1451, 'n_words': 245}