using BackOffice.Common.Utilities;
using Blazored.LocalStorage;

namespace BackOffice.Common.Utilities;

public class AppTokenProvider : ITokenProvider
{
    private readonly ILocalStorageService _localStorage;
    private string _token;

    public AppTokenProvider(ILocalStorageService localStorage)
    {
        _localStorage = localStorage;
    }

    public async Task<string> GetTokenAsync()
    {
        if (_token == null)
        {
            var authorizationToken = await _localStorage.GetItemAsync<string>(GlobalConstants.JwtTokenKey);
            if (!string.IsNullOrEmpty(authorizationToken))
                _token = authorizationToken.ToString().Replace("Bearer ", "");
        }

        return _token;
    }
}