/*!
 * Moajs MiddleWares
 * Copyright(c) 2015-2019 Alfred Sang <shiren1118@126.com>
 * MIT Licensed
 */
var debug = require('debug')('moa-middlewares');

function Middlewares(app, dir) {
  this.app = app;
  this.dir = dir;

  debug('moa-middlewares mount dir %s', this.dir);
  this.toObject();
  
  return this;
};

Middlewares.prototype.dir = function(dir) {
  if (!arguments[0]) {
    this.dir = "app/middlewares";
  } else {
    this.dir = dir;
  }
  
  this.toObject();
  
  return this;
}

Middlewares.prototype.mount = function(middlewares) {
  console.log(typeof middlewares);
  
  if(typeof middlewares == 'array'){
    debug('array')
    
    middlewares.forEach(function(i){
      console.log('mount m = ' + i);
      var m = middlewares[i];
      
      this.mount_one(m);
    });
  }else if(typeof middlewares == 'function'){
    debug('function')
    var m = middlewares;
    
    this.mount_one(m);
  }else{
    console.error('cant mount this middlewares!');
  }
}

Middlewares.prototype.mount_one = function(middleware) {
  this.app.use(middleware);
};


Middlewares.prototype.toObject = function() {
  var requireDirectory = require('require-directory');
  var middlewares = requireDirectory(module, this.dir);
  
  _extend(this, middlewares);
}

// ------ private 
function _extend(des, src) { 
  if (!des) { 
    des = {}; 
  } 
  if (src) { 
    for (var i in src) { 
      des[i] = src[i]; 
    } 
  } 
	
  return des; 
} 

module.exports = function (app, dir) {
  var _dir = "app/middlewares";
  
  if (arguments[1]) {
    _dir = dir;
  }
  
  return new Middlewares(app, _dir);
};
@@ -543,16 +543,19 @@
 res) %7B%0A 
+ //
  console
@@ -1065,17 +1065,16 @@
 e);%0A%7D;%0A%0A
-%0A
 Middlewa
@@ -1225,16 +1225,50 @@
 ir);%0A  %0A
+  this.middlewares = middlewares;%0A
   _exten
@@ -1309,17 +1309,16 @@
  private
- 
 %0Afunctio
@@ -1472,16 +1472,35 @@
 s; %0A%7D %0A%0A
+// ------- public %0A
 module.e

bind this.this.middlewares = middlewares