// @flow weak;
// Based on react-motion's mergeDiff (https://github.com/chenglou/react-motion)

function mergeKeys(currNodeKeys, currKeyIndex, nextNodeKeys, nextKeyIndex) {
  const allKeys = [];

  for (let i = 0; i < nextNodeKeys.length; i++) {
    allKeys[i] = nextNodeKeys[i];
  }

  for (let i = 0; i < currNodeKeys.length; i++) {
    if (nextKeyIndex[currNodeKeys[i]] === undefined) {
      allKeys.push(currNodeKeys[i]);
    }
  }

  return allKeys.sort((a, b) => {
    const nextOrderA = nextKeyIndex[a];
    const nextOrderB = nextKeyIndex[b];
    const currOrderA = currKeyIndex[a];
    const currOrderB = currKeyIndex[b];

    if (nextOrderA != null && nextOrderB != null) {
      return nextKeyIndex[a] - nextKeyIndex[b];
    } else if (currOrderA != null && currOrderB != null) {
      return currKeyIndex[a] - currKeyIndex[b];
    } else if (nextOrderA != null) {
      for (let i = 0; i < nextNodeKeys.length; i++) {
        const pivot = nextNodeKeys[i];

        if (!currKeyIndex[pivot]) {
          continue; // eslint-disable-line no-continue
        }
        if (
          nextOrderA < nextKeyIndex[pivot] &&
          currOrderB > currKeyIndex[pivot]
        ) {
          return -1;
        } else if (
          nextOrderA > nextKeyIndex[pivot] &&
          currOrderB < currKeyIndex[pivot]
        ) {
          return 1;
        }
      }

      return 1;
    }

    for (let i = 0; i < nextNodeKeys.length; i++) {
      const pivot = nextNodeKeys[i];

      if (!currKeyIndex[pivot]) {
        continue; // eslint-disable-line no-continue
      }

      if (
        nextOrderB < nextKeyIndex[pivot] &&
        currOrderA > currKeyIndex[pivot]
      ) {
        return 1;
      } else if (
        nextOrderB > nextKeyIndex[pivot] &&
        currOrderA < currKeyIndex[pivot]
      ) {
        return -1;
      }
    }

    return -1;
  });
}

export default mergeKeys;


@@ -108,16 +108,19 @@
 rgeKeys(
+%0A  
 currNode
@@ -123,17 +123,33 @@
 NodeKeys
-,
+:Array%3Cstring%3E,%0A 
  currKey
@@ -153,17 +153,22 @@
 KeyIndex
-,
+:%7B%7D,%0A 
  nextNod
@@ -172,17 +172,33 @@
 NodeKeys
-,
+:Array%3Cstring%3E,%0A 
  nextKey
@@ -202,16 +202,21 @@
 KeyIndex
+:%7B%7D,%0A
 ) %7B%0A  co

add flow types