"use strict";

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

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

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

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

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

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

var _fieldTripItineraryGroupSize = _interopRequireDefault(require("../../admin/field-trip-itinerary-group-size"));

var _narrativeItinerary = _interopRequireDefault(require("../narrative-itinerary"));

var _connectedItineraryBody = _interopRequireDefault(require("../line-itin/connected-itinerary-body"));

var _simpleRealtimeAnnotation = _interopRequireDefault(require("../simple-realtime-annotation"));

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

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

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

var _itinerarySummary = _interopRequireDefault(require("./itinerary-summary"));

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

const {
  isBicycle,
  isMicromobility,
  isTransit
} = _coreUtils.default.itinerary; // Styled components

const LegIconWrapper = _styledComponents.default.div`
  display: inline-block;
  height: 20px;
  padding-bottom: 6px;
  padding-left: 2px;
  width: 20px;

  /* Equivalent of a single space before the leg icon. */
  &::before {
    content: "";
    margin: 0 0.125em;
  }
`;
const DetailsHint = _styledComponents.default.div`
  clear: both;
  color: #685C5C;
  font-size: small;
  text-align: center;
`;
/**
 * Obtains the description of an itinerary in the given locale.
 */

function ItineraryDescription({
  itinerary
}) {
  let primaryTransitDuration = 0;
  let accessModeId = 'walk';
  let transitMode;
  itinerary.legs.forEach((leg, i) => {
    const {
      duration,
      mode,
      rentedBike,
      rentedVehicle
    } = leg;

    if (isTransit(mode) && duration > primaryTransitDuration) {
      // TODO: convert OTP's TRAM mode to the correct wording for Portland
      primaryTransitDuration = duration;
      transitMode = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
        id: `common.otpTransitModes.${mode.toLowerCase()}`
      });
    }

    if (isBicycle(mode)) accessModeId = 'bike';
    if (isMicromobility(mode)) accessModeId = 'micromobility';
    if (rentedVehicle) accessModeId = 'micromobilityRent';
    if (rentedBike) accessModeId = 'bikeshare';
    if (mode === 'CAR') accessModeId = 'drive';
  });

  const mainMode = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: `common.accessModes.${accessModeId}`
  });

  return transitMode ? /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
    id: "components.DefaultItinerary.multiModeSummary",
    values: {
      accessMode: mainMode,
      transitMode
    }
  }) : mainMode;
}

const ITINERARY_ATTRIBUTES = [{
  alias: 'best',
  id: 'duration',
  order: 0,
  render: (itinerary, options) => /*#__PURE__*/_react.default.createElement(_formattedDuration.default, {
    duration: itinerary.duration
  })
}, {
  alias: 'departureTime',
  id: 'arrivalTime',
  order: 1,
  render: (itinerary, options) => {
    if (options.isSelected) {
      if (options.selection === 'ARRIVALTIME') {
        return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedTime, {
          value: itinerary.endTime
        });
      } else {
        return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedTime, {
          value: itinerary.startTime
        });
      }
    }

    return /*#__PURE__*/_react.default.createElement(_formattedTimeRange.default, {
      endTime: itinerary.endTime,
      startTime: itinerary.startTime
    });
  }
}, {
  id: 'cost',
  order: 2,
  render: (itinerary, options) => {
    return /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
      currency: options.currency,
      currencyDisplay: "narrowSymbol",
      style: "currency",
      value: (0, _state.getTotalFare)(itinerary, options.configCosts) / 100
    });
  }
}, {
  id: 'walkTime',
  order: 3,
  render: (itinerary, options) => {
    const leg = itinerary.legs[0];
    const {
      LegIcon
    } = options;
    return (
      /*#__PURE__*/
      // FIXME: For CAR mode, walk time considers driving time.
      _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_formattedDuration.default, {
        duration: itinerary.walkTime
      }), /*#__PURE__*/_react.default.createElement(LegIconWrapper, null, /*#__PURE__*/_react.default.createElement(LegIcon, {
        leg: leg,
        size: 5
      })))
    );
  }
}];

class DefaultItinerary extends _narrativeItinerary.default {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "_onMouseEnter", () => {
      const {
        active,
        index,
        setVisibleItinerary,
        visibleItinerary
      } = this.props; // Set this itinerary as visible if not already visible.

      const visibleNotSet = visibleItinerary === null || visibleItinerary === undefined;
      const isVisible = visibleItinerary === index || active === index && visibleNotSet;

      if (typeof setVisibleItinerary === 'function' && !isVisible) {
        setVisibleItinerary({
          index
        });
      }
    });

    _defineProperty(this, "_onMouseLeave", () => {
      const {
        index,
        setVisibleItinerary,
        visibleItinerary
      } = this.props;

      if (typeof setVisibleItinerary === 'function' && visibleItinerary === index) {
        setVisibleItinerary({
          index: null
        });
      }
    });

    _defineProperty(this, "_isSortingOnAttribute", attribute => {
      const {
        sort
      } = this.props;

      if (sort && sort.type) {
        const type = sort.type.toLowerCase();
        return attribute.id.toLowerCase() === type || attribute.alias && attribute.alias.toLowerCase() === type;
      }

      return false;
    });
  }

  render() {
    const {
      active,
      configCosts,
      currency,
      expanded,
      itinerary,
      LegIcon,
      setActiveLeg,
      showRealtimeAnnotation,
      timeFormat
    } = this.props;
    const timeOptions = {
      format: timeFormat,
      offset: _coreUtils.default.itinerary.getTimeZoneOffset(itinerary)
    };
    return /*#__PURE__*/_react.default.createElement("div", {
      className: `option default-itin${active ? ' active' : ''}${expanded ? ' expanded' : ''}`,
      onMouseEnter: this._onMouseEnter,
      onMouseLeave: this._onMouseLeave,
      role: "presentation"
    }, /*#__PURE__*/_react.default.createElement("button", {
      className: "header" // _onHeaderClick comes from super component (NarrativeItinerary).
      ,
      onClick: this._onHeaderClick
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "title"
    }, /*#__PURE__*/_react.default.createElement(ItineraryDescription, {
      itinerary: itinerary
    })), /*#__PURE__*/_react.default.createElement("ul", {
      className: "list-unstyled itinerary-attributes"
    }, ITINERARY_ATTRIBUTES.sort((a, b) => {
      const aSelected = this._isSortingOnAttribute(a);

      const bSelected = this._isSortingOnAttribute(b);

      if (aSelected) return -1;
      if (bSelected) return 1;else return a.order - b.order;
    }).map(attribute => {
      const isSelected = this._isSortingOnAttribute(attribute);

      const options = attribute.id === 'arrivalTime' ? timeOptions : {};

      if (isSelected) {
        options.isSelected = true;
        options.selection = this.props.sort.type;
      }

      options.LegIcon = LegIcon;
      options.configCosts = configCosts;
      options.currency = currency;
      return /*#__PURE__*/_react.default.createElement("li", {
        className: `${attribute.id}${isSelected ? ' main' : ''}`,
        key: attribute.id
      }, attribute.render(itinerary, options));
    })), /*#__PURE__*/_react.default.createElement(_itinerarySummary.default, {
      itinerary: itinerary,
      LegIcon: LegIcon
    }), /*#__PURE__*/_react.default.createElement(_fieldTripItineraryGroupSize.default, {
      itinerary: itinerary
    }), active && !expanded && /*#__PURE__*/_react.default.createElement(DetailsHint, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "components.DefaultItinerary.clickDetails"
    }))), active && expanded && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, showRealtimeAnnotation && /*#__PURE__*/_react.default.createElement(_simpleRealtimeAnnotation.default, null), /*#__PURE__*/_react.default.createElement(_connectedItineraryBody.default, {
      itinerary: itinerary,
      LegIcon: LegIcon,
      setActiveLeg: setActiveLeg,
      timeOptions: timeOptions
    })));
  }

}

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

  return {
    configCosts: (_state$otp$config$iti = state.otp.config.itinerary) === null || _state$otp$config$iti === void 0 ? void 0 : _state$otp$config$iti.costs,
    // The configured (ambient) currency is needed for rendering the cost
    // of itineraries whether they include a fare or not, in which case
    // we show $0.00 or its equivalent in the configured currency and selected locale.
    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)(DefaultItinerary);

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

//# sourceMappingURL=default-itinerary.js