'use strict'

export default (customEmitter) => {
  const Emitter = customEmitter;
  const emit = Emitter.prototype.emit;
  
  const onevent = function (packet) {
    var args = packet.data || [];
    if (packet.id != null) { // eslint-disable-line no-eq-null, eqeqeq
      args.push(this.ack(packet.id));
    }    
    emit.call(this, '*', packet);
    return emit.apply(this, args);
  }
  
  return (socket, next?) => {
    let boundOnevent = onevent;
    const namespaces = (process as any).namespaces || {};
  
    for (const key of Object.keys(namespaces)) {
      const ns = namespaces[key];
      boundOnevent = ns.bind(boundOnevent);
    }

    socket.onevent = boundOnevent;

    return next ? next() : null;
  }
}
