"use strict";

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

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

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

var apiActions = _interopRequireWildcard(require("../../actions/api"));

var locationActions = _interopRequireWildcard(require("../../actions/location"));

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

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

/**
 * This higher-order component connects the target (styled) LocationField to the
 * redux store.
 * @param StyledLocationField The input LocationField component to connect.
 * @param options Optional object with the following optional props:
 *                - actions: a list of actions to include in mapDispatchToProps
 *                - includeLocation: whether to derive the location prop from
 *                  the active query
 * @returns The connected component.
 */
function connectLocationField(StyledLocationField, options = {}) {
  // By default, set actions to empty list and do not include location.
  const {
    actions = [],
    includeLocation = false
  } = options;

  const mapStateToProps = (state, ownProps) => {
    var _config$geocoder;

    const {
      config,
      currentQuery,
      location,
      transitIndex,
      user
    } = state.otp;
    const {
      currentPosition,
      nearbyStops,
      sessionSearches
    } = location;
    const activeSearch = (0, _state.getActiveSearch)(state);
    const query = activeSearch ? activeSearch.query : currentQuery;
    const stateToProps = {
      currentPosition,
      geocoderConfig: config.geocoder,
      layerColorMap: ((_config$geocoder = config.geocoder) === null || _config$geocoder === void 0 ? void 0 : _config$geocoder.resultsColors) || {},
      nearbyStops,
      sessionSearches,
      showUserSettings: (0, _state.getShowUserSettings)(state),
      stopsIndex: transitIndex.stops,
      userLocationsAndRecentPlaces: [...user.locations, ...user.recentPlaces]
    }; // Set the location prop only if includeLocation is specified, else leave unset.
    // Otherwise, the StyledLocationField component will use the fixed undefined/null value as location
    // and will not respond to user input.

    if (includeLocation) {
      stateToProps.location = query[ownProps.locationType];
    }

    return stateToProps;
  };

  const mapDispatchToProps = {
    addLocationSearch: locationActions.addLocationSearch,
    findNearbyStops: apiActions.findNearbyStops,
    getCurrentPosition: locationActions.getCurrentPosition,
    ...actions
  };
  return (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(StyledLocationField);
}

module.exports = exports.default;

//# sourceMappingURL=connect-location-field.js