using Microsoft.Extensions.Options;
using TakeoutSaaS.Module.Sms.Abstractions;
using TakeoutSaaS.Module.Sms.Options;

namespace TakeoutSaaS.Module.Sms.Services;

/// <summary>
/// 短信服务商解析器。
/// </summary>
public sealed class SmsSenderResolver(IOptionsMonitor<SmsOptions> optionsMonitor, IEnumerable<ISmsSender> senders) : ISmsSenderResolver
{
    private readonly IReadOnlyDictionary<SmsProviderKind, ISmsSender> _map = senders.ToDictionary(x => x.Provider);

    /// <inheritdoc />
    public ISmsSender Resolve(SmsProviderKind? provider = null)
    {
        var key = provider ?? optionsMonitor.CurrentValue.Provider;
        if (_map.TryGetValue(key, out var sender))
        {
            return sender;
        }

        throw new InvalidOperationException($"未注册短信服务商：{key}");
    }
}
