using Mapster;
using MapsterMapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using System.Reflection;

namespace Znyc.CloudCar.Core.Config
{
    /// <summary>
    /// Mapster
    /// </summary>
    public static class MapsterSetup
    {
        public static void AddMapsterSetup(this IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }
            Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
                .Where(x => x.Name.StartsWith("Znyc.CloudCar"))
                .Select(x => Assembly.Load(new AssemblyName(x.Name))).ToArray();
            services.AddScoped<IMapper>(sp => new Mapper());
            TypeAdapterConfig.GlobalSettings.Scan(assemblies);
        }
    }
}