import { shallowRef, watchEffect } from 'vue';

/**
 * Return cached Key Value map with DataNode.
 * Only re-calculate when `flattenOptions` changed.
 */
export default function useKeyValueMap(flattenOptions) {
  var cacheKeyMap = shallowRef(new Map());
  var cacheValueMap = shallowRef(new Map());
  watchEffect(function () {
    var newCacheKeyMap = new Map();
    var newCacheValueMap = new Map(); // Cache options by key

    flattenOptions.value.forEach(function (dataNode) {
      newCacheKeyMap.set(dataNode.key, dataNode);
      newCacheValueMap.set(dataNode.data.value, dataNode);
    });
    cacheKeyMap.value = newCacheKeyMap;
    cacheValueMap.value = newCacheValueMap;
  });
  return [cacheKeyMap, cacheValueMap];
}