"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = activeTripRenderer;

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

var _time = require("@opentripplanner/core-utils/lib/time");

var _baseRenderer = _interopRequireDefault(require("./base-renderer"));

var _viewer = require("../../../../util/viewer");

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

/**
 * Calculates various data for monitored trips that are currently active.
 */
function activeTripRenderer({
  monitoredTrip,
  onTimeThresholdSeconds,
  timeFormat
}) {
  const data = (0, _baseRenderer.default)(monitoredTrip);
  const tripEndMoment = (0, _moment.default)(data.matchingItinerary.endTime); // analyze whether the journey state indicates that the matching itinerary
  // has realtime data. It is assumed that realtime data will exist for
  // itineraries that have differing values of the scheduled arrival time
  // and matching itinerary arrival time.

  if (data.journeyState.hasRealtimeData) {
    // calculate the deviation from the scheduled arrival time (positive
    // value indicates delay)
    const arrivalDeviationSeconds = (data.matchingItinerary.endTime - data.journeyState.scheduledArrivalTimeEpochMillis) / 1000;
    const deviationHumanDuration = (0, _time.formatDuration)(Math.abs(arrivalDeviationSeconds));
    const tripStatus = (0, _viewer.getTripStatus)(true, arrivalDeviationSeconds, onTimeThresholdSeconds);

    if (tripStatus === _viewer.REALTIME_STATUS.ON_TIME) {
      // about on time
      data.panelBsStyle = 'success';
      data.headingText = 'Trip is in progress and is about on time.';
    } else if (tripStatus === _viewer.REALTIME_STATUS.LATE) {
      // delayed
      data.panelBsStyle = 'warning';
      data.headingText = `Trip is in progress and is delayed ${deviationHumanDuration}!`;
    } else {
      // early
      data.panelBsStyle = 'warning';
      data.headingText = `Trip is in progress and is arriving ${deviationHumanDuration} earlier than expected!`;
    }
  } else {
    data.panelBsStyle = 'info';
    data.headingText = 'Trip is in progress (no realtime updates available).';
  }

  data.bodyText = `Trip is due to arrive at the destination at ${tripEndMoment.format(timeFormat)}.`;
  data.shouldRenderTogglePauseTripButton = true;
  data.shouldRenderToggleSnoozeTripButton = true;
  return data;
}

module.exports = exports.default;

//# sourceMappingURL=active-trip-renderer.js