using adas_core.Domain.Models.MongoModels;
namespace adas_core.Domain.Utils;
///
/// Provides static extension methods for to augment its functionality.
///
///
/// This class is a static container for extension methods that extend the capabilities of the type.
///
///
public static class CardConfigExtensions
{
// Método principal para extraer todos los nombres
///
/// Retrieves all unique observation names defined within the rows and cells of the specified .
/// Returns an empty list when has no , and skips any row whose Cells collection is .
/// Observation names are extracted recursively from each cell, with duplicates removed.
///
/// The whose cell observation names should be collected.
/// A of distinct observation names found across all cells of , or an empty list if no rows are defined.
///
public static List GetAllObservationNames(this CardConfig config)
{
if (config.Rows == null) return [];
// 1. Usar SelectMany para aplanar la lista de Rows a una lista de Cells.
var allCells = config.Rows.Where(r => r.Cells != null)
.SelectMany(r => r.Cells!);
// 2. Usar SelectMany y el método recursivo para obtener todos los nombres de todas las Cells.
var observationNames = allCells.SelectMany(ExtractObservationNames)
.Distinct() // Opcional: para asegurar que los nombres sean únicos
.ToList();
return observationNames;
}
// Método auxiliar RECURSIVO para extraer nombres de una Cell y sus SubObs
///
/// Extracts observation names from the specified , yielding the values in when present and recursively collecting names from each .
///
/// The whose observation names and nested sub-observations are traversed.
/// An of observation names from the and its sub-observations.
///
private static IEnumerable ExtractObservationNames(Cell cell)
{
// 1. Si la Cell tiene ObservationName, devolver esos nombres.
if (cell.ObservationName != null)
// Retornar los elementos de la lista ObservationName
foreach (var name in cell.ObservationName)
yield return name;
// 2. Si la Cell tiene SubObs, llamar recursivamente al método para cada SubObs.
if (cell.SubObs == null) yield break;
{
foreach (var name in cell.SubObs.SelectMany(ExtractObservationNames))
yield return name;
}
}
}