'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('react-bootstrap'),
    Button = _require.Button;

var FontAwesome = require('react-fontawesome');

// const debug = require('debug')('mongodb-compass:validation:editable');

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

  function Editable() {
    _classCallCheck(this, Editable);

    return _possibleConstructorReturn(this, (Editable.__proto__ || Object.getPrototypeOf(Editable)).apply(this, arguments));
  }

  _createClass(Editable, [{
    key: 'getButtons',
    value: function getButtons() {
      if (this.props.editState === 'modified') {
        return React.createElement(
          'div',
          { className: 'pull-right' },
          React.createElement(
            Button,
            {
              bsStyle: 'link',
              bsSize: 'xsmall',
              className: 'btn-borderless',
              onClick: this.props.onCancel },
            'Cancel'
          ),
          React.createElement(
            Button,
            {
              bsSize: 'xsmall',
              bsStyle: 'default',
              className: 'btn-edit',
              onClick: this.props.onUpdate },
            'Update'
          )
        );
      }
      if (this.props.editState === 'error') {
        return React.createElement(
          'div',
          { className: 'pull-right' },
          React.createElement(
            Button,
            {
              bsStyle: 'link',
              bsSize: 'xsmall',
              className: 'btn-borderless',
              onClick: this.props.onCancel },
            'Cancel'
          )
        );
      }
      return null;
    }
  }, {
    key: 'getMessage',
    value: function getMessage() {
      /* eslint complexity: 0 */
      var name = this.props.childName;
      var errorMsg = this.props.errorMessage;

      switch (this.props.editState) {
        case 'unmodified':
          return '';
        case 'modified':
          return name ? name + ' modified' : 'Modified';
        case 'updating':
          return name ? 'Updating ' + name + '...' : 'Updating...';
        case 'success':
          return name ? name + ' updated.' : 'Updated';
        case 'error':
          if (errorMsg) {
            return name ? name + ' could not be updated: ' + errorMsg : 'Error: ' + errorMsg;
          }
          return name ? name + ' could not be updated.' : 'An error occurred during the update.';
        default:
          return '';
      }
    }
  }, {
    key: 'getSymbol',
    value: function getSymbol() {
      switch (this.props.editState) {
        case 'unmodified':
          return null;
        case 'modified':
          return null;
        case 'updating':
          return React.createElement(FontAwesome, { name: 'spinner', spin: true });
        case 'success':
          return React.createElement(FontAwesome, { name: 'check' });
        case 'error':
          return React.createElement(FontAwesome, { name: 'exclamation-triangle' });
        default:
          return '';
      }
    }

    /**
     * Render view switcher component.
     *
     * @returns {React.Component} The component.
     */

  }, {
    key: 'render',
    value: function render() {
      var message = this.getMessage();
      var buttons = this.getButtons();
      var symbol = this.getSymbol();

      return React.createElement(
        'div',
        { className: 'editable editable-is-' + this.props.editState },
        React.createElement(
          'div',
          { className: 'editable-wrapper' },
          React.createElement(
            'div',
            { className: 'editable-content', ref: 'content' },
            React.cloneElement(this.props.children, { ref: 'child' })
          ),
          React.createElement(
            'div',
            { className: 'editable-statusbar' },
            React.createElement(
              'span',
              null,
              symbol,
              '\xA0',
              message
            ),
            buttons
          )
        )
      );
    }
  }]);

  return Editable;
}(React.Component);

Editable.propTypes = {
  children: PropTypes.node.isRequired,
  editState: PropTypes.oneOf(['unmodified', 'modified', 'updating', 'success', 'error']).isRequired,
  childName: PropTypes.string,
  onCancel: PropTypes.func,
  onUpdate: PropTypes.func,
  errorMessage: PropTypes.string
};

Editable.defaultProps = {
  editState: 'unmodified',
  onCancel: function onCancel() {},
  onUpdate: function onUpdate() {}
};

Editable.displayName = 'Editable';

module.exports = Editable;