"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getLeafletItineraryBounds = getLeafletItineraryBounds;
exports.getLeafletLegBounds = getLeafletLegBounds;
exports.isBatchRoutingEnabled = isBatchRoutingEnabled;
exports.itineraryCanBeMonitored = itineraryCanBeMonitored;
exports.getFirstStopId = getFirstStopId;
exports.getMinutesUntilItineraryStart = getMinutesUntilItineraryStart;
exports.getItineraryDefaultMonitoredDays = getItineraryDefaultMonitoredDays;

var _leaflet = require("leaflet");

var _moment = _interopRequireDefault(require("moment"));

var _coreUtils = _interopRequireDefault(require("@opentripplanner/core-utils"));

var _monitoredTrip = require("./monitored-trip");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function getLeafletItineraryBounds(itinerary) {
  return (0, _leaflet.latLngBounds)(_coreUtils.default.itinerary.getItineraryBounds(itinerary));
}
/**
 * Return a leaflet LatLngBounds object that encloses the given leg's geometry.
 */


function getLeafletLegBounds(leg) {
  return (0, _leaflet.latLngBounds)(_coreUtils.default.itinerary.getLegBounds(leg));
}

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.)
 */


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).
 */


function getFirstStopId(itinerary) {
  var _getFirstTransitLeg;

  return (_getFirstTransitLeg = getFirstTransitLeg(itinerary)) === null || _getFirstTransitLeg === void 0 ? void 0 : _getFirstTransitLeg.from.stopId.replace(':', '_');
}

function getMinutesUntilItineraryStart(itinerary) {
  return (0, _moment.default)(itinerary.startTime).diff((0, _moment.default)(), 'minutes');
}
/**
 * Gets the first transit leg of the given itinerary, or null if none found.
 */


function getFirstTransitLeg(itinerary) {
  var _itinerary$legs;

  return itinerary === null || itinerary === void 0 ? void 0 : (_itinerary$legs = itinerary.legs) === null || _itinerary$legs === void 0 ? void 0 : _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.
 */


function getItineraryDefaultMonitoredDays(itinerary) {
  const firstTransitLeg = getFirstTransitLeg(itinerary);
  const startMoment = firstTransitLeg ? (0, _moment.default)(firstTransitLeg.serviceDate, 'YYYYMMDD') : (0, _moment.default)(itinerary.startTime);
  const dayOfWeek = startMoment.day();
  return dayOfWeek === 0 || dayOfWeek === 6 ? _monitoredTrip.WEEKEND_DAYS : _monitoredTrip.WEEKDAYS;
}

//# sourceMappingURL=itinerary.js