import { Pipe, PipeTransform } from '@angular/core';
import { Leg } from '../models-legacy';

@Pipe({
    name: 'codesharing',
})
export class CodesharingPipe implements PipeTransform {
    transform(legOrLegs: Leg | Leg[]): string {
        if (!legOrLegs) return '';

        if (!Array.isArray(legOrLegs)) legOrLegs = [legOrLegs];

        const operators = legOrLegs
            .filter((leg) => leg.operatingBy?.operators)
            .map((leg) => leg.operatingBy?.operators)
            .reduce((a, c) => a.concat(c), []);

        return operators
            .map((operator) => `${operator.carrier} ${operator.flightNumber}`)
            .join(', ');
    }
}
