"use strict";

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

var _constant = require("../../constant");

var _Comment = require("../../Comment");

var _Comment2 = _interopRequireDefault(_Comment);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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 CommentInput = function (_Component) {
  _inherits(CommentInput, _Component);

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

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

    _this.state = {};
    _this.handleSubmit = _this.handleSubmit.bind(_this);
    return _this;
  }

  /**
   * 提交评论
   * @param {object} { text<string>, files<array> } 需要提交的评论的文本和图片
   * @param {function} cb 提交成功后的回掉
   */


  _createClass(CommentInput, [{
    key: "handleSubmit",
    value: function handleSubmit(_ref, cb) {
      var text = _ref.text,
          files = _ref.files;

      var value = text;
      if (files && files.length) {
        value += _constant.IMAGE_SPLIT;
        files.forEach(function (file) {
          value += file + ",";
        });
      }
      if (value.substr(-1) === ",") {
        value = value.slice(0, -1);
      }

      var _props = this.props,
          action = _props.action,
          commentId = _props.commentId,
          replyId = _props.replyId,
          callback = _props.callback;

      if (action === "comment") {
        this.props.app.sCreateComment({
          content: value
        }, cb);
      } else if (action === "reply") {
        this.props.app.sCreateReply({
          comment_id: commentId,
          content: value
        }, function () {
          return callback && callback();
        });
      } else if (action === "replyToReply") {
        this.props.app.sCreateReply({
          comment_id: commentId,
          content: value,
          reply_id: replyId
        }, function () {
          return callback && callback();
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var childrenWithProps = _react2.default.Children.map(this.props.content, function (child) {
        return _react2.default.cloneElement(child, _extends({
          // 编辑器本身不提交值，但 CommentInput 会提交
          // CommentInput 主要是负责评论的业务逻辑，提交评论和回复
          // 默认使用 CommentInput 的 onSubmit 来提交评论
          // 但也可以使用 Editor 的 props 来覆盖 onSubmit
          onSubmit: _this2.handleSubmit
        }, child.props, {
          // 如果当前的编辑器不是“评论”，则编辑器高度减小一些
          rows: _this2.props.action === "comment" ? child.props.rows : child.props.rows - 1
        }));
      });

      return _react2.default.createElement(
        "div",
        null,
        childrenWithProps
      );
    }
  }]);

  return CommentInput;
}(_react.Component);

CommentInput.propTypes = {
  // comment 评论
  // reply 评论的回复
  // replyToReply 回复的回复
  action: _propTypes2.default.oneOf(["comment", "reply", "replyToReply"])
};

CommentInput.defaultProps = {
  action: "comment"
};

exports.default = (0, _Comment2.default)(CommentInput);
//# sourceMappingURL=index.js.map