"use strict";

require("core-js/modules/web.dom.iterable");

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _reactRedux = require("react-redux");

var fieldTripActions = _interopRequireWildcard(require("../../actions/field-trip"));

var formActions = _interopRequireWildcard(require("../../actions/form"));

var _callTaker = require("../../util/call-taker");

var _fieldTripStatusIcon = _interopRequireDefault(require("./field-trip-status-icon"));

var _styled = require("./styled");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

class TripStatus extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_formatTime", time => (0, _moment.default)(time).format(this.props.timeFormat));

    _defineProperty(this, "_onDeleteTrip", () => {
      const {
        deleteRequestTripItineraries,
        request,
        trip
      } = this.props;

      if (!confirm('Are you sure you want to delete the planned trip?')) {
        return;
      }

      deleteRequestTripItineraries(request, trip.id);
    });

    _defineProperty(this, "_onPlanTrip", () => {
      const {
        outbound,
        planTrip,
        request,
        status,
        trip
      } = this.props;

      if (status && trip) {
        if (!confirm('Re-planning this trip will cause the trip planner to avoid the currently saved trip. Are you sure you want to continue?')) {
          return;
        }
      }

      planTrip(request, outbound);
    });

    _defineProperty(this, "_onSaveTrip", () => {
      const {
        outbound,
        request,
        saveRequestTripItineraries
      } = this.props;
      saveRequestTripItineraries(request, outbound);
    });

    _defineProperty(this, "_onViewTrip", () => {
      const {
        outbound,
        request,
        viewRequestTripItineraries
      } = this.props;
      viewRequestTripItineraries(request, outbound);
    });

    _defineProperty(this, "_tripIsPlanned", () => Boolean(this.props.status && this.props.trip));

    _defineProperty(this, "_renderTripStatus", () => {
      const {
        trip
      } = this.props;

      if (!this._tripIsPlanned()) {
        return /*#__PURE__*/_react.default.createElement(_styled.Para, null, "No itineraries planned! Click Plan to plan trip.");
      }

      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_styled.Para, null, trip.groupItineraries.length, " group itineraries, planned by", ' ', trip.createdBy, " at ", trip.timeStamp), /*#__PURE__*/_react.default.createElement(_styled.Para, null, /*#__PURE__*/_react.default.createElement(_styled.Button, {
        bsSize: "xs",
        onClick: this._onViewTrip
      }, "View"), /*#__PURE__*/_react.default.createElement(_styled.Button, {
        bsSize: "xs",
        onClick: this._onDeleteTrip
      }, "Delete")));
    });
  }

  render() {
    const {
      outbound,
      request,
      saveable
    } = this.props;
    const {
      arriveDestinationTime,
      arriveSchoolTime,
      endLocation,
      leaveDestinationTime,
      startLocation
    } = request;

    if (!request) {
      console.warn('Could not find field trip request');
      return null;
    }

    const start = outbound ? startLocation : endLocation;
    const end = outbound ? endLocation : startLocation;
    return /*#__PURE__*/_react.default.createElement(_styled.Full, null, /*#__PURE__*/_react.default.createElement(_styled.Header, null, /*#__PURE__*/_react.default.createElement(_fieldTripStatusIcon.default, {
      ok: this._tripIsPlanned()
    }), outbound ? 'Outbound' : 'Inbound', " trip", /*#__PURE__*/_react.default.createElement(_styled.Button, {
      bsSize: "xs",
      onClick: this._onPlanTrip
    }, "Plan"), /*#__PURE__*/_react.default.createElement(_styled.Button, {
      bsSize: "xs",
      disabled: !saveable,
      onClick: this._onSaveTrip
    }, "Save")), /*#__PURE__*/_react.default.createElement(_styled.Para, null, "From ", /*#__PURE__*/_react.default.createElement(_styled.Bold, null, start), " to ", /*#__PURE__*/_react.default.createElement(_styled.Bold, null, end)), outbound ? /*#__PURE__*/_react.default.createElement(_styled.Para, null, "Arriving at ", this._formatTime(arriveDestinationTime)) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_styled.Para, null, "Leave at ", this._formatTime(leaveDestinationTime), ",", ' ', "due back at ", this._formatTime(arriveSchoolTime))), this._renderTripStatus());
  }

}

const mapStateToProps = (state, ownProps) => {
  const {
    outbound,
    request
  } = ownProps;
  const {
    saveable
  } = state.callTaker.fieldTrip;
  return {
    currentQuery: state.otp.currentQuery,
    saveable: outbound ? saveable === null || saveable === void 0 ? void 0 : saveable.outbound : saveable === null || saveable === void 0 ? void 0 : saveable.inbound,
    status: outbound ? request.outboundTripStatus : request.inboundTripStatus,
    timeFormat: (0, _time.getTimeFormat)(state.otp.config),
    trip: (0, _callTaker.getTripFromRequest)(request, outbound)
  };
};

const mapDispatchToProps = {
  deleteRequestTripItineraries: fieldTripActions.deleteRequestTripItineraries,
  planTrip: fieldTripActions.planTrip,
  saveRequestTripItineraries: fieldTripActions.saveRequestTripItineraries,
  setQueryParam: formActions.setQueryParam,
  viewRequestTripItineraries: fieldTripActions.viewRequestTripItineraries
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(TripStatus);

exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=trip-status.js