
export const FETCH_INVALID = 'FETCH_INVALID';
export const FETCH_ONGOING = 'FETCH_ONGOING';
export const FETCH_SUCCEEDED = 'FETCH_SUCCEEDED';
export const FETCH_FAILED = 'FETCH_FAILED';

const missingFetchStatusError = {
  msg: 'fetchStatuses is undefined!',
};

function combineArrayOfFetchStatuses(fetchStatusesArray) {
  if (fetchStatusesArray === undefined) {
    throw missingFetchStatusError;
  }
  const allInvalid = fetchStatusesArray.reduce((total, status) => total && (status === FETCH_INVALID), true);
  const anyOngoing = fetchStatusesArray.reduce((total, status) => total || (status === FETCH_ONGOING), false);
  const anyFailed = fetchStatusesArray.reduce((total, status) => total || (status === FETCH_FAILED), false);
  const allSucceeded = fetchStatusesArray.reduce((total, status) => total && (status === FETCH_SUCCEEDED), true);
  if (allInvalid) {
    return FETCH_INVALID;
  }
  if (anyOngoing) {
    return FETCH_ONGOING;
  }
  if (anyFailed) {
    return FETCH_FAILED;
  }
  if (allSucceeded) {
    return FETCH_SUCCEEDED;
  }
  return FETCH_ONGOING;
}

function combineIndexedFetchStatuses(indexedFetchStatuses) {
  if (indexedFetchStatuses === undefined) {
    throw missingFetchStatusError;
  }
  const fetchStatusesArray = Object.keys(indexedFetchStatuses).map(k => indexedFetchStatuses[k]);
  return combineArrayOfFetchStatuses(fetchStatusesArray);
}

// accept a string, an array, or an object
export function combineFetchStatuses(input) {
  let fetchStatusToUse;
  if (input === undefined) {
    throw missingFetchStatusError;
  // support a basic string
  } else if (typeof input === 'string') {
    fetchStatusToUse = input;
  // or an array of strings
  } else if (Array.isArray(input)) {
    fetchStatusToUse = combineArrayOfFetchStatuses(input);
  // or an object keyed to strings
  } else if (typeof input === 'object' && input !== null) {
    fetchStatusToUse = combineIndexedFetchStatuses(input);
  } else {
    const badFetchStatusType = {
      msg: `fetchStatus received is not a valid type - it is ${typeof input}`,
    };
    throw badFetchStatusType;
  }
  return fetchStatusToUse;
}

@@ -185,223 +185,66 @@
 ';%0A%0A
-const missingFetchStatusError = %7B%0A  msg: 'fetchStatuses is undefined!',%0A%7D;%0A%0Afunction combineArrayOfFetchStatuses(fetchStatusesArray) %7B%0A  if (fetchStatusesArray === undefined) %7B%0A    throw missingFetchStatusError;%0A  %7D
+function combineArrayOfFetchStatuses(fetchStatusesArray) %7B
 %0A  c
@@ -978,91 +978,8 @@
 ) %7B%0A
-  if (indexedFetchStatuses === undefined) %7B%0A    throw missingFetchStatusError;%0A  %7D%0A
   co
@@ -1271,24 +1271,111 @@
 ndefined) %7B%0A
+    const missingFetchStatusError = %7B%0A      msg: 'fetchStatuses is undefined!',%0A    %7D;%0A
     throw mi
@@ -1814,16 +1814,21 @@
 atusType
+Error
  = %7B%0A   
@@ -1938,16 +1938,21 @@
 atusType
+Error
 ;%0A  %7D%0A  

move redundant error checking