 request = require("request-promise")
 Promise = require("bluebird")
 _ = require("lodash")
+EventEmitter = require("events").EventEmitter
+notificationsApi = null
 
 _getHeaders = (headers) ->
   _(headers)


  request options
  .promise()
  .then ({ statusCode }) -> notificationsApi.success message.JobId, statusCode
  .catch ({ statusCode, error }) ->
    throw { statusCode, error } unless dequeueCount >= MAX_DEQUEUE_COUNT
    notificationsApi.fail message.JobId, statusCode, error

    headers: _getHeaders HeadersForRequest
  }, _.isUndefined

emitter = new EventEmitter

emitter.on "success", ({ message: { JobId }, statusCode }) -> notificationsApi.success JobId, statusCode
emitter.on "failed", ({ message: { JobId }, statusCode, error }) -> notificationsApi.fail JobId, statusCode, error

module.exports = (generateOptions) -> ({ message, meta: { dequeueCount }}) ->
  messageOptions = _cleanOptions message
  notificationsApi = new NotificationsApi messageOptions.headers["Authorization"]
