import { latLngBounds } from 'leaflet'
import moment from 'moment'
import coreUtils from '@opentripplanner/core-utils'

import { WEEKDAYS, WEEKEND_DAYS } from './monitored-trip'

export function getLeafletItineraryBounds (itinerary) {
  return latLngBounds(coreUtils.itinerary.getItineraryBounds(itinerary))
}

/**
 * Return a leaflet LatLngBounds object that encloses the given leg's geometry.
 */
export function getLeafletLegBounds (leg) {
  return latLngBounds(coreUtils.itinerary.getLegBounds(leg))
}

export function isBatchRoutingEnabled (otpConfig) {
  return Boolean(otpConfig.routingTypes.find(t => t.key === 'BATCH'))
}

/**
 * Determines whether the specified Itinerary can be monitored.
 * @returns true if at least one Leg of the specified Itinerary is a transit leg,
 *   and none of the legs is a rental or ride hail leg (e.g. CAR_RENT, CAR_HAIL, BICYCLE_RENT, etc.).
 *   (We use the corresponding fields returned by OTP to get transit legs and rental/ride hail legs.)
 */
export function itineraryCanBeMonitored (itinerary) {
  let hasTransit = false
  let hasRentalOrRideHail = false

  if (itinerary && itinerary.legs) {
    for (const leg of itinerary.legs) {
      if (leg.transitLeg) {
        hasTransit = true
      }
      if (leg.rentedBike ||
          leg.rentedCar ||
          leg.rentedVehicle ||
          leg.hailedCar) {
        hasRentalOrRideHail = true
      }
    }
  }

  return hasTransit && !hasRentalOrRideHail
}

/**
 * Get the first stop ID from the itinerary in the underscore format required by
 * the startTransitStopId query param (e.g., TRIMET_12345 instead of TRIMET:12345).
 */
export function getFirstStopId (itinerary) {
  return getFirstTransitLeg(itinerary)?.from.stopId.replace(':', '_')
}

export function getMinutesUntilItineraryStart (itinerary) {
  return moment(itinerary.startTime).diff(moment(), 'minutes')
}

/**
 * Gets the first transit leg of the given itinerary, or null if none found.
 */
function getFirstTransitLeg (itinerary) {
  return itinerary?.legs?.find(leg => leg.transitLeg)
}

/**
 * Returns the set of monitored days that will be initially shown to the user
 * for the given itinerary.
 * @param itinerary The itinerary from which the default monitored days are extracted.
 * @returns ['monday' thru 'friday'] if itinerary happens on a weekday(*),
 *          ['saturday', 'sunday'] if itinerary happens on a saturday/sunday(*).
 * (*) For transit itineraries, the first transit leg is used to make
 * the determination. Otherwise, the itinerary startTime is used.
 */
export function getItineraryDefaultMonitoredDays (itinerary) {
  const firstTransitLeg = getFirstTransitLeg(itinerary)
  const startMoment = firstTransitLeg
    ? moment(firstTransitLeg.serviceDate, 'YYYYMMDD')
    : moment(itinerary.startTime)
  const dayOfWeek = startMoment.day()

  return (dayOfWeek === 0 || dayOfWeek === 6)
    ? WEEKEND_DAYS
    : WEEKDAYS
}

@@ -36,36 +36,8 @@
 et'%0A
-import moment from 'moment'%0A
 impo
@@ -83,16 +83,44 @@
 e-utils'
+%0Aimport moment from 'moment'
 %0A%0Aimport
@@ -213,17 +213,16 @@
 ryBounds
- 
 (itinera
@@ -426,17 +426,16 @@
 egBounds
- 
 (leg) %7B%0A
@@ -535,17 +535,16 @@
 gEnabled
- 
 (otpConf
@@ -594,17 +594,19 @@
 es.find(
-t
+(t)
  =%3E t.ke
@@ -1021,17 +1021,16 @@
 onitored
- 
 (itinera
@@ -1240,24 +1240,33 @@
 %7D%0A      if (
+%0A        
 leg.rentedBi
@@ -1271,34 +1271,32 @@
 Bike %7C%7C%0A        
- 
- 
 leg.rentedCar %7C%7C
@@ -1292,26 +1292,24 @@
 entedCar %7C%7C%0A
-  
         leg.
@@ -1329,26 +1329,24 @@
  %7C%7C%0A        
-  
 leg.hailedCa
@@ -1346,16 +1346,23 @@
 ailedCar
+%0A      
 ) %7B%0A    
@@ -1462,299 +1462,8 @@
 %0A%7D%0A%0A
-/**%0A * Get the first stop ID from the itinerary in the underscore format required by%0A * the startTransitStopId query param (e.g., TRIMET_12345 instead of TRIMET:12345).%0A */%0Aexport function getFirstStopId (itinerary) %7B%0A  return getFirstTransitLeg(itinerary)?.from.stopId.replace(':', '_')%0A%7D%0A%0A
 expo
@@ -1503,17 +1503,16 @@
 aryStart
- 
 (itinera
@@ -1695,17 +1695,16 @@
 ansitLeg
- 
 (itinera
@@ -1740,19 +1740,21 @@
 s?.find(
+(
 leg
+)
  =%3E leg.
@@ -1764,24 +1764,314 @@
 nsitLeg)%0A%7D%0A%0A
+/**%0A * Get the first stop ID from the itinerary in the underscore format required by%0A * the startTransitStopId query param (e.g., TRIMET_12345 instead of TRIMET:12345).%0A */%0Aexport function getFirstStopId(itinerary) %7B%0A  return getFirstTransitLeg(itinerary)?.from.stopId.replace(':', '_')%0A%7D%0A%0A
 /**%0A * Retur
@@ -2599,17 +2599,16 @@
 oredDays
- 
 (itinera
@@ -2843,17 +2843,16 @@
  return 
-(
 dayOfWee
@@ -2877,21 +2877,16 @@
 ek === 6
-)%0A   
  ? WEEKE
@@ -2892,20 +2892,16 @@
 END_DAYS
-%0A   
  : WEEKD

run autoformatter