import Ember from 'ember'
{get} = Ember

export default Ember.Component.extend
  tagName: 'span'
  organizations: []
  relevant_organizations: Ember.computed 'organizations.[]', 'globals.current_user.admin', ->
    return [] unless user = @get('globals.current_user')
    organizations = @get('organizations')
    return organizations if get(user, 'admin')
    organizations.filter (o) ->
      user_ids = get(o, 'users').mapBy('id')
      get(user, 'id') in user_ids
  actions:
    apply_filters: ->
      @get('apply_filters')?()
    set_scope_organization: (organization) ->
      if organization?
        #organization.reload().then =>
        organization_id = organization?.get('id') || ''
        @set 'scope_organization', organization
        @set 'filters.workflow_action_definition_organization_id', organization_id
        @$('.select-organization .dropdown.button').addClass('orange')
        @send 'apply_filters'
      else
        @setProperties
          scope_organization: null
          scope_scenario: null
          'filters.workflow_action_definition_organization_id': ''
          @$('.select-organization .dropdown.button').removeClass('orange')
        @send 'apply_filters'
  didRender: ->
    if organization_id = @get('filters.workflow_action_definition_organization_id')
      if organization = @get('relevant_organizations').findBy('id', organization_id)
        @set 'scope_organization', organization
        @$('.select-organization .dropdown.button').addClass('orange')

