import { first, isEmpty, last } from 'lodash';
import { FlightSearchResponseFlight, FlightSearchResponseFlightSegment, VendorType } from '@qite/tide-client';
import { ExtendedFlightSearchResponseItem, SortByType } from '../types';
import { DepartureRange } from '../../shared/types';

export const getFlightKey = (segments: FlightSearchResponseFlightSegment[]) => {
  if (!segments || segments.length === 0) return '';
  return segments.map((s) => `${s.marketingAirlineCode}${s.flightNumber}_${s.departureDateTime}`).join('_');
};

export const getOutwardFlight = (flightResult: ExtendedFlightSearchResponseItem): FlightSearchResponseFlight | undefined => {
  return flightResult?.outward;
};

// export const getAirportName = (airport: TideAirport | undefined, language: string): string => {
//   if (!airport || !airport.name) {
//     return "";
//   }
//   const airportName = airport.localizedNames?.find((name) => name?.languageCode === language);
//   return airportName && airportName?.value ? airportName.value : airport.name;
// };

// export const getAirportLocationName = (airport: TideAirport | undefined, language: string): string => {
//   if (!airport || !airport.locationName) {
//     return "";
//   }
//   const locationName = airport.localizedLocations?.find((name) => name?.languageCode === language);
//   return locationName && locationName?.value ? locationName.value : airport.locationName;
// };

// export const getCountryName = (country: TideCountry | undefined | null, language: string): Maybe<string> | undefined => {
//   if (!country) {
//     return "";
//   }

//   const countryName = country.localizedNames?.find((name) => name?.languageCode === language);
//   return countryName && countryName.value !== "" ? countryName.value : country.name;
// };

export const getFlightSegments = (flight: FlightSearchResponseFlight | undefined): FlightSearchResponseFlightSegment[] => {
  if (!flight) {
    return [];
  }
  return flight?.segments;
};

export const getDepartureSegment = (flight: FlightSearchResponseFlight | undefined): FlightSearchResponseFlightSegment | undefined => {
  const segments = getFlightSegments(flight);
  if (isEmpty(segments)) {
    return undefined;
  }

  return first(segments);
};

export const getArrivalSegment = (flight: FlightSearchResponseFlight | undefined): FlightSearchResponseFlightSegment | undefined => {
  const segments = getFlightSegments(flight);
  if (isEmpty(segments)) {
    return undefined;
  }

  return last(segments);
};

export const getNumberOfStops = (flight: FlightSearchResponseFlight | undefined): number => {
  const segments = getFlightSegments(flight);
  if (isEmpty(segments)) {
    return 0;
  }
  return segments.length - 1;
};

export const getNumberOfStopsLabel = (flight: FlightSearchResponseFlight | undefined, directLabel: string, stopsLabel: string, stopLabel: string): string => {
  const numberOfStops = getNumberOfStops(flight);

  if (numberOfStops === 0) {
    return directLabel;
  } else if (numberOfStops === 1) {
    return `${numberOfStops} ${stopLabel}`;
  } else {
    return `${numberOfStops} ${stopsLabel}`;
  }
};

export const getDepartureRangeName = (translations: any, range: DepartureRange | undefined): string => {
  switch (range) {
    case DepartureRange.Morning:
      return translations.FLIGHTS_FORM.MORNING_DEPARTURE;
    case DepartureRange.Afternoon:
      return translations.FLIGHTS_FORM.AFTERNOON_DEPARTURE;
    case DepartureRange.Evening:
      return translations.FLIGHTS_FORM.EVENING_DEPARTURE;
    case DepartureRange.Night:
      return translations.FLIGHTS_FORM.NIGHT_DEPARTURE;
    default:
      return '';
  }
};
