/**
 * L'API renvoie lfaIns tantôt comme un objet instrument unique,
 * tantôt comme un tableau pouvant contenir des entrées nulles ou
 * incomplètes. On normalise toujours vers un tableau propre.
 */

export interface NormalizedInstrument {
  insId: number | string;
  insNom: string;
}

export function normalizeInstruments(lfaIns: unknown): NormalizedInstrument[] {
  const list = Array.isArray(lfaIns) ? lfaIns : lfaIns ? [lfaIns] : [];
  return list.filter(
    (ins): ins is NormalizedInstrument =>
      Boolean(ins && typeof ins === 'object' && (ins as NormalizedInstrument).insNom)
  );
}
