// Generated by CoffeeScript 1.12.5
var get,
  indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

import Ember from 'ember';

get = Ember.get;

export default Ember.Component.extend({
  tagName: 'span',
  organizations: [],
  relevant_organizations: Ember.computed('organizations.[]', 'globals.current_user.admin', function() {
    var organizations, user;
    if (!(user = this.get('globals.current_user'))) {
      return [];
    }
    organizations = this.get('organizations');
    if (get(user, 'admin')) {
      return organizations;
    }
    return organizations.filter(function(o) {
      var ref, user_ids;
      user_ids = get(o, 'users').mapBy('id');
      return ref = get(user, 'id'), indexOf.call(user_ids, ref) >= 0;
    });
  }),
  actions: {
    apply_filters: function() {
      var base;
      return typeof (base = this.get('apply_filters')) === "function" ? base() : void 0;
    },
    set_scope_organization: function(organization) {
      var organization_id;
      if (organization != null) {
        organization_id = (organization != null ? organization.get('id') : void 0) || '';
        this.set('scope_organization', organization);
        this.set('filters.workflow_action_definition_organization_id', organization_id);
        this.$('.select-organization .dropdown.button').addClass('orange');
        return this.send('apply_filters');
      } else {
        this.setProperties({
          scope_organization: null,
          scope_scenario: null,
          'filters.workflow_action_definition_organization_id': ''
        }, this.$('.select-organization .dropdown.button').removeClass('orange'));
        return this.send('apply_filters');
      }
    }
  },
  didRender: function() {
    var organization, organization_id;
    if (organization_id = this.get('filters.workflow_action_definition_organization_id')) {
      if (organization = this.get('relevant_organizations').findBy('id', organization_id)) {
        this.set('scope_organization', organization);
        return this.$('.select-organization .dropdown.button').addClass('orange');
      }
    }
  }
});
