import type { Moment } from 'moment' ; import moment from 'moment-timezone' ; import findIndex from 'lodash/findIndex' ; import { timezone } from '@frogpond/constants' ; import type { DayPartMenuType , DayPartsCollectionType , ProcessedMealType , } from '../types' ; export function findMenu ( dayparts , now ) { if ( ! dayparts . length || ! dayparts [ 0 ] . length ) { return ; } const daypart = dayparts [ 0 ] ; const menuIndex = findMenuIndex ( daypart , now ) ; return daypart [ menuIndex ] ; } export function findMeal ( meals , now ) { if ( ! meals . length ) { return ; } const dayparts = meals . map ( ( m ) => ( { starttime : m . starttime , endtime : m . endtime , label : m . label , stations : m . stations , id : m . label , abbreviation : m . label , } ) ) ; const mealIndex = findMenuIndex ( dayparts , now ) ; return meals [ mealIndex ] ; } function findMenuIndex ( dayparts , now ) { if ( dayparts . length === 1 ) { return 0 ; } const times = dayparts . map ( ( { starttime , endtime } ) => ( { start : moment . tz ( starttime , 'H:mm' , true , timezone ( ) ) . dayOfYear ( now . dayOfYear ( ) ) , end : moment . tz ( endtime , 'H:mm' , true , timezone ( ) ) . dayOfYear ( now . dayOfYear ( ) ) , } ) ) ; let mealIndex = findIndex ( times , ( { end } ) => now . isSameOrBefore ( end ) ) ; if ( mealIndex === - 1 ) { mealIndex = times . length - 1 ; } return mealIndex ; }