// <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(int version, string data)
        {
        }
    }
}

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 given object instance to configuration values by matching property names against configuration keys recursively.</summary>
        [InterceptsLocation(1, "/TzDbopkyui/vWzNJfmpq0IBAABzcmMtMC5jcw==")] // src-0.cs(12,14)
        public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance)
        {
            ArgumentNullException.ThrowIfNull(configuration);

            if (instance is null)
            {
                return;
            }

            var typedObj = (global::Program.MyClass)instance;
            BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, binderOptions: null);
        }

        /// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
        [InterceptsLocation(1, "/TzDbopkyui/vWzNJfmpq2YBAABzcmMtMC5jcw==")] // src-0.cs(13,20)
        public static void Bind_ProgramMyClass(this IConfiguration configuration, object? instance, Action<BinderOptions>? configureOptions)
        {
            ArgumentNullException.ThrowIfNull(configuration);

            if (instance is null)
            {
                return;
            }

            var typedObj = (global::Program.MyClass)instance;
            BindCore(configuration, ref typedObj, defaultValueIfNotFound: false, GetBinderOptions(configureOptions));
        }

        /// <summary>Attempts to bind the given object instance to configuration values by matching property names against configuration keys recursively.</summary>
        [InterceptsLocation(1, "/TzDbopkyui/vWzNJfmpq5oBAABzcmMtMC5jcw==")] // src-0.cs(14,20)
        public static void Bind_ProgramMyClass(this IConfiguration configuration, string key, object? instance)
        {
            ArgumentNullException.ThrowIfNull(configuration);

            if (instance is null)
            {
                return;
            }

            var typedObj = (global::Program.MyClass)instance;
            BindCore(configuration.GetSection(key), ref typedObj, defaultValueIfNotFound: false, binderOptions: null);
        }
        #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", "MyDictionary", "MyComplexDictionary" });

        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 && !string.IsNullOrEmpty(value))
                {
                    instance.Add(ParseInt(value, section.Path));
                }
            }
        }

        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::System.Collections.Generic.Dictionary<string, global::Program.MyClass2> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (!(instance.TryGetValue(section.Key, out global::Program.MyClass2? element) && element is not null))
                {
                    element = new global::Program.MyClass2();
                }
                instance[section.Key] = element;
            }
        }

        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 value0)
            {
                instance.MyString = value0;
            }
            else if (defaultValueIfNotFound)
            {
                var currentValue = instance.MyString;
                if (currentValue is not null)
                {
                    instance.MyString = currentValue;
                }
            }

            if (configuration["MyInt"] is string value1 && !string.IsNullOrEmpty(value1))
            {
                instance.MyInt = ParseInt(value1, configuration.GetSection("MyInt").Path);
            }
            else if (defaultValueIfNotFound)
            {
                instance.MyInt = instance.MyInt;
            }

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

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

            if (AsConfigWithChildren(configuration.GetSection("MyComplexDictionary")) is IConfigurationSection section8)
            {
                global::System.Collections.Generic.Dictionary<string, global::Program.MyClass2>? temp10 = instance.MyComplexDictionary;
                temp10 ??= new global::System.Collections.Generic.Dictionary<string, global::Program.MyClass2>();
                BindCore(section8, ref temp10, defaultValueIfNotFound: false, binderOptions);
                instance.MyComplexDictionary = temp10;
            }
            else
            {
                instance.MyComplexDictionary = instance.MyComplexDictionary;
            }
        }


        /// <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 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, string? path)
        {
            try
            {
                return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value at '{path}' to type '{typeof(int)}'.", exception);
            }
        }
        #endregion Core binding extensions.
    }
}
