'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var React = require('react');
var PropTypes = require('prop-types');

var _require = require('hadron-react-buttons'),
    TextButton = _require.TextButton;

var _require2 = require('hadron-react-components'),
    Tooltip = _require2.Tooltip;

var ReadStateStore = require('../stores/read-state-store');

/**
 * The wrapper class.
 */
var WRAPPER = 'tooltip-button-wrapper';

/**
 * Button component that is aware of the read state of the application.
 * This button contains only text, no icons, no animations.
 */

var TextReadButton = function (_React$Component) {
  _inherits(TextReadButton, _React$Component);

  /**
   * Instantiate the component.
   *
   * @param {Object} props - The properties.
   */
  function TextReadButton(props) {
    _classCallCheck(this, TextReadButton);

    var _this = _possibleConstructorReturn(this, (TextReadButton.__proto__ || Object.getPrototypeOf(TextReadButton)).call(this, props));

    _this.state = ReadStateStore.state;
    return _this;
  }

  /**
   * Subscribe to the state changing stores.
   */


  _createClass(TextReadButton, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.unsubscribeReadState = ReadStateStore.listen(this.readStateChanged.bind(this));
    }

    /**
     * Unsubscribe from the stores.
     */

  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.unsubscribeReadState();
    }

    /**
     * Handle read state changes.
     *
     * @param {Object} state - The read state.
     */

  }, {
    key: 'readStateChanged',
    value: function readStateChanged(state) {
      this.setState(state);
    }

    /**
     * Get the tooltip text.
     *
     * @returns {String} The tooltip text.
     */

  }, {
    key: 'tooltipText',
    value: function tooltipText() {
      if (!this.state.isReadable) {
        return ReadStateStore.state.description;
      }
    }

    /**
     * Render the component.
     *
     * @returns {React.Component} The rendered component.
     */

  }, {
    key: 'render',
    value: function render() {
      var tooltip = this.state.isReadable ? null : React.createElement(Tooltip, { id: this.props.tooltipId });
      return React.createElement(
        'div',
        { className: WRAPPER, 'data-tip': this.tooltipText(), 'data-for': this.props.tooltipId },
        React.createElement(TextButton, {
          id: this.props.id,
          className: this.props.className,
          dataTestId: this.props.dataTestId,
          disabled: !this.state.isReadable || this.props.disabled,
          clickHandler: this.props.clickHandler,
          style: this.props.style,
          text: this.props.text }),
        tooltip
      );
    }
  }]);

  return TextReadButton;
}(React.Component);

TextReadButton.propTypes = {
  className: PropTypes.string.isRequired,
  clickHandler: PropTypes.func.isRequired,
  dataTestId: PropTypes.string,
  disabled: PropTypes.bool,
  id: PropTypes.string,
  style: PropTypes.object,
  text: PropTypes.string.isRequired,
  tooltipId: PropTypes.string.isRequired
};

TextReadButton.displayName = 'TextReadButton';

module.exports = TextReadButton;