// <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.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, 38)]
        public static T? Get<T>(this IConfiguration configuration) => (T?)(GetCore(configuration, typeof(T), configureOptions: null) ?? default(T));

        /// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
        [InterceptsLocation(@"src-0.cs", 14, 36)]
        public static T? Get<T>(this IConfiguration configuration, Action<BinderOptions>? configureOptions) => (T?)(GetCore(configuration, typeof(T), configureOptions) ?? default(T));

        /// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
        [InterceptsLocation(@"src-0.cs", 13, 56)]
        public static object? Get(this IConfiguration configuration, Type type) => GetCore(configuration, type, configureOptions: null);

        /// <summary>Attempts to bind the configuration instance to a new instance of type T.</summary>
        [InterceptsLocation(@"src-0.cs", 15, 47)]
        public static object? Get(this IConfiguration configuration, Type type, Action<BinderOptions>? configureOptions) => GetCore(configuration, type, configureOptions);
        #endregion IConfiguration extensions.

        #region Core binding extensions.
        private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClass = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "MyString", "MyInt", "MyList", "MyArray", "MyDictionary" });
        private readonly static Lazy<HashSet<string>> s_configKeys_ProgramMyClass2 = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "MyInt" });

        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.MyClass))
            {
                var instance = new global::Program.MyClass();
                BindCore(configuration, ref instance, defaultValueIfNotFound: true, binderOptions);
                return instance;
            }
            else if (type == typeof(global::Program.MyClass2))
            {
                var instance = new global::Program.MyClass2();
                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::System.Collections.Generic.List<int> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (section.Value is string value)
                {
                    instance.Add(ParseInt(value, () => section.Path));
                }
            }
        }

        public static void BindCore(IConfiguration configuration, ref int[] instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            var temp2 = new List<int>();

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

            int originalCount = instance.Length;
            Array.Resize(ref instance, originalCount + temp2.Count);
            temp2.CopyTo(instance, originalCount);
        }

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

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

            if (configuration["MyString"] is string value3)
            {
                instance.MyString = value3;
            }

            if (configuration["MyInt"] is string value4)
            {
                instance.MyInt = ParseInt(value4, () => configuration.GetSection("MyInt").Path);
            }
            else if (defaultValueIfNotFound)
            {
                instance.MyInt = default;
            }

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

            if (AsConfigWithChildren(configuration.GetSection("MyArray")) is IConfigurationSection section8)
            {
                int[]? temp10 = instance.MyArray;
                temp10 ??= new int[0];
                BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions);
                instance.MyArray = temp10;
            }

            if (AsConfigWithChildren(configuration.GetSection("MyDictionary")) is IConfigurationSection section11)
            {
                global::System.Collections.Generic.Dictionary<string, string>? temp13 = instance.MyDictionary;
                temp13 ??= new global::System.Collections.Generic.Dictionary<string, string>();
                BindCore(section11, ref temp13, defaultValueIfNotFound: false, binderOptions);
                instance.MyDictionary = temp13;
            }
        }

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

            if (configuration["MyInt"] is string value14)
            {
                instance.MyInt = ParseInt(value14, () => configuration.GetSection("MyInt").Path);
            }
            else if (defaultValueIfNotFound)
            {
                instance.MyInt = default;
            }
        }


        /// <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.
    }
}
