using System;
using BLL.Interfaces;
using MDL.Models;

namespace BLL.Services
{
    public class TagService : ITagReplacing
    {
        private readonly ITagParsing _parsing;
        private readonly ITagValidation _validation;

        public TagService(ITagParsing parsing, ITagValidation validation)
        {
            _parsing = parsing;
            _validation = validation;
        }

        public string Replace(string input, IDictionary<string,string> variables)
        {
            if (string.IsNullOrEmpty(input)) return input;
            if (!_validation.Validate(input, out var error)) throw new ArgumentException(error);

            var parsed = _parsing.Parse(input);
            foreach(var kv in variables ?? new Dictionary<string,string>())
            {
                parsed = parsed.Replace($"{{{{{kv.Key}}}}}", kv.Value);
            }
            return parsed;
        }
    }
}
