"use strict";

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

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

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

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

var _contexts = require("../../../util/contexts");

var _formattedDuration = _interopRequireDefault(require("../../util/formatted-duration"));

var _formattedTimeRange = _interopRequireDefault(require("../../util/formatted-time-range"));

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; }

// TODO: make this a prop
const defaultRouteColor = '#008';
const Container = _styledComponents.default.div`
  display: ${() => _coreUtils.default.ui.isMobile() ? 'table' : 'none'};
  height: 60px;
  margin-bottom: 15px;
  padding-right: 5px;
  width: 100%;
`;
const Detail = _styledComponents.default.div`
  color: #676767;
  font-size: 13px;
`;
const Details = _styledComponents.default.div`
  display: table-cell;
  vertical-align: top;
`;
const Header = _styledComponents.default.div`
  font-size: 18px;
  font-weight: bold;
  margin-top: -3px;
`;
const LegIconContainer = _styledComponents.default.div`
  height: 30px;
  width: 30px;
`;
const NonTransitSpacer = _styledComponents.default.div`
  height: 30px;
  overflow: hidden
`;
const RoutePreview = _styledComponents.default.div`
  display: inline-block;
  margin-left: 8px;
  vertical-align: top;
`;
const Routes = _styledComponents.default.div`
  display: table-cell;
  text-align: right;
`;
const ShortName = _styledComponents.default.div`
  background-color: ${props => getRouteColorForBadge(props.leg)};
  border-radius: 15px;
  border: 2px solid white;
  box-shadow: 0 0 0.5em #000;
  color: white;
  font-size: 15px;
  font-weight: 500;
  height: 30px;
  margin-top: 6px;
  overflow: hidden;
  padding-top: 4px;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 30px;
`;

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

    _defineProperty(this, "_onSummaryClicked", () => {
      if (typeof this.props.onClick === 'function') this.props.onClick();
    });
  }

  render() {
    const {
      currency,
      itinerary
    } = this.props;
    const {
      LegIcon
    } = this.context;

    const {
      maxTNCFare,
      minTNCFare,
      transitFare
    } = _coreUtils.default.itinerary.calculateFares(itinerary); // TODO: support non-USD


    const minTotalFare = minTNCFare * 100 + transitFare;
    const maxTotalFare = maxTNCFare * 100 + transitFare;

    const {
      caloriesBurned
    } = _coreUtils.default.itinerary.calculatePhysicalActivity(itinerary);

    return /*#__PURE__*/_react.default.createElement(Container, {
      onClick: this._onSummaryClicked
    }, /*#__PURE__*/_react.default.createElement(Details, null, /*#__PURE__*/_react.default.createElement(Header, null, /*#__PURE__*/_react.default.createElement(_formattedDuration.default, {
      duration: itinerary.duration
    })), /*#__PURE__*/_react.default.createElement(Detail, null, /*#__PURE__*/_react.default.createElement(_formattedTimeRange.default, {
      endTime: itinerary.endTime,
      startTime: itinerary.startTime
    })), /*#__PURE__*/_react.default.createElement(Detail, null, minTotalFare > 0 && /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "components.ItinerarySummary.fareCost",
      values: {
        maxTotalFare: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          currency: currency,
          currencyDisplay: "narrowSymbol",
          style: "currency",
          value: maxTotalFare / 100
        }),
        minTotalFare: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          currency: currency,
          currencyDisplay: "narrowSymbol",
          style: "currency",
          value: minTotalFare / 100
        }),
        useMaxFare: minTotalFare !== maxTotalFare ? 'true' : 'false'
      }
    }), /*#__PURE__*/_react.default.createElement("span", null, " \u2022 ")), /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "common.itineraryDescriptions.calories",
      values: {
        calories: Math.round(caloriesBurned)
      }
    })), /*#__PURE__*/_react.default.createElement(Detail, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "common.itineraryDescriptions.transfers",
      values: {
        transfers: itinerary.transfers
      }
    }))), /*#__PURE__*/_react.default.createElement(Routes, null, itinerary.legs.filter(leg => {
      return !(leg.mode === 'WALK' && itinerary.transitTime > 0);
    }).map((leg, k) => {
      return /*#__PURE__*/_react.default.createElement(RoutePreview, {
        key: k
      }, /*#__PURE__*/_react.default.createElement(LegIconContainer, null, /*#__PURE__*/_react.default.createElement(LegIcon, {
        leg: leg
      })), _coreUtils.default.itinerary.isTransit(leg.mode) ? /*#__PURE__*/_react.default.createElement(ShortName, {
        leg: leg
      }, getRouteNameForBadge(leg)) : /*#__PURE__*/_react.default.createElement(NonTransitSpacer, null));
    })));
  }

} // Helper functions


exports.ItinerarySummary = ItinerarySummary;

_defineProperty(ItinerarySummary, "propTypes", {
  itinerary: _propTypes.default.object
});

_defineProperty(ItinerarySummary, "contextType", _contexts.ComponentContext);

function getRouteLongName(leg) {
  return leg.routes && leg.routes.length > 0 ? leg.routes[0].longName : leg.routeLongName;
}

function getRouteNameForBadge(leg) {
  const shortName = leg.routes && leg.routes.length > 0 ? leg.routes[0].shortName : leg.routeShortName;
  const longName = getRouteLongName(leg); // check for max

  if (longName && longName.toLowerCase().startsWith('max')) return null; // check for streetcar

  if (longName && longName.startsWith('Portland Streetcar')) return longName.split('-')[1].trim().split(' ')[0];
  return shortName || longName;
}

function getRouteColorForBadge(leg) {
  return leg.routeColor ? '#' + leg.routeColor : defaultRouteColor;
}

const mapStateToProps = (state, ownProps) => {
  var _state$otp$config$loc;

  return {
    currency: ((_state$otp$config$loc = state.otp.config.localization) === null || _state$otp$config$loc === void 0 ? void 0 : _state$otp$config$loc.currency) || 'USD'
  };
};

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

exports.default = _default;

//# sourceMappingURL=itin-summary.js