import DataLoader from 'dataloader';

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

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

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

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

  const activityByURLLoader =
    new DataLoader(keys =>
      Promise.all(keys.map(key => getEntityByURL(`${PATH}/${key}`))), { cache: false }); // , { cacheMap }

  activityLoader.loadAll = activitiesLoader.load.bind(activitiesLoader, '__all__');
  activityLoader.loadByURL = activityByURLLoader.load.bind(activityByURLLoader);
  activityLoader.loadManyByURL = activityByURLLoader.loadMany.bind(activityByURLLoader);
  activityLoader.addActivity = addEntity;
  activityLoader.updateActivity = updateEntity;

  return activityLoader;
};

export default { getActivityLoader };
