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

async function loadWebsocket(config) {
    var Path = cleanPath(process.cwd()  + "/extensions/websockets/**/index.js")
   
    return new Promise(function (resolve, reject) {

        glob(Path, {}, function async(er, files) {
          // register all endpoints
         // consola.success(`${files.length} Endpoints charged`);

          files.map(async (file) => {
            let register = require("" + file);
            if(register){
              await register({
                config:{...config},
                api: config.api,
                services:config.service,
                state:{...config.state},
                socket:config.state?.socket,
                io:config.state?.io,
                
              });
            }
          });
          // 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 = {
  loadWebsocket,
};
