"use strict";

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

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

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

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

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

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

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

var narrativeActions = _interopRequireWildcard(require("../../actions/narrative"));

var _map = _interopRequireDefault(require("../map/map"));

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

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

var _container = _interopRequireDefault(require("./container"));

var _resultsError = _interopRequireDefault(require("./results-error"));

var _resultsHeader = _interopRequireDefault(require("./results-header"));

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

const OptionExpander = _styledComponents.default.button`
  border: none;
  bottom: ${props => props.expanded ? 'inherit' : '100px'};
  outline: none;
  padding-bottom: 3px;
  display: block;
  top: ${props => props.expanded ? '100px' : 'inherit'};
  width: 100%;
`;
const NarrativeContainer = _styledComponents.default.div`
  background-color: white;
  ${props => props.expanded ? 'top: 140px; overflow-y: auto;' : 'height: 80px; overflow-y: hidden;'}
`;

class MobileResultsScreen extends _react.Component {
  constructor() {
    super();

    _defineProperty(this, "_optionClicked", () => {
      this._setExpanded(!this.state.expanded);
    });

    _defineProperty(this, "_toggleRealtime", () => this.props.setUseRealtimeResponse({
      useRealtime: !this.props.useRealtime
    }));

    _defineProperty(this, "renderDots", () => {
      const {
        activeItineraryIndex,
        resultCount
      } = this.props; // Construct the 'dots'

      const dots = [];

      for (let i = 0; i < resultCount; i++) {
        dots.push( /*#__PURE__*/_react.default.createElement("div", {
          className: `dot${activeItineraryIndex === i ? ' active' : ''}`,
          key: i
        }));
      }

      return /*#__PURE__*/_react.default.createElement("div", {
        className: "dots-container",
        style: {
          padding: 'none'
        }
      }, dots);
    });

    this.state = {
      expanded: false
    };
  }

  componentDidMount() {
    // Get the target element that we want to persist scrolling for
    // FIXME Do we need to add something that removes the listeners when
    // component unmounts?
    _coreUtils.default.ui.enableScrollForSelector('.mobile-narrative-container');
  }

  componentDidUpdate(prevProps) {
    // Check if the active leg changed
    if (this.props.activeLeg !== prevProps.activeLeg) {
      this._setExpanded(false);
    }
  }

  _setExpanded(expanded) {
    this.setState({
      expanded
    });
    this.refs['narrative-container'].scrollTop = 0;
  }

  render() {
    const {
      activeItineraryIndex,
      error,
      realtimeEffects,
      useRealtime
    } = this.props;
    const {
      expanded
    } = this.state;
    const showRealtimeAnnotation = realtimeEffects.isAffectedByRealtimeData && (realtimeEffects.exceedsThreshold || realtimeEffects.routesDiffer || !useRealtime);
    return /*#__PURE__*/_react.default.createElement(_container.default, null, /*#__PURE__*/_react.default.createElement(_resultsHeader.default, null), /*#__PURE__*/_react.default.createElement("div", {
      className: error ? 'results-error-map' : 'results-map'
    }, /*#__PURE__*/_react.default.createElement(_map.default, null)), error ? /*#__PURE__*/_react.default.createElement(_resultsError.default, {
      error: error
    }) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(OptionExpander, {
      className: "mobile-narrative-header",
      expanded: expanded,
      onClick: this._optionClicked
    }, "Option ", activeItineraryIndex + 1, /*#__PURE__*/_react.default.createElement("i", {
      className: `fa fa-caret-${expanded ? 'down' : 'up'}`,
      style: {
        marginLeft: 8
      }
    })), /*#__PURE__*/_react.default.createElement(NarrativeContainer, {
      className: "mobile-narrative-container",
      expanded: expanded,
      ref: "narrative-container"
    }, /*#__PURE__*/_react.default.createElement(_itineraryCarousel.default, {
      expanded: this.state.expanded,
      hideHeader: true,
      onClick: this._optionClicked,
      showRealtimeAnnotation: showRealtimeAnnotation
    })), this.renderDots()));
  }

} // connect to the redux store


_defineProperty(MobileResultsScreen, "propTypes", {
  activeItineraryIndex: _propTypes.default.number,
  activeLeg: _propTypes.default.number,
  error: _propTypes.default.object,
  query: _propTypes.default.object,
  realtimeEffects: _propTypes.default.object,
  resultCount: _propTypes.default.number,
  useRealtime: _propTypes.default.bool
});

const mapStateToProps = (state, ownProps) => {
  const activeSearch = (0, _state.getActiveSearch)(state);
  const {
    useRealtime
  } = state.otp;
  const response = !activeSearch ? null : useRealtime ? activeSearch.response : activeSearch.nonRealtimeResponse;
  const realtimeEffects = (0, _state.getRealtimeEffects)(state);
  const itineraries = (0, _state.getActiveItineraries)(state);
  return {
    activeItineraryIndex: activeSearch ? activeSearch.activeItinerary : null,
    activeLeg: activeSearch ? activeSearch.activeLeg : null,
    error: (0, _state.getActiveError)(state),
    query: state.otp.currentQuery,
    realtimeEffects,
    resultCount: response ? activeSearch.query.routingType === 'ITINERARY' ? itineraries.length : response.otp.profile.length : null,
    useRealtime
  };
};

const mapDispatchToProps = {
  setUseRealtimeResponse: narrativeActions.setUseRealtimeResponse
};

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

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

//# sourceMappingURL=results-screen.js