"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.planFirst = planFirst;
exports.planPrevious = planPrevious;
exports.planNext = planNext;
exports.planLast = planLast;

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

var _itinerary = require("@opentripplanner/core-utils/lib/itinerary");

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

var _itinerary2 = require("../util/itinerary");

var _state = require("../util/state");

var _api = require("./api");

var _form = require("./form");

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

const SERVICE_BREAK = '03:00';
const NINETY_SECONDS = 90000;

function updateParamsAndPlan(params) {
  return function (dispatch, getState) {
    dispatch((0, _form.setQueryParam)(params));
    dispatch((0, _api.routingQuery)());
  };
}

function offsetTime(itinerary, unixTime) {
  let offset = 0;
  if (itinerary) offset = (0, _itinerary.getTimeZoneOffset)(itinerary);
  return (0, _moment.default)(unixTime + offset);
}
/**
 * Effectively checks whether #planFirst has already been clicked, i.e., the
 * query is planning a depart at the service break time.
 */


function isPlanningFirst(query) {
  const {
    departArrive,
    time
  } = query;
  return departArrive === 'DEPART' && time === SERVICE_BREAK;
}
/**
 * Plan the first trip of the day, or if the first trip has already been planned,
 * plan the first trip of the previous day.
 */


function planFirst() {
  return function (dispatch, getState) {
    const state = getState();
    const itinerary = (0, _state.getActiveItinerary)(state);
    const {
      currentQuery
    } = state.otp;
    const date = (0, _moment.default)(currentQuery.date); // If already planning for the "first" trip, subtract a day to mirror the
    // behavior of planLast.

    if (isPlanningFirst(currentQuery)) date.subtract('days', 1);
    const params = {
      date: date.format(_time.OTP_API_DATE_FORMAT),
      departArrive: 'DEPART',
      startTransitStopId: (0, _itinerary2.getFirstStopId)(itinerary),
      time: SERVICE_BREAK
    };
    dispatch(updateParamsAndPlan(params));
  };
}
/**
 * Plan the previous trip, setting the arrive by time to the current itinerary's
 * end time (minus a small amount).
 */


function planPrevious() {
  return function (dispatch, getState) {
    const itinerary = (0, _state.getActiveItinerary)(getState());
    const newEndTime = offsetTime(itinerary, itinerary.endTime - NINETY_SECONDS);
    const params = {
      date: newEndTime.format(_time.OTP_API_DATE_FORMAT),
      departArrive: 'ARRIVE',
      startTransitStopId: (0, _itinerary2.getFirstStopId)(itinerary),
      time: newEndTime.format(_time.OTP_API_TIME_FORMAT)
    };
    dispatch(updateParamsAndPlan(params));
  };
}
/**
 * Plan the next trip, setting the depart at time to the current itinerary's
 * start time (plus a small amount).
 */


function planNext() {
  return function (dispatch, getState) {
    const itinerary = (0, _state.getActiveItinerary)(getState());
    const newStartTime = offsetTime(itinerary, itinerary.startTime + NINETY_SECONDS);
    const params = {
      date: newStartTime.format(_time.OTP_API_DATE_FORMAT),
      departArrive: 'DEPART',
      startTransitStopId: (0, _itinerary2.getFirstStopId)(itinerary),
      time: newStartTime.format(_time.OTP_API_TIME_FORMAT)
    };
    dispatch(updateParamsAndPlan(params));
  };
}
/**
 * Plan the last trip of the day, or if the last trip has already been planned,
 * plan the last trip of the next day.
 */


function planLast() {
  return function (dispatch, getState) {
    const state = getState();
    const itinerary = (0, _state.getActiveItinerary)(state);
    const {
      currentQuery
    } = state.otp;
    const params = {
      date: (0, _moment.default)(currentQuery.date).add('days', 1).format(_time.OTP_API_DATE_FORMAT),
      departArrive: 'ARRIVE',
      startTransitStopId: (0, _itinerary2.getFirstStopId)(itinerary),
      time: SERVICE_BREAK
    };
    dispatch(updateParamsAndPlan(params));
  };
}

//# sourceMappingURL=plan.js