"use strict";

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

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

var _ModeCircles = require("@opentripplanner/transit-vehicle-overlay/lib/components/markers/ModeCircles");

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

var _reactIntl = require("react-intl");

var _transitVehicleOverlay = _interopRequireDefault(require("@opentripplanner/transit-vehicle-overlay"));

var _reactLeaflet = require("react-leaflet");

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

/**
 * This overlay is similar to gtfs-rt-vehicle-overlay in that it shows
 * realtime positions of vehicles on a route using the otp-ui/transit-vehicle-overlay.
 *
 * However, this overlay differs in a few ways:
 * 1) This overlay retrieves vehicle locations from OTP
 * 2) This overlay renders vehicles as blobs rather than a custom shape
 * 3) This overlay does not handle updating positions
 * 4) This overlay does not render route paths
 * 5) This overlay has a custom popup on vehicle hover
 */
const vehicleSymbols = [{
  minZoom: 0,
  symbol: _ModeCircles.Circle
}, {
  minZoom: 10,
  symbol: _ModeCircles.CircledVehicle
}];

function VehicleTooltip(props) {
  var _vehicleLabel;

  const {
    direction,
    intl,
    permanent,
    vehicle
  } = props;
  let vehicleLabel = vehicle === null || vehicle === void 0 ? void 0 : vehicle.label; // If a vehicle's label is less than 5 characters long, we can assume it is a vehicle
  // number. If this is the case, prepend "vehicle" to it.
  // Otherwise, the label itself is enough

  if (vehicleLabel !== null && ((_vehicleLabel = vehicleLabel) === null || _vehicleLabel === void 0 ? void 0 : _vehicleLabel.length) <= 5) {
    vehicleLabel = intl.formatMessage({
      id: 'components.TransitVehicleOverlay.vehicleName'
    }, {
      vehicleNumber: vehicleLabel
    });
  } else {
    vehicleLabel = '';
  }

  const stopStatus = (vehicle === null || vehicle === void 0 ? void 0 : vehicle.stopStatus) || 'in_transit_to'; // FIXME: This may not be timezone adjusted as reported seconds may be in the wrong timezone.
  // All needed info to fix this is available via route.agency.timezone
  // However, the needed coreUtils methods are not updated to support this

  return /*#__PURE__*/_react.default.createElement(_reactLeaflet.Tooltip, {
    direction: direction,
    permanent: permanent
  }, /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "components.TransitVehicleOverlay.realtimeVehicleInfo",
    values: {
      b: m => /*#__PURE__*/_react.default.createElement("b", {
        style: {
          fontSize: '110%'
        }
      }, m),
      relativeTime: intl.formatRelativeTime(Math.floor((vehicle === null || vehicle === void 0 ? void 0 : vehicle.seconds) - Date.now() / 1000)),
      vehicleNameOrBlank: vehicleLabel
    }
  })), stopStatus !== 'STOPPED_AT' && (vehicle === null || vehicle === void 0 ? void 0 : vehicle.speed) > 0 && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "components.TransitVehicleOverlay.travelingAt",
    values: {
      milesPerHour: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
        style: "unit",
        unit: "mile-per-hour",
        value: Math.round(vehicle.speed)
      })
    }
  })), (vehicle === null || vehicle === void 0 ? void 0 : vehicle.nextStopName) && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: `components.TransitVehicleOverlay.${stopStatus.toLowerCase()}`,
    values: {
      stop: vehicle.nextStopName
    }
  })));
} // connect to the redux store


const mapStateToProps = (state, ownProps) => {
  var _state$otp$transitInd, _state$otp$transitInd2;

  const viewedRoute = state.otp.ui.viewedRoute;
  const route = (_state$otp$transitInd = state.otp.transitIndex) === null || _state$otp$transitInd === void 0 ? void 0 : (_state$otp$transitInd2 = _state$otp$transitInd.routes) === null || _state$otp$transitInd2 === void 0 ? void 0 : _state$otp$transitInd2[viewedRoute === null || viewedRoute === void 0 ? void 0 : viewedRoute.routeId];
  let vehicleList = []; // Add missing fields to vehicle list

  if (viewedRoute === null || viewedRoute === void 0 ? void 0 : viewedRoute.routeId) {
    var _route$vehicles;

    vehicleList = route === null || route === void 0 ? void 0 : (_route$vehicles = route.vehicles) === null || _route$vehicles === void 0 ? void 0 : _route$vehicles.map(vehicle => {
      vehicle.routeType = route === null || route === void 0 ? void 0 : route.mode;
      vehicle.routeColor = route === null || route === void 0 ? void 0 : route.color;
      vehicle.textColor = route === null || route === void 0 ? void 0 : route.routeTextColor;
      return vehicle;
    }); // Remove all vehicles not on pattern being currently viewed

    if (viewedRoute.patternId && vehicleList) {
      vehicleList = vehicleList.filter(vehicle => vehicle.patternId === viewedRoute.patternId);
    }
  }

  return {
    symbols: vehicleSymbols,
    TooltipSlot: (0, _reactIntl.injectIntl)(VehicleTooltip),
    vehicleList
  };
};

const mapDispatchToProps = {};

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

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

//# sourceMappingURL=connected-transit-vehicle-overlay.js