'use strict';

var Utils = {
  promisify: function (array, document) {
    var promises = [];

    array.forEach(function (promisedCallback) {
      promises.push(promisedCallback(document));
    });
    
    if (array.length <= 0) {
      promises.push(new Promise(function(resolve, reject) {
        reject = reject || null; // to avoid jshint unused code
        return resolve(document);
      }));
    }

    return promises;
  },

  setBeforeOrAfter: function (opType, opName, hooks, callback) {
    var promisedCallback = function (query) {
      return new Promise(function(resolve, reject) {
        reject = reject || null; // to avoid jshint unused code
        return resolve(callback(query));
      });
    };

    if (opType === 'before') {
      hooks.before[opName].push(promisedCallback);
    } else if (opType === 'after') {
      hooks.after[opName].push(promisedCallback);
    }

    return hooks;
  },

  setDatetime: function (opName, datetime, document, options) {
    if (datetime) {
      if (opName === 'update') {
        if (!document.$set && !document.$setOnInsert) {
          document.updatedAt = new Date();
          document.createdAt = new Date();
        } else {
          document.$set = document.$set || {};
          document.$set.updatedAt = new Date();
          
          document.$setOnInsert = document.$setOnInsert || {};
          document.$setOnInsert.createdAt = new Date();
        }
      } else if (opName === 'insert') {
        document.createdAt = new Date();
        document.updatedAt = new Date();
      }
    }
    return document;
  }
};

// export Utils helper
module.exports = Utils;
@@ -434,495 +434,8 @@
 %7D,%0A%0A
-  setBeforeOrAfter: function (opType, opName, hooks, callback) %7B%0A    var promisedCallback = function (query) %7B%0A      return new Promise(function(resolve, reject) %7B%0A        reject = reject %7C%7C null; // to avoid jshint unused code%0A        return resolve(callback(query));%0A      %7D);%0A    %7D;%0A%0A    if (opType === 'before') %7B%0A      hooks.before%5BopName%5D.push(promisedCallback);%0A    %7D else if (opType === 'after') %7B%0A      hooks.after%5BopName%5D.push(promisedCallback);%0A    %7D%0A%0A    return hooks;%0A  %7D,%0A%0A
   se

Remove useless function