import isEqual from '../vc-util/isEqual';
/**
 * Singleton cache will only take latest `cacheParams` as key
 * and return the result for callback matching.
 */
export default function useSingletonCache() {
  let cacheRef = [null, null];
  const getCache = (cacheKeys, callback) => {
    const filteredKeys = cacheKeys.map(item => item instanceof HTMLElement || Number.isNaN(item) ? '' : item);
    if (!isEqual(cacheRef[0], filteredKeys)) {
      cacheRef = [filteredKeys, callback()];
    }
    return cacheRef[1];
  };
  return getCache;
}