"use strict";

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

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

var _itinerary = require("@opentripplanner/core-utils/lib/itinerary");

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

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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

/**
 * Dropdown selector for the Call Taker form that allows quick selection of the
 * full set of exclusive modes and access modes + transit. This will also
 * automatically apply any companies associated with the mode option to the
 * query params (e.g., Uber for CAR_HAIL or one of the various bike/scooter
 * rental companies).
 */
class ModeDropdown extends _react.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "modeToOptionValue", mode => {
      const {
        modes
      } = this.props;

      for (let i = 0; i < modes.exclusiveModes.length; i++) {
        if (mode === modes.exclusiveModes[i]) return mode;
      }

      for (let i = 0; i < modes.accessModes.length; i++) {
        const accessMode = modes.accessModes[i].mode;
        const index = mode.indexOf(accessMode); // Set value if mode matches and next character is not an underscore
        // (otherwise we might incorrectly return BICYCLE instead of BICYCLE_RENT).

        if (index !== -1 && mode[index + accessMode.length] !== '_') {
          return `TRANSIT,${accessMode}`;
        }
      } // Default to transit


      return 'TRANSIT';
    });

    _defineProperty(this, "_getModeOptions", () => {
      const {
        modes
      } = this.props;
      return [{
        children: 'Transit',
        mode: 'TRANSIT',
        value: 'TRANSIT'
      }, ...modes.exclusiveModes.map(mode => ({
        children: `${(0, _itinerary.toSentenceCase)(mode)} only`,
        mode,
        value: `${mode}`
      })), ...modes.accessModes.map(m => ({ ...m,
        children: m.label,
        value: `TRANSIT,${m.mode}`
      }))];
    });

    _defineProperty(this, "_onChange", evt => {
      const {
        value: mode
      } = evt.target;
      const {
        updateTransitModes
      } = this.props;

      const selectedOption = this._getModeOptions().filter(o => o.value === mode);

      const transitIsSelected = mode.indexOf('TRANSIT') !== -1;

      if (transitIsSelected) {
        // Collect transit modes and selected access mode.
        const accessMode = mode === 'TRANSIT' ? 'WALK' : mode.replace('TRANSIT,', ''); // If no transit is selected, selected all available. Otherwise, default
        // to state.

        const transitModes = this.props.selectedTransitModes.length > 0 ? this.props.selectedTransitModes : this.props.modes.transitModes.map(m => m.mode);
        const newModes = [...transitModes, accessMode];
        if (accessMode.indexOf('CAR') !== -1) newModes.push('WALK');
        updateTransitModes(transitModes);
        const params = {
          mode: newModes.join(',')
        }; // Update companies if provided by selected option.

        if (selectedOption && selectedOption.company) {
          params.companies = selectedOption.company;
        }

        this.props.onChange(params);
      } else {
        this.props.onChange({
          mode
        });
      }
    });
  }

  render() {
    const {
      mode,
      onKeyDown
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("select", {
      onBlur: this._setMode,
      onChange: this._onChange,
      onKeyDown: onKeyDown,
      value: this.modeToOptionValue(mode)
    }, this._getModeOptions().map(o => /*#__PURE__*/_react.default.createElement("option", _extends({
      key: o.value
    }, o))));
  }

}

exports.default = ModeDropdown;
module.exports = exports.default;

//# sourceMappingURL=mode-dropdown.js