import { RouteType } from '@typings/enums';
import { getFlightFirstLeg } from '@utils/flight/legs/first-leg';
import { IWithLeg } from '@utils/flight/legs/types';
import * as moment from 'moment';

export function getFlightDate(flight: IWithLeg) {
    if (flight.routeType === RouteType.CONNECTING) {
        return getSimpleFlightDate(flight.flights[0]);
    }

    return getSimpleFlightDate(flight);
}

function getSimpleFlightDate(flight: IWithLeg) {
    const leg = getFlightFirstLeg(flight);
    const departure = leg.departure.times.scheduledDeparture.local;

    return moment.parseZone(departure).local(true).toDate();
}

const FlightDateUtils = {
    getFlightDate,
};

export default FlightDateUtils;
