import Component from '@ember/component'
import { filter } from '@ember/object/computed'
import { computed, observer } from '@ember/object'
import { inject as service } from '@ember/service'

import layout from 'leaf-semantic-core/templates/components/leaf-toasts'


ToastsComponent = Component.extend(
  layout: layout

  toasts: service('leaf-toasts')

  tagName: 'aside'
  classNames: 'toasts'
  classNameBindings: ['location']


  messages: filter('toasts.visibleToasts', (toast) ->
    # If this instance of the notifications component has no location affinity
    # then it gets all notifications

    if !@get('location')
      return true

    displayLocation =
      if (typeof(toast.toJSON) == 'function')
        toast.get('location')
      else
        toast.location
    @get('location') == displayLocation
  )

  messageCountObserver: observer('messages.[]', ->
    @sendAction('notify', @get('messages').length)
  )
)

export default ToastsComponent
