using ScadaLink.TemplateEngine;

namespace ScadaLink.CentralUI.ScriptAnalysis;

/// <summary>
/// Indirection so ScriptAnalysisService can be unit-tested without standing
/// up SharedScriptService and its EF Core repository chain.
/// </summary>
public interface ISharedScriptCatalog
{
    Task<IReadOnlyList<ScriptShape>> GetShapesAsync();
}

public class SharedScriptCatalog : ISharedScriptCatalog
{
    private readonly SharedScriptService _service;

    public SharedScriptCatalog(SharedScriptService service) => _service = service;

    public async Task<IReadOnlyList<ScriptShape>> GetShapesAsync()
    {
        var scripts = await _service.GetAllSharedScriptsAsync();
        return scripts
            .Select(s => ScriptShapeParser.Parse(s.Name, s.ParameterDefinitions, s.ReturnDefinition))
            .ToList();
    }
}
