import DataLoader from 'dataloader';

import {
  updateEntity,
  addEntity,
  getEntity,
  getEntities,
  PATH,
  getEntityByURL,
} from '../../Services/Score';

const getScoreLoader = (cacheMap) => {
  const scoresLoader =
      new DataLoader(keys => Promise.all(keys.map(getEntities)), { cache: false }); // , { cacheMap }

  const scoreLoader =
    new DataLoader(keys => Promise.all(keys.map(getEntity)), { cache: false });

    // , {
    //   cacheKeyFn: key => `${PATH}/${key}`,
    //   cacheMap,
    // }

  const scoreByURLLoader =
      new DataLoader(keys => Promise.all(keys.map(getEntityByURL)), { cache: false }); // , { cacheMap }

  scoreLoader.loadAll = scoresLoader.load.bind(scoresLoader, '__all__');
  scoreLoader.loadByURL = scoreByURLLoader.load.bind(scoreByURLLoader);
  scoreLoader.loadManyByURL = scoreByURLLoader.loadMany.bind(scoreByURLLoader);
  scoreLoader.addScore = addEntity;
  scoreLoader.updateScore = updateEntity;

  return scoreLoader;
};

export default { getScoreLoader };
