const getGroups = (superset, minSize, maxSize) => {
  const result = [];
  let size = minSize;
  const max = maxSize || superset.length;
  while (size <= max) {
    let done = false;
    let currentCombo = null;
    let distanceBack = null;
    let newLastIndex = null;
    const indexes = [];
    const indexesLast = size - 1;
    const supersetLast = superset.length - 1;

    // initialize indexes to start with leftmost combo
    for (let i = 0; i < size; i += 1) {
      indexes[i] = i;
    }
    while (!done) {
      currentCombo = [];
      for (let i = 0; i < size; i += 1) {
        currentCombo.push(superset[indexes[i]]);
      }
      result.push(currentCombo);
      if (indexes[indexesLast] === supersetLast) {
        done = true;
        for (let i = indexesLast - 1; i > -1; i -= 1) {
          distanceBack = indexesLast - i;
          newLastIndex = indexes[indexesLast - distanceBack] + distanceBack + 1;
          if (newLastIndex <= supersetLast) {
            indexes[indexesLast] = newLastIndex;
            done = false;
            break;
          }
        }
        if (!done) {
          indexes[indexesLast - distanceBack] += 1;
          distanceBack -= 1;
          for (; distanceBack; distanceBack -= 1) {
            indexes[indexesLast - distanceBack] = indexes[indexesLast - distanceBack - 1] + 1;
          }
        }
      } else {
        indexes[indexesLast] += 1;
      }
    }
    size += 1;
  }
  return result;
};
const getPathCounts = function getPathCounts(opts) {
  const {
    unsetElectorCounts,
    goal,
    assigned
  } = opts;
  const groups = getGroups(unsetElectorCounts, 1);
  const pathCounts = {
    dem: 0,
    gop: 0
  };
  groups.forEach(group => {
    const sum = group.reduce((s, x) => s + x, 0);
    if (sum + assigned.dem >= goal) {
      pathCounts.dem += 1;
    }
    if (sum + assigned.gop >= goal) {
      pathCounts.gop += 1;
    }
  });
  return pathCounts;
};
export default getPathCounts;