// <auto-generated/>

#nullable enable annotations
#nullable disable warnings

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0612, CS0618

namespace System.Runtime.CompilerServices
{
    using System;
    using System.CodeDom.Compiler;

    [GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    file sealed class InterceptsLocationAttribute : Attribute
    {
        public InterceptsLocationAttribute(string filePath, int line, int column)
        {
        }
    }
}

namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
{
    using Microsoft.Extensions.Configuration;
    using System;
    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Runtime.CompilerServices;

    [GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
    file static class BindingExtensions
    {
        #region IConfiguration extensions.
        /// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
        [InterceptsLocation(@"src-0.cs", 12, 17)]
        public static T? Get<T>(this IConfiguration configuration) => (T?)(GetCore(configuration, typeof(T), configureOptions: null) ?? default(T));
        #endregion IConfiguration extensions.

        #region Core binding extensions.
        private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClassWithCustomCollections = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "CustomDictionary", "CustomList", "ICustomDictionary", "ICustomCollection", "IReadOnlyList", "UnsupportedIReadOnlyDictionaryUnsupported", "IReadOnlyDictionary" });

        public static object? GetCore(this IConfiguration configuration, Type type, Action<BinderOptions>? configureOptions)
        {
            ArgumentNullException.ThrowIfNull(configuration);

            BinderOptions? binderOptions = GetBinderOptions(configureOptions);

            if (!HasValueOrChildren(configuration))
            {
                return null;
            }

            if (type == typeof(global::Program.MyClassWithCustomCollections))
            {
                var instance = new global::Program.MyClassWithCustomCollections();
                BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions);
                return instance;
            }

            throw new NotSupportedException($"Unable to bind to type '{type}': generator did not detect the type as input.");
        }

        public static void BindCore(IConfiguration configuration, ref global::Program.CustomDictionary<string, int> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    instance[section.Key] = ParseInt(value, () => section.Path);
                }
            }
        }

        public static void BindCore(IConfiguration configuration, ref global::Program.CustomList instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    instance.Add(value);
                }
            }
        }

        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.IReadOnlyList<int> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            if (instance is not ICollection<int> temp)
            {
                return;
            }

            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    temp.Add(ParseInt(value, () => section.Path));
                }
            }
        }

        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.IReadOnlyDictionary<string, int> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            if (instance is not IDictionary<string, int> temp)
            {
                return;
            }

            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    temp[section.Key] = ParseInt(value, () => section.Path);
                }
            }
        }

        public static void BindCore(IConfiguration configuration, ref global::Program.MyClassWithCustomCollections instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            ValidateConfigurationKeys(typeof(global::Program.MyClassWithCustomCollections), s_configKeys_ProgramMyClassWithCustomCollections, configuration, binderOptions);

            if (AsConfigWithChildren(configuration.GetSection("CustomDictionary")) is IConfigurationSection section1)
            {
                global::Program.CustomDictionary<string, int>? temp3 = instance.CustomDictionary;
                temp3 ??= new global::Program.CustomDictionary<string, int>();
                BindCore(section1, ref temp3, defaultValueIfNotFound: false, binderOptions);
                instance.CustomDictionary = temp3;
            }

            if (AsConfigWithChildren(configuration.GetSection("CustomList")) is IConfigurationSection section4)
            {
                global::Program.CustomList? temp6 = instance.CustomList;
                temp6 ??= new global::Program.CustomList();
                BindCore(section4, ref temp6, defaultValueIfNotFound: false, binderOptions);
                instance.CustomList = temp6;
            }

            if (AsConfigWithChildren(configuration.GetSection("IReadOnlyList")) is IConfigurationSection section7)
            {
                global::System.Collections.Generic.IReadOnlyList<int>? temp9 = instance.IReadOnlyList;
                temp9 = temp9 is null ? (global::System.Collections.Generic.IReadOnlyList<int>)new List<int>() : (global::System.Collections.Generic.IReadOnlyList<int>)new List<int>(temp9);
                BindCore(section7, ref temp9, defaultValueIfNotFound: false, binderOptions);
                instance.IReadOnlyList = temp9;
            }

            if (AsConfigWithChildren(configuration.GetSection("IReadOnlyDictionary")) is IConfigurationSection section10)
            {
                global::System.Collections.Generic.IReadOnlyDictionary<string, int>? temp12 = instance.IReadOnlyDictionary;
                temp12 = temp12 is null ? (global::System.Collections.Generic.IReadOnlyDictionary<string, int>)new Dictionary<string, int>() : (global::System.Collections.Generic.IReadOnlyDictionary<string, int>)temp12.ToDictionary(pair => pair.Key, pair => pair.Value);
                BindCore(section10, ref temp12, defaultValueIfNotFound: false, binderOptions);
                instance.IReadOnlyDictionary = temp12;
            }
        }


        /// <summary>If required by the binder options, validates that there are no unknown keys in the input configuration object.</summary>
        public static void ValidateConfigurationKeys(Type type, Lazy<HashSet<string>> keys, IConfiguration configuration, BinderOptions? binderOptions)
        {
            if (binderOptions?.ErrorOnUnknownConfiguration is true)
            {
                List<string>? temp = null;
        
                foreach (IConfigurationSection section in configuration.GetChildren())
                {
                    if (!keys.Value.Contains(section.Key))
                    {
                        (temp ??= new List<string>()).Add($"'{section.Key}'");
                    }
                }
        
                if (temp is not null)
                {
                    throw new InvalidOperationException($"'ErrorOnUnknownConfiguration' was set on the provided BinderOptions, but the following properties were not found on the instance of {type}: {string.Join(", ", temp)}");
                }
            }
        }

        public static bool HasValueOrChildren(IConfiguration configuration)
        {
            if ((configuration as IConfigurationSection)?.Value is not null)
            {
                return true;
            }
            return AsConfigWithChildren(configuration) is not null;
        }

        public static IConfiguration? AsConfigWithChildren(IConfiguration configuration)
        {
            foreach (IConfigurationSection _ in configuration.GetChildren())
            {
                return configuration;
            }
            return null;
        }

        public static BinderOptions? GetBinderOptions(Action<BinderOptions>? configureOptions)
        {
            if (configureOptions is null)
            {
                return null;
            }
        
            BinderOptions binderOptions = new();
            configureOptions(binderOptions);
        
            if (binderOptions.BindNonPublicProperties)
            {
                throw new NotSupportedException($"The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.");
            }
        
            return binderOptions;
        }

        public static int ParseInt(string value, Func<string?> getPath)
        {
            try
            {
                return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(int)}'.", exception);
            }
        }
        #endregion Core binding extensions.
    }
}
