namespace Mlaumcherb.Client.Avalonia.классы;

public class ArgumentsWithPlaceholders
{
    protected List<string> _raw_args = new();

    public IEnumerable<string> FillPlaceholders(Dictionary<string, string> values)
    {
        foreach (var _s in _raw_args)
        {
            string arg = _s;
            int begin = arg.IndexOf("${", StringComparison.Ordinal);
            while(begin != -1)
            {
                int keyBegin = begin + 2;
                int end = arg.IndexOf('}', keyBegin);
                if (end != -1)
                {
                    var key = arg.Substring(keyBegin, end - keyBegin);
                    if (!values.TryGetValue(key, out var value))
                        throw new Exception($"can't find value for placeholder '{key}'");
                    arg = arg.Replace("${"+ key + "}", value);
                }
                if(end + 1 < arg.Length)
                    begin = arg.IndexOf("${", end + 1, StringComparison.Ordinal);
                else break;
            }
            yield return arg;
        }
    }
}
