$ = jQuery
context = window
logger = context.JK.logger
rest = context.JK.Rest()

@SessionNotificationStore = Reflux.createStore(
  {
    listenables: @NotificationActions

    notifications: []
    count: 0

    issueChange: () ->
      @trigger(@notifications)

    onClear: () ->
      @notifications = []
      @issueChange()

    onSessionEnded: () ->
      @notifications = []
      @issueChange()

    processNotification: (notification) ->
      notification.id = ++@count

      title = 'n/a'
      extra = null

      if notification.backend_detail?
        if notification.backend_detail == 'Network Issues'
          title = 'Network Issues'
          extra = notification.msg
        else
          title = notification.msg
          extra = notification.backend_detail
      else
        title = notification.msg

      detail = if notification.detail? && notification.detail != "" then notification.detail else null

      data =
        title: title
        extra: extra
        detail: detail
        help: notification.help

      @notifications.unshift(data)

      if @notifications.length > 100
        @notifications.pop();
      @issueChange()

    onBackendNotification: (notification) ->
      @processNotification(notification)

    onFrontendNotification: (notification) ->
      @processNotification(notification)
  }
)

