import DataLoader from 'dataloader';
import {
  getCloseUsersLocations,
  getDistanceLocation,
  getLocation,
  updateGeolocation,
} from '../../Services/Geolocation';

// loader
const getGeolocationsLoader = (cacheMap) => {
  const geolocationsLoader =
      new DataLoader(params => Promise.all(params.map(getLocation)), { cache: false });

    // , {
    //   cacheKeyFn: params => `/${params.id}/${params.key}`,
    //   cacheMap,
    // }

  const geolocationsCloseUsersLoader =
    new DataLoader(params => Promise.all(params.map(getCloseUsersLocations)), { cache: false });

    // , {
    //   cacheKeyFn: params => `/${params.id}/${params.key}/${params.distance}/${params.unit}`,
    //   cacheMap,
    // }

  const geolocationsDistanceLoader =
    new DataLoader(params => Promise.all(params.map(getDistanceLocation)), { cache: false });

    // , {
    //   cacheKeyFn: params => `/${params.id}/${params.key}/${params.foreignKey}`,
    //   cacheMap,
    // }

  geolocationsLoader.load = geolocationsLoader.load.bind(geolocationsLoader);
  geolocationsLoader.loadCloseUsers = geolocationsCloseUsersLoader.load.bind(geolocationsCloseUsersLoader);
  geolocationsLoader.loadDistanceFromUser = geolocationsDistanceLoader.load.bind(geolocationsDistanceLoader);
  geolocationsLoader.updateGeolocation = updateGeolocation;

  return geolocationsLoader;
};

export default { getGeolocationsLoader };
