import { UTCDate } from "@date-fns/utc"; import { getDistanceBetweenAirportsInKm } from "./get-distance-between-airports"; import { getFlightDurationInMinutes } from "./get-flight-duration"; import { addMinutes, formatISO } from "date-fns"; import type { Airport } from "../../types/airport"; import { procedurallyPickItemFrom } from "../seed/procedurally-pick-item-from"; import { procedurallyGetIntegerBetween } from "../seed/procedurally-get-integer-between"; interface GetFlightTimesOptions { originAirport: Airport; destinationAirport: Airport; flightSeed: number; date: string; } interface GetDepartTimeOptions { flightSeed: number; date: string; } const possibleMinutes = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]; const possibleHours = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, ]; const getDepartTime = ({ flightSeed, date }: GetDepartTimeOptions) => { const minute = procedurallyPickItemFrom(flightSeed, possibleMinutes) .toString() .padStart(2, "0"); const hour = procedurallyPickItemFrom(flightSeed, possibleHours) .toString() .padStart(2, "0"); return new UTCDate(`${date}T${hour}:${minute}:00Z`); }; export const getFlightTimes = ({ originAirport, destinationAirport, flightSeed, date, }: GetFlightTimesOptions) => { const departTime = getDepartTime({ flightSeed, date }); const distanceBetweenAirportsInKm = getDistanceBetweenAirportsInKm({ originAirport, destinationAirport, }); const flightDurationInMinutes = getFlightDurationInMinutes({ distanceInKm: distanceBetweenAirportsInKm, delayInMinutes: procedurallyGetIntegerBetween(flightSeed, 15, 150), }); const arrivalTime = addMinutes(departTime, flightDurationInMinutes); return [formatISO(departTime), formatISO(arrivalTime)]; };