import DataLoader from 'dataloader';

import {
  updateUser,
  addUser,
  getUser,
  getUsers,
  PATH,
  getUserByURL,
} from '../../Services/User';

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

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

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

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

  userLoader.loadAll = usersLoader.load.bind(usersLoader, '__all__');
  userLoader.loadByURL = userByURLLoader.load.bind(userByURLLoader);
  userLoader.loadManyByURL = userByURLLoader.loadMany.bind(userByURLLoader);
  userLoader.addUser = addUser;
  userLoader.updateUser = updateUser;
  return userLoader;
};

export default { getUserLoader };
