


var _ = require("lodash");
const { cleanPath } = require("../utilities/utils");
async function loadHooks(config) {
  var Path = cleanPath(process.cwd() + "/extensions/api/**/hooks.*.js")
  return new Promise(function (resolve, reject) {
    glob(Path, {}, function (er, files) {
      // register all endpoints
      var hooks = {}    
      files.map((file) => {
        let hook = require("" + file);

        try{
          //console.log(hook)
        } catch(err){}
       
        if (hook.model) {
          hooks[hook.model] = hook;
        } else {
          let hookMissing = file.split('/')
          hooks[hookMissing[hookMissing.length - 2]] =hook;
        }
       // register({
       //   router: config.app,
       //   api: config.api,
      //  });
      });
      config.hooks = {
        ...config.hooks,
        ...hooks,
      }

     


      

    

       // consola.success(`${Object.keys(hooks).length} Hooks charged`);

      // 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.
      resolve();
    });
  });
}


module.exports = {
  loadHooks,
};
