'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 ValidationActions = require('../actions');

var _require = require('react-bootstrap'),
    FormGroup = _require.FormGroup,
    FormControl = _require.FormControl;

/**
 * Component to select a field for which the rule applies.
 *
 * @todo this is currently a simple input field but needs to become a
 * select2 type auto-complete/dropdown component.
 *
 * @see https://jedwatson.github.io/react-select/
 */


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

  /**
   * constructor sets the initial state.
   *
   * @param {Object} props   initial props, passed to super class.
   */
  function RuleFieldSelector(props) {
    _classCallCheck(this, RuleFieldSelector);

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

    _this.state = {
      value: _this.props.field,
      isValid: true,
      hasStartedValidating: false
    };
    return _this;
  }

  _createClass(RuleFieldSelector, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(props) {
      this.setState({
        value: props.field
      });
    }

    /**
     * The content of the input field has changed (onChange). Update internal
     * state but do not trigger an action to change the store yet.
     *
     * @param {Object} evt   event triggered on onBlur.
     */

  }, {
    key: 'onFieldChanged',
    value: function onFieldChanged(evt) {
      this.setState({
        value: evt.target.value
      });
    }

    /**
     * The input field has lost focus (onBlur). Trigger an action to inform
     * the store of the change.
     */

  }, {
    key: 'onBlur',
    value: function onBlur() {
      this.validate(true);
      ValidationActions.setRuleField(this.props.id, this.state.value);
    }
  }, {
    key: 'validate',
    value: function validate(force) {
      if (!force && !this.state.hasStartedValidating) {
        return true;
      }
      var isValid = this.state.value !== '';
      this.setState({
        isValid: isValid,
        hasStartedValidating: true
      });
      return isValid;
    }

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

  }, {
    key: 'render',
    value: function render() {
      var validationState = this.state.isValid ? null : 'error';
      return React.createElement(
        FormGroup,
        { validationState: validationState },
        React.createElement(FormControl, {
          type: 'text',
          placeholder: 'Enter field name',
          id: this.props.id,
          value: this.state.value,
          onChange: this.onFieldChanged.bind(this),
          onBlur: this.onBlur.bind(this),
          disabled: !this.props.isWritable
        })
      );
    }
  }]);

  return RuleFieldSelector;
}(React.Component);

RuleFieldSelector.propTypes = {
  id: PropTypes.string.isRequired,
  field: PropTypes.string.isRequired,
  isWritable: PropTypes.bool
};

RuleFieldSelector.displayName = 'RuleFieldSelector';

module.exports = RuleFieldSelector;