// 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({
  get_active_workflow_groups: function(workflow_groups, project_scenarios, set_operation) {
    if (set_operation == null) {
      set_operation = 'union';
    }
    project_scenarios = Ember.makeArray(project_scenarios);
    workflow_groups = workflow_groups.toArray();
    if (project_scenarios.length) {
      if (set_operation === 'intersection') {
        return this.get_active_workflow_groups_using_intersection(workflow_groups, project_scenarios);
      } else {
        return this.get_active_workflow_groups_using_union(project_scenarios);
      }
    } else {
      return workflow_groups;
    }
  },
  get_active_workflow_groups_using_union: function(project_scenarios) {
    var selected_project_scenario_workflow_group_ids, working_workflow_groups;
    selected_project_scenario_workflow_group_ids = [];
    working_workflow_groups = [];
    project_scenarios.forEach(function(selected_project_scenario) {
      return selected_project_scenario.get('workflow_groups').forEach(function(workflow_group) {
        if (selected_project_scenario_workflow_group_ids.indexOf(workflow_group.get('id')) < 0) {
          selected_project_scenario_workflow_group_ids.push(workflow_group.get('id'));
          return working_workflow_groups.push(workflow_group);
        }
      });
    });
    return working_workflow_groups;
  },
  get_active_workflow_groups_using_intersection: function(workflow_groups, project_scenarios) {
    var first_run, selected_project_scenario_workflow_group_ids;
    selected_project_scenario_workflow_group_ids = [];
    first_run = true;
    project_scenarios.forEach(function(selected_project_scenario) {
      var current_workflow_group_ids;
      current_workflow_group_ids = selected_project_scenario.get('workflow_groups').mapBy('id').toArray();
      if (first_run) {
        selected_project_scenario_workflow_group_ids = current_workflow_group_ids;
        first_run = false;
        return;
      }
      return selected_project_scenario_workflow_group_ids = selected_project_scenario_workflow_group_ids.filter(function(id) {
        return indexOf.call(current_workflow_group_ids, id) >= 0;
      });
    });
    return workflow_groups.filter(function(r) {
      var ref;
      return ref = r.get('id'), indexOf.call(selected_project_scenario_workflow_group_ids, ref) >= 0;
    });
  }
});
