// <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.Globalization;
    using System.Runtime.CompilerServices;

    [GeneratedCode("Microsoft.Extensions.Configuration.Binder.SourceGeneration", "42.42.42.42")]
    file static class BindingExtensions
    {
        #region IConfiguration extensions.
        /// <summary>Extracts the value with the specified key and converts it to the specified type.</summary>
        [InterceptsLocation(1, "FUU/7viOc6J4+bBdF0LYFmoBAABzcmMtMC5jcw==")] // src-0.cs(13,18)
        public static T? GetValue<T>(this IConfiguration configuration, string key) => (T?)(BindingExtensions.GetValueCore(configuration, typeof(T), key) ?? default(T));

        /// <summary>Extracts the value with the specified key and converts it to the specified type.</summary>
        [InterceptsLocation(1, "FUU/7viOc6J4+bBdF0LYFg4CAABzcmMtMC5jcw==")] // src-0.cs(16,24)
        [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(defaultValue))]
        public static T? GetValue<T>(this IConfiguration configuration, string key, T defaultValue) => (T?)(BindingExtensions.GetValueCore(configuration, typeof(T), key) ?? defaultValue);

        /// <summary>Extracts the value with the specified key and converts it to the specified type.</summary>
        [InterceptsLocation(1, "FUU/7viOc6J4+bBdF0LYFpcBAABzcmMtMC5jcw==")] // src-0.cs(14,24)
        public static object? GetValue(this IConfiguration configuration, Type type, string key) => BindingExtensions.GetValueCore(configuration, type, key);

        /// <summary>Extracts the value with the specified key and converts it to the specified type.</summary>
        [InterceptsLocation(1, "FUU/7viOc6J4+bBdF0LYFk4CAABzcmMtMC5jcw==")] // src-0.cs(17,24)
        [return: global::System.Diagnostics.CodeAnalysis.NotNullIfNotNull(nameof(defaultValue))]
        public static object? GetValue(this IConfiguration configuration, Type type, string key, object? defaultValue) => BindingExtensions.GetValueCore(configuration, type, key) ?? defaultValue;
        #endregion IConfiguration extensions.

        #region Core binding extensions.
        public static object? GetValueCore(this IConfiguration configuration, Type type, string key)
        {
            ArgumentNullException.ThrowIfNull(configuration);

            IConfigurationSection section = configuration.GetSection(key);

            if (TryGetConfigurationValue(section, key: null, out string? value) && !string.IsNullOrEmpty(value))
            {
                if (type == typeof(int))
                {
                    return ParseInt(value, section.Path);
                }
                else if (type == typeof(bool?))
                {
                    return ParseBool(value, section.Path);
                }
                else if (type == typeof(byte[]))
                {
                    return ParseByteArray(value, section.Path);
                }
                else if (type == typeof(global::System.Globalization.CultureInfo))
                {
                    return ParseSystemGlobalizationCultureInfo(value, section.Path);
                }
            }

            return null;
        }

        /// <summary>Tries to get the configuration value for the specified key.</summary>
        public static bool TryGetConfigurationValue(IConfiguration configuration, string key, out string? value)
        {
            if (configuration is ConfigurationSection section)
            {
                return section.TryGetValue(key, out value);
            }
        
            value = key != null ? configuration[key] : configuration is IConfigurationSection sec ? sec.Value : null;
            return value != null;
        }

        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 '{value ?? "null"}' at '{path}' to type '{typeof(int)}'.", exception);
            }
        }

        public static bool ParseBool(string value, string? path)
        {
            try
            {
                return bool.Parse(value);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(bool)}'.", exception);
            }
        }

        public static byte[] ParseByteArray(string value, string? path)
        {
            try
            {
                return Convert.FromBase64String(value);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(byte[])}'.", exception);
            }
        }

        public static global::System.Globalization.CultureInfo ParseSystemGlobalizationCultureInfo(string value, string? path)
        {
            try
            {
                return CultureInfo.GetCultureInfo(value);
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException($"Failed to convert configuration value '{value ?? "null"}' at '{path}' to type '{typeof(global::System.Globalization.CultureInfo)}'.", exception);
            }
        }
        #endregion Core binding extensions.
    }
}
