'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 WriteStateStore = require('../stores/write-state-store');

/**
 * The collection store name.
 */
var COLLECTION_STORE = 'App.CollectionStore';

/**
 * The readonly collection message.
 */
var READONLY = 'Write operations are not permitted on readonly collections.';

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

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

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

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

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

    _this.CollectionStore = global.hadronApp.appRegistry.getStore(COLLECTION_STORE);
    _this.boundNamespaceChanged = _this.namespaceChanged.bind(_this);
    return _this;
  }

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


  _createClass(TextWriteButton, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      global.hadronApp.appRegistry.on('namespace-changed', this.boundNamespaceChanged);
      this.unsubscribeWriteState = WriteStateStore.listen(this.writeStateChanged.bind(this));
    }

    /**
     * Unsubscribe from the stores.
     */

  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      global.hadronApp.appRegistry.removeListener('namespace-changed', this.boundNamespaceChanged);
      this.unsubscribeWriteState();
    }

    /**
     * Determine if the application is in a writable state.
     *
     * @returns {Boolean} If the application is writable.
     */

  }, {
    key: 'isWritable',
    value: function isWritable() {
      var isWritable = WriteStateStore.state.isWritable;
      if (!this.props.isCollectionLevel) {
        return isWritable;
      }
      return isWritable && !this.CollectionStore.isReadonly();
    }

    /**
     * Handle namespace changes.
     */

  }, {
    key: 'namespaceChanged',
    value: function namespaceChanged() {
      this.forceUpdate();
    }

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

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

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

  }, {
    key: 'tooltipText',
    value: function tooltipText() {
      if (!this.isWritable()) {
        if (this.props.isCollectionLevel && this.CollectionStore.isReadonly()) {
          return READONLY;
        }
        return WriteStateStore.state.description;
      }
    }

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

  }, {
    key: 'render',
    value: function render() {
      var tooltip = this.isWritable() ? 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, {
          className: this.props.className,
          dataTestId: this.props.dataTestId,
          disabled: !this.isWritable(),
          clickHandler: this.props.clickHandler,
          text: this.props.text }),
        tooltip
      );
    }
  }]);

  return TextWriteButton;
}(React.Component);

TextWriteButton.propTypes = {
  className: PropTypes.string.isRequired,
  clickHandler: PropTypes.func.isRequired,
  dataTestId: PropTypes.string,
  isCollectionLevel: PropTypes.bool,
  text: PropTypes.string.isRequired,
  tooltipId: PropTypes.string.isRequired
};

TextWriteButton.displayName = 'TextWriteButton';

module.exports = TextWriteButton;