
/**
 * Module dependencies.
 */

var CommentLike = require('./commentlike');
var debug = require('debug')('wpcom:comment');

/**
 * Comment methods
 *
 * @param {String} [cid] comment id
 * @param {String} [pid] post id
 * @param {String} sid site id
 * @param {WPCOM} wpcom
 * @api public
 */

function Comment(cid, pid, sid, wpcom) {
  if (!sid) {
    throw new Error('`site id` is not correctly defined');
  }

  if (!(this instanceof Comment)) {
    return new Comment(cid, pid, sid, wpcom);
  }

  this.wpcom = wpcom;
  this._cid = cid;
  this._pid = pid;
  this._sid = sid;
}

/**
 * Return a single Comment
 *
 * @param {Object} [query]
 * @param {Function} fn
 * @api public
 */

Comment.prototype.get = function (query, fn) {
  var path = '/sites/' + this._sid + '/comments/' + this._cid;
  return this.wpcom.req.get(path, query, fn);
};

/**
 * Return recent comments for a post
 *
 * @param {Object} [query]
 * @param {Function} fn
 * @api public
 */

Comment.prototype.replies = function (query, fn) {
  var path = '/sites/' + this._sid + '/posts/' + this._pid + '/replies/';
  return this.wpcom.req.get(path, query, fn);
};

/**
 * Create a comment on a post
 *
 * @param {Object} [query]
 * @param {String|Object} body
 * @param {Function} fn
 * @api public
 */

Comment.prototype.add = function (query, body, fn) {
  if ('function' === typeof body) {
    fn = body;
    body = query;
    query = {};
  }

  body = 'string' === typeof body ? { content: body } : body;

  var path = '/sites/' + this._sid + '/posts/' + this._pid + '/replies/new';
  return this.wpcom.req.post(path, query, body, fn);
};

/**
 * Edit a comment
 *
 * @param {Object} [query]
 * @param {String|Object} body
 * @param {Function} fn
 * @api public
 */

Comment.prototype.update = function (query, body, fn) {
  if ('function' === typeof body) {
    fn = body;
    body = query;
    query = {};
  }

  body = 'string' === typeof body ? { content: body } : body;

  var path = '/sites/' + this._sid + '/comments/' + this._cid;
  return this.wpcom.req.put(path, query, body, fn);
};

/**
 * Create a Comment as a reply to another Comment
 *
 * @param {Object} [query]
 * @param {String|Object} body
 * @param {Function} fn
 * @api public
 */

Comment.prototype.reply = function (query, body, fn) {
  if ('function' === typeof body) {
    fn = body;
    body = query;
    query = {};
  }
  
  body = 'string' === typeof body ? { content: body } : body;

  var path = '/sites/' + this._sid + '/comments/' + this._cid + '/replies/new';
  return this.wpcom.req.post(path, query, body, fn);
};

/**
 * Delete a comment
 *
 * @param {Object} [query]
 * @param {Function} fn
 * @api public
 */

Comment.prototype.del =
Comment.prototype['delete'] = function (query, fn) {
  var path = '/sites/' + this._sid + '/comments/' + this._cid + '/delete';
  return this.wpcom.req.del(path, query, fn);
};

/**
 * Create a `CommentLike` instance
 *
 * @api public
 */

Comment.prototype.like = function() {
  return CommentLike(this._cid, this._sid, this.wpcom);
};

/**
 * Get comment likes list
 *
 * @param {Object} [query]
 * @param {Function} fn
 * @api public
 */

Comment.prototype.likesList = function (query, fn) {
  var path = '/sites/' + this._sid + '/comments/' + this._cid + '/likes';
  return this.wpcom.req.get(path, query, fn);
};

/**
 * Expose `Comment` module
 */

module.exports = Comment;
@@ -1322,32 +1322,140 @@
 dy, fn) %7B%0A  if (
+ undefined === fn ) %7B%0A    if ( undefined === body ) %7B%0A      body = query;%0A      query = %7B%7D;%0A    %7D else if ( 
 'function' === t
@@ -1460,32 +1460,33 @@
  typeof body
+ 
 ) %7B%0A    
 fn = body;%0A 
@@ -1465,32 +1465,34 @@
 of body ) %7B%0A    
+ 
+ 
 fn = body;%0A    b
@@ -1478,32 +1478,34 @@
      fn = body;%0A
+  
     body = query
@@ -1502,32 +1502,34 @@
 dy = query;%0A    
+ 
+ 
 query = %7B%7D;%0A  %7D%0A
@@ -1516,32 +1516,38 @@
     query = %7B%7D;%0A
+    %7D%0A
   %7D%0A%0A  body = 's
@@ -2494,11 +2494,9 @@
   %7D%0A
-  
 %0A
+
   bo
@@ -3468,16 +3468,16 @@
 le%0A */%0A%0A
-
 module.e
@@ -3493,8 +3493,9 @@
 Comment;
+%0A

Prepare `comment` for promises