var RestClient = require('carbon-client')
var util = require('util')
var fibrous = require('fibrous');

/****************************************************************************************************
 * monkey patch the endpoint class to support sync get/post/put/delete/head/patch
 */

var Endpoint = RestClient.super_

/****************************************************************************************************
 * syncifyClassMethod
 *
 * @param clazz the class
 * @param methodName name of the method to syncify
 */

function syncifyClassMethod(clazz, methodName) {

  var asyncMethod = clazz.prototype[methodName]

  clazz.prototype[methodName] = function() {
    // if last argument is a callback then run async
    if (arguments && (typeof(arguments[arguments.length-1]) === 'function') ) {
      asyncMethod.apply(this, arguments);
    } else { // sync call!
      return asyncMethod.sync.apply(this, arguments);
    }
  }

}
/****************************************************************************************************
 * syncify all Endpoint methods
 */
var ENDPOINT_METHODS = [
  "get",
  "post",
  "head",
  "put",
  "delete",
  "patch"
]

ENDPOINT_METHODS.forEach(function(m) { syncifyClassMethod(Endpoint, m) })

/****************************************************************************************************
 * syncify Collection methods
 */

var COLLECTION_METHODS = [
  "insert",
  "update",
  "remove"
]

var Collection = Endpoint.collectionClass
var CollectionCursor = Collection.collectionCursorClass

COLLECTION_METHODS.forEach(function(m) { syncifyClassMethod(Collection, m) })

/****************************************************************************************************
 * syncify CollectionCursor methods
 */

syncifyClassMethod(CollectionCursor, "next")
syncifyClassMethod(CollectionCursor, "toArray")


/****************************************************************************************************
 *
 * @param cb
 * eachSync: sync version of each()
 * TODO: XXX this method is causing program to hang
 */
CollectionCursor.prototype.eachSync = function(cb) {
  return this.each.sync.apply(this, arguments)
}
/****************************************************************************************************
 * exports
 */
module.exports = RestClient

@@ -1491,26 +1491,16 @@
 ass%0Avar 
-Collection
 Cursor =
@@ -1512,26 +1512,16 @@
 ection.c
-ollectionC
 ursorCla
@@ -1716,26 +1716,16 @@
 syncify 
-Collection
 Cursor m
@@ -1751,34 +1751,24 @@
 ClassMethod(
-Collection
 Cursor, %22nex
@@ -1790,26 +1790,16 @@
 sMethod(
-Collection
 Cursor, 
@@ -2021,26 +2021,16 @@
 ang%0A */%0A
-Collection
 Cursor.p

rename CollectionCursor to Cursor