import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity';
import { AmChildren } from 'src/entities/am_children.entity';
import { sanitizeUserForApi } from '../../common/utils/sanitize-user-for-api';

/**
 * Sérialisation API fiche AM — ticket #131.
 * Expose `amChildren` actifs (date_fin null) avec enfant imbriqué, sans secrets user.
 */
export function mapAmForApi(am: AssistanteMaternelle): AssistanteMaternelle {
  const activeChildren = (am.amChildren ?? []).filter((link) => !link.date_fin);

  return {
    ...am,
    user: sanitizeUserForApi(am.user)!,
    amChildren: activeChildren.map((link) => ({
      ...link,
      child: link.child,
    })),
  };
}

export function mapAmsForApi(ams: AssistanteMaternelle[]): AssistanteMaternelle[] {
  return ams.map(mapAmForApi);
}

export function filterActiveAmChildren(links: AmChildren[] | undefined): AmChildren[] {
  return (links ?? []).filter((link) => !link.date_fin);
}
