'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CommentForm = undefined;

var _textarea, _arrow;

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; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _radium = require('radium');

var _radium2 = _interopRequireDefault(_radium);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _marger = require('kitten/components/layout/marger');

var _commentAvatar = require('kitten/components/form/comment-avatar');

var _buttonImage = require('kitten/components/buttons/button-image');

var _button = require('kitten/components/buttons/button');

var _text = require('kitten/components/typography/text');

var _screenConfig = require('kitten/constants/screen-config');

var _colorsConfig = require('kitten/constants/colors-config');

var _colorsConfig2 = _interopRequireDefault(_colorsConfig);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 Marger = (0, _radium2.default)(_marger.Marger);
var Button = (0, _radium2.default)(_button.Button);

var CommentForm = exports.CommentForm = function (_Component) {
  _inherits(CommentForm, _Component);

  function CommentForm(props) {
    _classCallCheck(this, CommentForm);

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

    _this.handleFocus = function () {
      _this.setState({ isFocused: true });
    };

    _this.handleBlur = function () {
      _this.setState({ isFocused: false });
    };

    _this.handleChange = function (e) {
      var element = e.target;

      _this.setState({
        value: element.value,
        height: 'auto'
      }, function () {
        _this.setState({
          height: element.scrollHeight
        });
      });
    };

    _this.handleSubmit = function () {
      _this.props.onSubmit(_this.state.value);
    };

    _this.state = {
      isFocused: false,
      value: _this.props.defaultValue,
      height: 'auto'
    };
    return _this;
  }

  _createClass(CommentForm, [{
    key: 'render',
    value: function render() {
      var avatarImgProps = this.props.avatarImgProps;


      return _react2.default.createElement(
        _radium.StyleRoot,
        null,
        _react2.default.createElement(
          'div',
          { style: styles.grid },
          _react2.default.createElement(_commentAvatar.CommentAvatar, { avatarImgProps: avatarImgProps }),
          this.renderInput()
        )
      );
    }
  }, {
    key: 'renderInput',
    value: function renderInput() {
      var _props = this.props,
          isDisabled = _props.isDisabled,
          placeholder = _props.placeholder,
          defaultValue = _props.defaultValue,
          error = _props.error,
          errorMessage = _props.errorMessage;


      var styleInput = [styles.input.textarea, error && styles.input.textarea.error, this.state.isFocused && styles.input.textarea.focus, isDisabled && styles.input.textarea.isDisabled, { height: this.state.height }];

      var styleArrow = [styles.input.arrow, error && styles.input.arrow.error, this.state.isFocused && styles.input.arrow.focus];

      var textareaClassNames = (0, _classnames2.default)('k-CommentForm__input', 'k-u-weight-light');

      // We are forced to duplicate <Style />, to avoid having space between the class and the pseudo-element.
      // https://github.com/FormidableLabs/radium/issues/243
      return _react2.default.createElement(
        _react.Fragment,
        null,
        _react2.default.createElement(_radium.Style, {
          scopeSelector: '.k-CommentForm__input::-webkit-input-placeholder',
          rules: { color: _colorsConfig2.default.font2 }
        }),
        _react2.default.createElement(_radium.Style, {
          scopeSelector: '.k-CommentForm__input:-moz-placeholder',
          rules: { color: _colorsConfig2.default.font2 }
        }),
        _react2.default.createElement(_radium.Style, {
          scopeSelector: '.k-CommentForm__input::-moz-placeholder',
          rules: { color: _colorsConfig2.default.font2 }
        }),
        _react2.default.createElement(_radium.Style, {
          scopeSelector: '.k-CommentForm__input:-ms-input-placeholder',
          rules: { color: _colorsConfig2.default.font2 }
        }),
        _react2.default.createElement(
          'div',
          { style: styles.grid.col },
          _react2.default.createElement(
            Marger,
            { bottom: '.5', style: styles.input },
            _react2.default.createElement('textarea', {
              className: textareaClassNames,
              style: styleInput,
              defaultValue: defaultValue,
              key: 'comment-form',
              disabled: isDisabled,
              placeholder: placeholder,
              onFocus: this.handleFocus,
              onBlur: this.handleBlur,
              onChange: this.handleChange,
              rows: '1'
            }),
            _react2.default.createElement(
              'span',
              { style: styleArrow },
              _react2.default.createElement('span', { style: styles.input.arrow.before })
            )
          ),
          this.renderError(),
          this.renderButton()
        )
      );
    }
  }, {
    key: 'renderButton',
    value: function renderButton() {
      if (!this.state.value) return;

      var commentButton = this.props.commentButton;


      return _react2.default.createElement(
        Marger,
        { top: '2' },
        _react2.default.createElement(
          Button,
          {
            type: 'button',
            modifier: 'helium',
            onClick: this.handleSubmit,
            style: styles.button.left
          },
          commentButton
        )
      );
    }
  }, {
    key: 'renderError',
    value: function renderError() {
      var _props2 = this.props,
          error = _props2.error,
          errorMessage = _props2.errorMessage;


      if (!error) return;

      return _react2.default.createElement(
        Marger,
        { top: '.5' },
        _react2.default.createElement(
          _text.Text,
          { color: 'error', size: 'micro', weight: 'regular' },
          errorMessage
        )
      );
    }
  }]);

  return CommentForm;
}(_react.Component);

CommentForm.propTypes = {
  avatarImgProps: _propTypes2.default.object.isRequired,

  isDisabled: _propTypes2.default.bool,
  placeholder: _propTypes2.default.string.isRequired,

  commentButton: _propTypes2.default.string,
  error: _propTypes2.default.bool,
  errorMessage: _propTypes2.default.string,
  onSubmit: _propTypes2.default.func,
  defaultValue: _propTypes2.default.string
};
CommentForm.defaultProps = {
  onSubmit: function onSubmit() {},
  error: false,
  errorMessage: '',
  isDisabled: false,
  commentButton: '',
  defaultValue: ''
};


var styles = {
  grid: {
    display: 'flex',
    col: _defineProperty({
      flex: 1,
      marginLeft: 20
    }, '@media (min-width: ' + _screenConfig.ScreenConfig['S'].min + 'px)', {
      marginLeft: 35
    })
  },

  input: {
    display: 'flex',
    position: 'relative',

    textarea: (_textarea = {
      width: '100%',
      overflowY: 'hidden',
      resize: 'none',
      boxSizing: 'border-box',
      borderWidth: 2,
      borderStyle: 'solid',
      borderColor: _colorsConfig2.default.line1,
      color: _colorsConfig2.default.font1,
      padding: 30,
      fontSize: 14
    }, _defineProperty(_textarea, '@media (min-width: ' + _screenConfig.ScreenConfig['S'].min + 'px)', {
      fontSize: 16
    }), _defineProperty(_textarea, 'focus', {
      outline: 'none',
      borderColor: _colorsConfig2.default.line2,
      color: _colorsConfig2.default.font1
    }), _defineProperty(_textarea, 'isDisabled', {
      borderColor: _colorsConfig2.default.line1,
      color: _colorsConfig2.default.font2,
      backgroundColor: _colorsConfig2.default.line1
    }), _defineProperty(_textarea, 'error', {
      borderColor: _colorsConfig2.default.error3,
      color: _colorsConfig2.default.error3
    }), _textarea),

    arrow: (_arrow = {
      position: 'absolute',
      top: 20,
      display: 'block',
      width: 0,
      height: 0,
      borderWidth: 10,
      borderStyle: 'solid',
      borderColor: 'transparent',
      borderRightColor: _colorsConfig2.default.line1,
      left: -20
    }, _defineProperty(_arrow, '@media (min-width: ' + _screenConfig.ScreenConfig['S'].min + 'px)', {
      top: 35
    }), _defineProperty(_arrow, 'focus', {
      borderRightColor: _colorsConfig2.default.line2
    }), _defineProperty(_arrow, 'error', {
      borderRightColor: _colorsConfig2.default.error3
    }), _defineProperty(_arrow, 'before', {
      position: 'absolute',
      width: 0,
      height: 0,
      marginTop: -10,
      borderWidth: 10,
      borderStyle: 'solid',
      borderColor: 'transparent',
      borderRightColor: 'white',
      left: -7
    }), _arrow)
  },

  button: {
    left: {
      marginRight: 10
    }
  }
};