using ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;

/// <summary>
/// Parses the parameter-definitions JSON Schema written by SchemaBuilder and
/// returns the declared parameter names (and shapes). Used by script-edit
/// pages to feed the Monaco editor's Parameters["..."] context.
/// </summary>
public static class ScriptParameterNames
{
    /// <summary>
    /// Parses a parameter definitions JSON Schema and returns the declared parameter names.
    /// </summary>
    /// <param name="json">JSON Schema or legacy flat-array string; null/empty returns an empty list.</param>
    /// <returns>A read-only list of declared parameter names.</returns>
    public static IReadOnlyList<string> Parse(string? json) =>
        JsonSchemaShapeParser.ParseParameters(json)
            .Select(p => p.Name)
            .Where(s => !string.IsNullOrEmpty(s))
            .ToList();

    /// <summary>
    /// Parses a parameter definitions JSON Schema and returns the full parameter shape objects.
    /// </summary>
    /// <param name="json">JSON Schema or legacy flat-array string; null/empty returns an empty list.</param>
    /// <returns>A read-only list of parameter shape objects with name and type information.</returns>
    public static IReadOnlyList<ParameterShape> ParseShapes(string? json) =>
        JsonSchemaShapeParser.ParseParameters(json);
}
