using ZB.MOM.WW.ScadaBridge.TemplateEngine;
namespace ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis;
///
/// Indirection so ScriptAnalysisService can be unit-tested without standing
/// up SharedScriptService and its EF Core repository chain.
///
public interface ISharedScriptCatalog
{
/// Returns the parameter and return shapes for all registered shared scripts.
/// A task that resolves to the list of all shared script shapes.
Task> GetShapesAsync();
///
/// Returns the source code and metadata for a named shared script, or
/// null if no shared script with that name exists. Used by Test Run to
/// compile and execute nested CallShared invocations.
///
/// Name of the shared script to retrieve.
/// Cancellation token for the async lookup.
/// A task that resolves to the matching shared script source, or null if none exists.
Task GetByNameAsync(string name, CancellationToken cancellationToken = default);
}
public record SharedScriptSource(string Name, string Code, string? ParameterDefinitions, string? ReturnDefinition);
public class SharedScriptCatalog : ISharedScriptCatalog
{
private readonly SharedScriptService _service;
///
/// Initializes a new backed by the given service.
///
/// Service providing access to shared script definitions.
public SharedScriptCatalog(SharedScriptService service) => _service = service;
///
public async Task> GetShapesAsync()
{
var scripts = await _service.GetAllSharedScriptsAsync();
return scripts
.Select(s => ScriptShapeParser.Parse(s.Name, s.ParameterDefinitions, s.ReturnDefinition))
.ToList();
}
///
public async Task GetByNameAsync(string name, CancellationToken cancellationToken = default)
{
if (string.IsNullOrEmpty(name)) return null;
var scripts = await _service.GetAllSharedScriptsAsync(cancellationToken);
var s = scripts.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.Ordinal));
return s == null ? null : new SharedScriptSource(s.Name, s.Code, s.ParameterDefinitions, s.ReturnDefinition);
}
}