"use strict";

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

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

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

var _reactIntl = require("react-intl");

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

var _connectedLinks = require("../form/connected-links");

var _constants = require("../../util/constants");

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

var _itinerary = require("../../util/itinerary");

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

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

/**
 * This connected component encapsulates the states and behavior of the button
 * to save an itinerary for notifications.
 */
const SaveTripButton = ({
  itinerary,
  loggedInUser,
  persistence
}) => {
  const intl = (0, _reactIntl.useIntl)(); // We are dealing with the following states:
  // 1. Persistence disabled => just return null
  // 2. User is not logged in => render something like: "Please sign in to save trip".
  // 3. itin cannot be monitored => disable the button with prompt and tooltip.

  let buttonDisabled;
  let buttonText;
  let tooltipText;
  let iconType;

  if (!persistence || !persistence.enabled) {
    return null;
  } else if (!loggedInUser) {
    buttonDisabled = true;
    buttonText = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "components.SaveTripButton.signInText"
    });
    iconType = 'lock';
    tooltipText = intl.formatMessage({
      id: 'components.SaveTripButton.signInTooltip'
    });
  } else if (!(0, _itinerary.itineraryCanBeMonitored)(itinerary)) {
    buttonDisabled = true;
    buttonText = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "components.SaveTripButton.cantSaveText"
    });
    iconType = 'ban';
    tooltipText = intl.formatMessage({
      id: 'components.SaveTripButton.cantSaveTooltip'
    });
  } else {
    buttonText = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, {
      id: "components.SaveTripButton.saveTripText"
    });
    iconType = 'plus-circle';
  }

  const button = /*#__PURE__*/_react.default.createElement("button", {
    // Apply pull-right style class so that the element is flush right,
    // even with LineItineraries.
    className: "clear-button-formatting pull-right",
    disabled: buttonDisabled,
    style: buttonDisabled ? {
      pointerEvents: 'none'
    } : {}
  }, /*#__PURE__*/_react.default.createElement(_icon.default, {
    type: iconType,
    withSpace: true
  }), buttonText); // Show tooltip with help text if button is disabled.


  if (buttonDisabled) {
    return /*#__PURE__*/_react.default.createElement(_reactBootstrap.OverlayTrigger, {
      overlay: /*#__PURE__*/_react.default.createElement(_reactBootstrap.Tooltip, {
        id: "disabled-save-tooltip"
      }, tooltipText),
      placement: "top"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: "pull-right",
      style: {
        cursor: 'not-allowed'
      }
    }, button));
  }

  return /*#__PURE__*/_react.default.createElement(_connectedLinks.LinkContainerWithQuery, {
    to: _constants.CREATE_TRIP_PATH
  }, button);
}; // connect to the redux store


const mapStateToProps = (state, ownProps) => {
  const {
    persistence
  } = state.otp.config;
  return {
    itinerary: (0, _state.getActiveItinerary)(state),
    loggedInUser: state.user.loggedInUser,
    persistence
  };
};

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

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

//# sourceMappingURL=save-trip-button.js