using adas_core.Domain.Models.MongoModels;
namespace adas_core.Domain.Utils;
///
/// Provides extension methods for the type.
///
///
/// This is a static utility class that cannot be instantiated and is intended to extend the functionality of through extension method definitions.
///
public static class DetailConfigExtension
{
// Método principal para iniciar la extracción
///
/// Retrieves all unique observation names defined in the nurse rows of the specified .
/// Returns an empty list when is null.
///
/// The extension target whose nurse rows are inspected.
/// A containing the distinct observation names extracted from the nurse rows; an empty list when no nurse rows are defined.
///
public static List GetAllObservationNames(this CardDetailsConfig config)
{
if (config.NurseRows == null) return [];
// Se usa el método auxiliar para recorrer todas las RowDetailsConfig
var observationNames = ExtractNamesFromRows(config.NurseRows)
.Distinct() // Opcional: para nombres únicos
.ToList();
return observationNames;
}
// --- Auxiliar 1: Recorre la anidación de Filas (RowDetailsConfig) ---
///
/// Recursively extracts names from a collection of entries, traversing both the and nested of each row.
///
/// The list of instances to process.
/// An of containing all names collected from the cells and nested rows.
///
private static IEnumerable ExtractNamesFromRows(List rows)
{
foreach (var row in rows)
{
// 1. EXTRAER de las CELDAS (Cells)
if (row.Cells != null)
// Usar SelectMany para aplanar los resultados del método recursivo de Celdas
foreach (var name in row.Cells.SelectMany(ExtractNamesFromCells))
yield return name;
// 2. EXTRAER de las FILAS ANIDADAS (Rows)
if (row.Rows != null)
// Llamada recursiva: Volver a este mismo método para procesar las filas anidadas
foreach (var name in ExtractNamesFromRows(row.Rows))
yield return name;
}
}
// --- Auxiliar 2: Recorre la anidación de Celdas (CellDetails) ---
///
/// Recursively extracts every observation name from a , yielding names from the current cell as well as from all its nested . Null and null collections are safely skipped without yielding any elements.
///
/// The whose observation names, including those of its descendant cells, should be collected.
/// A lazily evaluated of containing every observation name found in and its nested cells.
///
private static IEnumerable ExtractNamesFromCells(CellDetails cell)
{
// 1. EXTRAER nombres del nivel actual
if (cell.ObservationName != null)
foreach (var name in cell.ObservationName)
yield return name;
// 2. EXTRAER de las CELDAS ANIDADAS (Cells)
if (cell.Cells != null)
// Llamada recursiva: Volver a este mismo método para procesar las celdas anidadas
foreach (var name in cell.Cells.SelectMany(ExtractNamesFromCells))
yield return name;
}
}