const { cleanPath } = require("../utilities/utils");

async function  loadAllMiddlewareFile(config){


  var Path = cleanPath(process.cwd() + "/extensions/middlewares/**/index.js");

  return new Promise(function (resolve, reject) {

    glob(Path, {}, function (er, files) {
      // register all endpoints
      files.map((file) => {
        let splitPath = file.split('/')
        var folder = splitPath[splitPath.length-2]
        
        let register = require("" + file);
       // console.log("FIchier ",register)
        config.middlewares[register.name] = {
          handler:register?.handler
        }

       
      });

      //console.log(config.middlewares)
      // files is an array of filenames.
      // If the `nonull` option is set, and nothing
      // was found, then files is ["**/*.js"]
      // er is an error object or null.
       //consola.success(`${config.plugins.length}  charged`);
      resolve();
    });
  });

}



module.exports={
   loadAllMiddlewareFile
}