"use strict";

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

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

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

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

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

var uiActions = _interopRequireWildcard(require("../../actions/ui"));

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

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

var _dateTimeScreen = _interopRequireDefault(require("./date-time-screen"));

var _optionsScreen = _interopRequireDefault(require("./options-screen"));

var _locationSearch = _interopRequireDefault(require("./location-search"));

var _welcomeScreen = _interopRequireDefault(require("./welcome-screen"));

var _stopViewer = _interopRequireDefault(require("./stop-viewer"));

var _tripViewer = _interopRequireDefault(require("./trip-viewer"));

var _routeViewer = _interopRequireDefault(require("./route-viewer"));

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

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 _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 {
  MainPanelContent,
  MobileScreens
} = uiActions;

class MobileMain extends _react.Component {
  componentDidUpdate(prevProps) {
    const {
      activeSearch,
      currentPosition,
      currentQuery,
      setMobileScreen
    } = this.props; // Check if we are in the welcome screen and both locations have been set OR
    // auto-detect is denied and one location is set

    if (prevProps.uiState.mobileScreen === MobileScreens.WELCOME_SCREEN && (currentQuery.from && currentQuery.to || !currentPosition.coords && (currentQuery.from || currentQuery.to))) {
      // If so, advance to main search screen
      setMobileScreen(MobileScreens.SEARCH_FORM);
    } // Display the results screen if an active search exists
    // (i.e. results are being fetched, or returned, or if there are errors).


    if (!prevProps.activeSearch && activeSearch) {
      setMobileScreen(MobileScreens.RESULTS_SUMMARY);
    }
  }

  render() {
    const {
      MobileResultsScreen,
      MobileSearchScreen
    } = this.context;
    const {
      uiState
    } = this.props; // check for route viewer

    if (uiState.mainPanelContent === MainPanelContent.ROUTE_VIEWER) {
      return /*#__PURE__*/_react.default.createElement(_routeViewer.default, null);
    } // check for viewed stop


    if (uiState.viewedStop) return /*#__PURE__*/_react.default.createElement(_stopViewer.default, null); // check for viewed trip

    if (uiState.viewedTrip) return /*#__PURE__*/_react.default.createElement(_tripViewer.default, null);

    switch (uiState.mobileScreen) {
      case MobileScreens.WELCOME_SCREEN:
        return /*#__PURE__*/_react.default.createElement(_welcomeScreen.default, null);

      case MobileScreens.SET_INITIAL_LOCATION:
        return /*#__PURE__*/_react.default.createElement(_locationSearch.default, {
          backScreen: MobileScreens.WELCOME_SCREEN,
          locationType: "to"
        });

      case MobileScreens.SEARCH_FORM:
        // Render batch search screen if batch routing enabled. Otherwise,
        // default to standard search screen.
        return /*#__PURE__*/_react.default.createElement(MobileSearchScreen, {
          newScreen: this.newScreen
        });

      case MobileScreens.SET_FROM_LOCATION:
        return /*#__PURE__*/_react.default.createElement(_locationSearch.default, {
          backScreen: MobileScreens.SEARCH_FORM,
          locationType: "from"
        });

      case MobileScreens.SET_TO_LOCATION:
        return /*#__PURE__*/_react.default.createElement(_locationSearch.default, {
          backScreen: MobileScreens.SEARCH_FORM,
          locationType: "to"
        });

      case MobileScreens.SET_DATETIME:
        return /*#__PURE__*/_react.default.createElement(_dateTimeScreen.default, null);

      case MobileScreens.SET_OPTIONS:
        return /*#__PURE__*/_react.default.createElement(_optionsScreen.default, null);

      case MobileScreens.RESULTS_SUMMARY:
        return /*#__PURE__*/_react.default.createElement(MobileResultsScreen, null);

      default:
        return /*#__PURE__*/_react.default.createElement("p", null, "Invalid mobile screen");
    }
  }

} // connect to the redux store


_defineProperty(MobileMain, "propTypes", {
  activeSearch: _propTypes.default.object,
  currentPosition: _propTypes.default.object,
  currentQuery: _propTypes.default.object,
  setMobileScreen: _propTypes.default.func,
  uiState: _propTypes.default.object
});

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

const mapStateToProps = (state, ownProps) => {
  const {
    config,
    currentQuery,
    location,
    ui: uiState
  } = state.otp;
  const activeSearch = (0, _state.getActiveSearch)(state);
  return {
    activeSearch,
    config,
    currentPosition: location.currentPosition,
    currentQuery,
    uiState
  };
};

const mapDispatchToProps = {
  setMobileScreen: uiActions.setMobileScreen
};

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

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

//# sourceMappingURL=main.js