import React, { createContext, useContext, useMemo, useState } from 'react';
var StoresContext = /*#__PURE__*/createContext({});

var getPins = stores => {
  return stores.map(store => ({
    lat: +store.latitude,
    lng: +store.longitude
  }));
};

export var StoresContextProvider = (_ref) => {
  var {
    children,
    stores: allStores,
    type
  } = _ref;
  var [stores, setStores] = useState(allStores);
  var [selectedCity, setSelectedCity] = useState(null);
  var [selectedStore, setSelectedStore] = useState(null);
  var [selectedType, setSelectedType] = useState(null);
  var [pinsToFitInMap, setPinsToFitInMap] = useState(() => getPins(allStores));

  var filterByCity = city => {
    var filteredStores = allStores.filter(store => store.city === city);
    setStores(filteredStores);
    setSelectedType(null);
    setPinsToFitInMap(getPins(filteredStores));
    setSelectedCity(city);
  };

  var filterByStoreName = storeName => {
    var filteredStores = allStores.filter(store => store.name === storeName);
    setPinsToFitInMap(getPins(filteredStores));
    setSelectedStore(storeName);
  };

  var filterByStoreType = storeType => {
    var filteredStores = allStores.filter(store => selectedCity ? store.city === selectedCity && store.name.toLowerCase().includes(storeType.toLowerCase()) : store.name.toLowerCase().includes(storeType.toLowerCase()));
    setSelectedType(storeType || null);
    setStores(filteredStores);

    if (filteredStores.length) {
      setPinsToFitInMap(getPins(filteredStores));
    }
  };

  var reset = () => {
    setStores(allStores);
    setSelectedCity(null);
    setSelectedStore(null);
    setSelectedType(null);
    setPinsToFitInMap(getPins(allStores));
  };

  var cities = useMemo(() => {
    return [...new Set(allStores.map(store => store.city))].sort();
  }, [allStores]);
  var pins = useMemo(() => getPins(allStores), [allStores]);
  var providerValues = useMemo(() => {
    return {
      allStores,
      stores,
      cities,
      pins,
      reset,
      filterByCity,
      selectedCity,
      pinsToFitInMap,
      selectedStore,
      setSelectedStore,
      filterByStoreName,
      filterByStoreType,
      selectedType
    };
  }, [allStores, stores, cities, pins, selectedCity, pinsToFitInMap, selectedStore, selectedType]);
  return /*#__PURE__*/React.createElement(StoresContext.Provider, {
    value: providerValues
  }, children);
};
export var useStoresContext = () => {
  return useContext(StoresContext);
};
export var useStores = () => {
  return useContext(StoresContext).stores;
};
export var useStoresPins = () => {
  return useContext(StoresContext).pins;
};
export var useStoresPinsToFitInView = () => {
  return useContext(StoresContext).pinsToFitInMap;
};
//# sourceMappingURL=stores.context.js.map