"use strict";

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

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

var _formik = require("formik");

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

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

var _reactBootstrap = require("react-bootstrap");

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

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

var _ui = require("../../../util/ui");

var _user = require("../../../util/user");

var _placeLocationField = require("./place-location-field");

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 {
  isMobile
} = _coreUtils.default.ui; // Styled components

const LargeIcon = (0, _styledComponents.default)(_icon.default)`
  font-size: 150%;
`;
const FixedPlaceIcon = (0, _styledComponents.default)(LargeIcon)`
  margin-right: 10px;
  padding-top: 6px;
`;
const FlexContainer = _styledComponents.default.div`
  display: flex;
`;
const FlexFormGroup = (0, _styledComponents.default)(_reactBootstrap.FormGroup)`
  flex-grow: 1;
`;
const StyledToggleButtonGroup = (0, _styledComponents.default)(_reactBootstrap.ToggleButtonGroup)`
  & > label {
    padding: 5px;
  }
`;
const MESSAGES = {
  SET_PLACE_NAME: 'Set place name'
};
/**
 * Contains the fields for editing a favorite place.
 * This component uses Formik props that are passed
 * within the Formik context set up by FavoritePlaceScreen.
 */

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

    _defineProperty(this, "_handleLocationChange", ({
      location
    }) => {
      const {
        setValues,
        values
      } = this.props;
      const {
        lat,
        lon,
        name
      } = location;
      setValues({ ...values,
        address: name,
        lat,
        lon
      });
    });
  }

  render() {
    const {
      errors,
      handleBlur,
      handleChange,
      values: place
    } = this.props;
    const isFixed = (0, _user.isHomeOrWork)(place);
    const errorStates = (0, _ui.getErrorStates)(this.props);
    return /*#__PURE__*/_react.default.createElement("div", null, !isFixed && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactBootstrap.FormGroup, {
      validationState: errorStates.name
    }, /*#__PURE__*/_react.default.createElement(_formik.Field, {
      "aria-label": MESSAGES.SET_PLACE_NAME,
      as: _reactBootstrap.FormControl,
      name: "name",
      placeholder: MESSAGES.SET_PLACE_NAME
    }), /*#__PURE__*/_react.default.createElement(_reactBootstrap.FormControl.Feedback, null), errors.name && /*#__PURE__*/_react.default.createElement(_reactBootstrap.HelpBlock, null, errors.name)), /*#__PURE__*/_react.default.createElement(_reactBootstrap.FormGroup, null, /*#__PURE__*/_react.default.createElement(StyledToggleButtonGroup, {
      defaultValue: place.type,
      name: "type",
      type: "radio"
    }, Object.values(_user.CUSTOM_PLACE_TYPES).map(({
      icon,
      text,
      type
    }) => /*#__PURE__*/_react.default.createElement(_reactBootstrap.ToggleButton, {
      key: type // onBlur and onChange have to be set on individual controls instead of the control group
      // in order for Formik to correctly process the changes.
      ,
      onBlur: handleBlur,
      onChange: handleChange,
      title: text,
      value: type
    }, /*#__PURE__*/_react.default.createElement(LargeIcon, {
      name: icon
    })))))), /*#__PURE__*/_react.default.createElement(FlexContainer, null, isFixed && /*#__PURE__*/_react.default.createElement(FixedPlaceIcon, {
      name: place.icon
    }), /*#__PURE__*/_react.default.createElement(FlexFormGroup, {
      validationState: errorStates.address
    }, /*#__PURE__*/_react.default.createElement(_placeLocationField.PlaceLocationField, {
      className: "form-control",
      inputPlaceholder: isFixed ? `Search for ${place.name} location` : 'Search for location',
      location: (0, _placeLocationField.makeLocationFieldLocation)(place),
      locationType: "to",
      onLocationSelected: this._handleLocationChange,
      showClearButton: false,
      static: isMobile()
    }), /*#__PURE__*/_react.default.createElement(_reactBootstrap.FormControl.Feedback, null), errors.address && /*#__PURE__*/_react.default.createElement(_reactBootstrap.HelpBlock, null, errors.address))));
  }

}

var _default = PlaceEditor;
exports.default = _default;
module.exports = exports.default;

//# sourceMappingURL=place-editor.js