"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 _react = _interopRequireWildcard(require("react"));

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

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

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

var formActions = _interopRequireWildcard(require("../../actions/form"));

var _icon = _interopRequireDefault(require("../util/icon"));

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

var _batchPreferences = _interopRequireDefault(require("./batch-preferences"));

var _dateTimeModal = _interopRequireDefault(require("./date-time-modal"));

var _modeButtons = _interopRequireWildcard(require("./mode-buttons"));

var _batchStyled = require("./batch-styled");

var _styled = require("./styled");

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

/**
 * Simple utility to check whether a list of mode strings contains the provided
 * mode. This handles exact match and prefix/suffix matches (i.e., checking
 * 'BICYCLE' will return true if 'BICYCLE' or 'BICYCLE_RENT' is in the list).
 *
 * FIXME: This might need to be modified to be a bit looser in how it handles
 * the 'contains' check. E.g., we might not want to remove WALK,TRANSIT if walk
 * is turned off, but we DO want to remove it if TRANSIT is turned off.
 */
function listHasMode(modes, mode) {
  return modes.some(m => mode.indexOf(m) !== -1);
}

function combinationHasAnyOfModes(combination, modes) {
  return combination.mode.split(',').some(m => listHasMode(modes, m));
} // List of possible modes that can be selected via mode buttons.


const POSSIBLE_MODES = _modeButtons.MODE_OPTIONS.map(b => b.mode);

const ModeButtonsFullWidthContainer = _styledComponents.default.div`
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
`; // Define Mode Button styled components here to avoid circular imports. I.e., we
// cannot define them in styled.js (because mode-buttons.js imports buttonCss
// and then we would need to import ModeButtons/StyledModeButton from that file
// in turn).

const ModeButtonsFullWidth = (0, _styledComponents.default)(_modeButtons.default)`
  &:last-child {
    margin-right: 0px;
  }
`;
const ModeButtonsContainerCompressed = _styledComponents.default.div`
  display: contents;
`;
const ModeButtonsCompressed = (0, _styledComponents.default)(_modeButtons.default)`
  ${_modeButtons.StyledModeButton} {
    border-radius: 0px;
  }
  &:first-child {
    border-radius: 5px 0px 0px 5px;
  }
  &:last-child {
    margin-right: 5px;
    border-radius: 0px 5px 5px 0px;
  }
`;
/**
 * Main panel for the batch/trip comparison form.
 */

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

    _defineProperty(this, "state", {
      expanded: null,
      selectedModes: POSSIBLE_MODES
    });

    _defineProperty(this, "_onClickMode", mode => {
      const {
        possibleCombinations,
        setQueryParam
      } = this.props;
      const {
        selectedModes
      } = this.state;
      const index = selectedModes.indexOf(mode);
      const enableMode = index === -1;
      const newModes = [...selectedModes];
      if (enableMode) newModes.push(mode);else newModes.splice(index, 1); // Update selected modes for mode buttons.

      this.setState({
        selectedModes: newModes
      }); // Update the available mode combinations based on the new modes selection.

      const disabledModes = POSSIBLE_MODES.filter(m => !newModes.includes(m)); // Do not include combination if any of its modes are found in disabled
      // modes list.

      const newCombinations = possibleCombinations.filter(c => !combinationHasAnyOfModes(c, disabledModes));
      setQueryParam({
        combinations: newCombinations
      });
    });

    _defineProperty(this, "_planTrip", () => {
      const {
        currentQuery,
        routingQuery
      } = this.props; // Check for any validation issues in query.

      const issues = [];
      if (!(0, _state.hasValidLocation)(currentQuery, 'from')) issues.push('from');
      if (!(0, _state.hasValidLocation)(currentQuery, 'to')) issues.push('to');

      if (issues.length > 0) {
        // TODO: replace with less obtrusive validation.
        window.alert(`Please define the following fields to plan a trip: ${issues.join(', ')}`);
        return;
      } // Close any expanded panels.


      this.setState({
        expanded: null
      }); // Plan trip.

      routingQuery();
    });

    _defineProperty(this, "_updateExpanded", type => ({
      expanded: this.state.expanded === type ? null : type
    }));

    _defineProperty(this, "_toggleDateTime", () => this.setState(this._updateExpanded('DATE_TIME')));

    _defineProperty(this, "_toggleSettings", () => this.setState(this._updateExpanded('SETTINGS')));
  }

  render() {
    const {
      config,
      currentQuery
    } = this.props;
    const {
      expanded,
      selectedModes
    } = this.state;
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(ModeButtonsFullWidthContainer, {
      className: "hidden-lg"
    }, /*#__PURE__*/_react.default.createElement(ModeButtonsFullWidth, {
      className: "flex",
      onClick: this._onClickMode,
      selectedModes: selectedModes
    })), /*#__PURE__*/_react.default.createElement(_batchStyled.MainSettingsRow, null, /*#__PURE__*/_react.default.createElement(_batchStyled.SettingsPreview, {
      expanded: expanded === 'SETTINGS',
      onClick: this._toggleSettings
    }, _coreUtils.default.query.isNotDefaultQuery(currentQuery, config) && /*#__PURE__*/_react.default.createElement(_styled.Dot, {
      className: "dot"
    }), /*#__PURE__*/_react.default.createElement(_icon.default, {
      className: "fa-2x",
      type: "cog"
    })), /*#__PURE__*/_react.default.createElement(_batchStyled.StyledDateTimePreview // as='button'
    , {
      expanded: expanded === 'DATE_TIME',
      hideButton: true,
      onClick: this._toggleDateTime
    }), /*#__PURE__*/_react.default.createElement(ModeButtonsContainerCompressed, null, /*#__PURE__*/_react.default.createElement(ModeButtonsCompressed, {
      className: "visible-lg straight-corners",
      onClick: this._onClickMode,
      selectedModes: selectedModes
    })), /*#__PURE__*/_react.default.createElement(_batchStyled.PlanTripButton, {
      onClick: this._planTrip,
      title: "Plan trip"
    }, /*#__PURE__*/_react.default.createElement(_icon.default, {
      className: "fa-2x",
      type: "search"
    }))), expanded === 'DATE_TIME' && /*#__PURE__*/_react.default.createElement(_batchStyled.DateTimeModalContainer, null, /*#__PURE__*/_react.default.createElement(_dateTimeModal.default, null)), expanded === 'SETTINGS' && /*#__PURE__*/_react.default.createElement(_batchStyled.BatchPreferencesContainer, null, /*#__PURE__*/_react.default.createElement(_batchPreferences.default, null)));
  }

} // connect to the redux store


const mapStateToProps = (state, ownProps) => {
  const showUserSettings = (0, _state.getShowUserSettings)(state);
  return {
    activeSearch: (0, _state.getActiveSearch)(state),
    config: state.otp.config,
    currentQuery: state.otp.currentQuery,
    expandAdvanced: state.otp.user.expandAdvanced,
    possibleCombinations: state.otp.config.modes.combinations,
    showUserSettings
  };
};

const mapDispatchToProps = {
  routingQuery: apiActions.routingQuery,
  setQueryParam: formActions.setQueryParam
};

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

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

//# sourceMappingURL=batch-settings.js