// Generated by CoffeeScript 1.12.5
var 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';

export default Ember.Mixin.create({
  selected_project_scenarios: [],
  selected_project_scenarios_set_operation: 'union',
  active_workflow_action_definitions: [],
  active_workflow_action_definition_ids: [],
  set_active_workflow_action_definitions: function() {
    if (this.get('selected_project_scenarios.length')) {
      if (this.get('selected_project_scenarios_set_operation') === 'intersection') {
        return this.set_active_workflow_action_definitions_using_intersection();
      } else {
        return this.set_active_workflow_action_definitions_using_union();
      }
    } else {
      this.set('active_workflow_action_definitions', this.get('project.workflow_action_definitions') || []);
      return this.set('active_workflow_action_definition_ids', this.get('active_workflow_action_definitions').mapBy('id').toArray());
    }
  },
  set_active_workflow_action_definitions_using_union: function() {
    var selected_project_scenario_workflow_action_definition_ids, working_workflow_action_definitions;
    selected_project_scenario_workflow_action_definition_ids = [];
    working_workflow_action_definitions = [];
    this.get('selected_project_scenarios').forEach(function(selected_project_scenario) {
      return selected_project_scenario.get('workflow_action_definitions').forEach(function(workflow_action_definition) {
        if (selected_project_scenario_workflow_action_definition_ids.indexOf(workflow_action_definition.get('id')) < 0) {
          selected_project_scenario_workflow_action_definition_ids.push(workflow_action_definition.get('id'));
          return working_workflow_action_definitions.push(workflow_action_definition);
        }
      });
    });
    this.set('active_workflow_action_definitions', working_workflow_action_definitions);
    return this.set('active_workflow_action_definition_ids', selected_project_scenario_workflow_action_definition_ids);
  },
  set_active_workflow_action_definitions_using_intersection: function() {
    var first_run, selected_project_scenario_workflow_action_definition_ids;
    selected_project_scenario_workflow_action_definition_ids = [];
    first_run = true;
    this.get('selected_project_scenarios').forEach(function(selected_project_scenario) {
      var current_workflow_action_definition_ids;
      current_workflow_action_definition_ids = selected_project_scenario.get('workflow_action_definitions').mapBy('id').toArray();
      if (first_run) {
        selected_project_scenario_workflow_action_definition_ids = current_workflow_action_definition_ids;
        first_run = false;
        return;
      }
      return selected_project_scenario_workflow_action_definition_ids = selected_project_scenario_workflow_action_definition_ids.filter(function(id) {
        return indexOf.call(current_workflow_action_definition_ids, id) >= 0;
      });
    });
    this.set('active_workflow_action_definition_ids', selected_project_scenario_workflow_action_definition_ids);
    return this.set('active_workflow_action_definitions', this.get('project.workflow_action_definitions').filter(function(r) {
      var ref;
      return ref = r.get('id'), indexOf.call(selected_project_scenario_workflow_action_definition_ids, ref) >= 0;
    }));
  }
});
