"use strict";

var _ = require('underscore')
  , Q = require('q')
  , simctl = require("node-simctl");

var nodeify = function (obj) {
  if (typeof obj !== "function") {
    var nodeified = {};
    _.each(Object.getOwnPropertyNames(obj), function (name) {
      if (name !== "__esModule") {
        nodeified[name] = nodeify(obj[name]);
      }
    });
    return nodeified;
  }
  return function () {
    var args = Array.prototype.slice.call(arguments, 0);
    var cb = args[args.length - 1];
    args = args.slice(0, -1);
    return new Q(obj.apply(null, args)).nodeify(cb);
  };
};

module.exports = {
  simctl: nodeify(simctl),
  nodeify: nodeify
};

/* global Promise:true */
"use strict";

var _ = require('underscore')
  , Q = require('q')
  , simctl = require("node-simctl");

var nodeify = function (obj) {
  if (typeof obj !== "function") {
    var nodeified = {};
    _.each(Object.getOwnPropertyNames(obj), function (name) {
      if (name !== "__esModule") {
        nodeified[name] = nodeify(obj[name]);
      }
    });
    return nodeified;
  }
  return nodeifyFn(obj);
};

var nodeifyFn = function (fn, bindObj) {
  if (typeof bindObj === "undefined") {
    bindObj = null;
  }
  var newFn = function () {
    var args = Array.prototype.slice.call(arguments, 0);
    var cb = args[args.length - 1];
    args = args.slice(0, -1);
    return new Q(fn.apply(bindObj, args)).nodeify(cb);
  };
  return newFn;
};

// if we've imported es6 Promise type functions, ensure we can still use
// Q/B-like "nodeify" on them in our transitional code
if (typeof Promise === "function") {
  Promise.prototype.nodeify = function (cb) {
    this.then(function (res) {
      cb(null, res);
    }, function (err) {
      cb(err);
    });
  };
}

module.exports = {
  simctl: nodeify(simctl),
  nodeify: nodeify
};

update future.js to patch Promise with .nodeify so it can interoperate with Q/Bluebird