// Generated by CoffeeScript 1.12.5
var get_active_workflow_action_definitions, get_active_workflow_action_definitions_using_intersection, get_active_workflow_action_definitions_using_union,
  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_active_workflow_action_definitions_using_union = function(project_scenarios) {
  var selected_project_scenario_workflow_action_definition_ids, working_workflow_action_definitions;
  selected_project_scenario_workflow_action_definition_ids = [];
  working_workflow_action_definitions = [];
  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);
      }
    });
  });
  return working_workflow_action_definitions;
};

get_active_workflow_action_definitions_using_intersection = function(workflow_action_definitions, project_scenarios) {
  var first_run, selected_project_scenario_workflow_action_definition_ids;
  selected_project_scenario_workflow_action_definition_ids = [];
  first_run = true;
  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;
    });
  });
  return workflow_action_definitions.filter(function(r) {
    var ref;
    return ref = r.get('id'), indexOf.call(selected_project_scenario_workflow_action_definition_ids, ref) >= 0;
  });
};

get_active_workflow_action_definitions = function(workflow_action_definitions, project_scenarios, set_operation) {
  if (set_operation == null) {
    set_operation = 'union';
  }
  project_scenarios = Ember.makeArray(project_scenarios);
  workflow_action_definitions = workflow_action_definitions.toArray();
  if (project_scenarios.length) {
    if (set_operation === 'intersection') {
      return get_active_workflow_action_definitions_using_intersection(workflow_action_definitions, project_scenarios);
    } else {
      return get_active_workflow_action_definitions_using_union(project_scenarios);
    }
  } else {
    return workflow_action_definitions;
  }
};

export {
  get_active_workflow_action_definitions_using_union
};

export {
  get_active_workflow_action_definitions_using_intersection
};

export {
  get_active_workflow_action_definitions
};

export default get_active_workflow_action_definitions;
