import { RouteType } from '@typings/enums';
import { IConnectingFlight, ISimpleFlight } from '@typings/flight/flight';

export type IWithFlightPId =
    | Pick<IConnectingFlight, 'flights' | 'routeType'>
    | Pick<ISimpleFlight, 'id' | 'routeType'>;

export function getFlightPId(flight: IWithFlightPId) {
    if (flight.routeType === RouteType.CONNECTING) {
        return flight.flights.reduce((result, current) => {
            return `${result}${current.id}`;
        }, '');
    }

    return flight.id;
}

/**
 * returns true if flights' pIds are equal
 * */
export function compareFlightsByPId(
    flight1: IWithFlightPId,
    flight2: IWithFlightPId,
) {
    return getFlightPId(flight1) === getFlightPId(flight2);
}
