<commit_before>import load from 'little-loader';
import isMobile from '../../utils/detectMobile';

const defaultIcon = 'http://maps.google.com/mapfiles/ms/icons/red-dot.png';

export const createMarkers = (
  addMarkerToStore,
  geocoder,
  map,
  markerClickCallback,
  stores
) => {
  // Note: This has to be done because google maps api has a request limit
  const maxReqPerSec = 1;
  //todo: remove this comments
  const loops = 4; //Math.ceil(stores.length / maxReqPerSec); //e.g. 233/50 = 5
  let storesChunk = [];

  for (var i = 0; i < loops; i++) {
    /* e.g. n = 233 loops = 5
    storesChunk 1 = stores.slice(0, 50)
    storesChunk 2 = stores.slice(50, 100)
    ...
    */
    if (i === loops.length - 1) {
      storesChunk = stores.slice(i * maxReqPerSec);
    } else {
      storesChunk = stores.slice(i * maxReqPerSec, (i + 1) * maxReqPerSec);
    }
    // create markers every sec
    setTimeout(
      ((storesChunk, i) => {
        return () => {
          createMarkersChunk(
            addMarkerToStore,
            geocoder,
            map,
            markerClickCallback,
            storesChunk,
            i * maxReqPerSec
          );
        };
      })(storesChunk, i),
      1000 * i
    );
  }
};

function createMarkersChunk(
  addMarkerToStore,
  geocoder,
  map,
  markerClickCallback,
  storesChunk,
  chunkInitialIndex
) {
  storesChunk.forEach(async (storeObj, i) => {
    const newMarker = await createMarker(storeObj, map, geocoder);
    if (newMarker) {
      newMarker.addListener('click', () =>
        markerClickCallback(chunkInitialIndex + i)
      );
      addMarkerToStore(newMarker, chunkInitialIndex + i);
    }
  });
}

function createMarker(storeObj, map, geocoder, clickCallback) {
  return getLocationByName(geocoder, storeObj.Address).then(
    position => {
      return new google.maps.Marker({
        icon: defaultIcon,
        map,
        position,
        animation: google.maps.Animation.DROP
      });
    },
    error => {
      console.warn(
        `Marker could not be created, name: ${storeObj.Name}, address: ${storeObj.Address}, error: ${error}`
      );
      return false;
    }
  );
}

export const loadMapApi = () => {
  const GOOGLE_MAPS_API_KEY = '<KEY>';
  const googleMapsUrl = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}`;<commit_msg>Removed commented code. Removed marker transition<commit_after>import load from 'little-loader';
import isMobile from '../../utils/detectMobile';

const defaultIcon = 'http://maps.google.com/mapfiles/ms/icons/red-dot.png';

export const createMarkers = (
  addMarkerToStore,
  geocoder,
  map,
  markerClickCallback,
  stores
) => {
  // Note: This has to be done this way because google maps api has a request limit
  const maxReqPerSec = 1;
  const delay = 900;
  const loops = Math.ceil(stores.length / maxReqPerSec); //e.g. 233/50 = 5
  let storesChunk = [];

  for (var i = 0; i < loops; i++) {
    /* e.g. n = 233 loops = 5
    storesChunk 1 = stores.slice(0, 50)
    storesChunk 2 = stores.slice(50, 100)
    ...
    */
    if (i === loops.length - 1) {
      storesChunk = stores.slice(i * maxReqPerSec);
    } else {
      storesChunk = stores.slice(i * maxReqPerSec, (i + 1) * maxReqPerSec);
    }
    // create markers every sec
    setTimeout(
      ((storesChunk, i) => {
        return () => {
          createMarkersChunk(
            addMarkerToStore,
            geocoder,
            map,
            markerClickCallback,
            storesChunk,
            i * maxReqPerSec
          );
        };
      })(storesChunk, i),
      delay * i
    );
  }
};

function createMarkersChunk(
  addMarkerToStore,
  geocoder,
  map,
  markerClickCallback,
  storesChunk,
  chunkInitialIndex
) {
  storesChunk.forEach(async (storeObj, i) => {
    const newMarker = await createMarker(storeObj, map, geocoder);
    if (newMarker) {
      newMarker.addListener('click', () =>
        markerClickCallback(chunkInitialIndex + i)
      );
      addMarkerToStore(newMarker, chunkInitialIndex + i);
    }
  });
}

function createMarker(storeObj, map, geocoder, clickCallback) {
  return getLocationByName(geocoder, storeObj.Address).then(
    position => {
      return new google.maps.Marker({
        icon: defaultIcon,
        map,
        position
      });
    },
    error => {
      console.warn(
        `Marker could not be created, name: ${storeObj.Name}, address: ${storeObj.Address}, error: ${error}`
      );
      return false;
    }
  );
}

export const loadMapApi = () => {
  const GOOGLE_MAPS_API_KEY = '<KEY>';
  const googleMapsUrl = `https://maps.googleapis.com/maps/api/js?key=${GOOGLE_MAPS_API_KEY}`;
{'fraction_non_alphanumeric': 0.12993138936535162, 'fraction_numerical': 0.012006861063464836, 'mean_word_length': 2.4787472035794185, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2200158', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1493, 'n_words': 344}