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

async function loadPlugins(config) {


  // Externes Plugins
  if (config.plugins.length) {
    config.plugins.map(registerPlugin => {
      registerPlugin({
        config: congig,
        services: config.service,
        
      })
    })
  }

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

  return new Promise(function (resolve, reject) {

    glob(Path, {}, function (er, files) {
      // register all endpoints
     
      files.map((file) => {
        let register = require("" + file);
        register({
          config: config,
          services: config.service
        });
        config.plugins.push(register); // Push all plugins
      });
      // 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} Plugins charged`);
      resolve();
    });
  });
}

module.exports = {
  loadPlugins,
};
