namespace ZB.MOM.WW.MxGateway.Server.Workers;

/// <summary>
/// Represents a worker process command line.
/// </summary>
public sealed class WorkerProcessCommandLine
{
    /// <summary>
    /// Initializes a command line with executable path and arguments.
    /// </summary>
    /// <param name="executablePath">Path to the worker executable.</param>
    /// <param name="arguments">Command-line arguments.</param>
    public WorkerProcessCommandLine(
        string executablePath,
        IReadOnlyList<string> arguments)
    {
        ExecutablePath = executablePath;
        Arguments = arguments;
    }

    /// <summary>
    /// Gets the path to the worker executable.
    /// </summary>
    public string ExecutablePath { get; }

    /// <summary>
    /// Gets the command-line arguments.
    /// </summary>
    public IReadOnlyList<string> Arguments { get; }

    /// <inheritdoc />
    public override string ToString()
    {
        return string.Join(
            " ",
            new[] { Quote(ExecutablePath) }.Concat(Arguments.Select(Quote)));
    }

    private static string Quote(string value)
    {
        return value.Contains(' ', StringComparison.Ordinal)
            ? $"\"{value}\""
            : value;
    }
}
