"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

exports.__esModule = true;
exports.useStoresPinsToFitInView = exports.useStoresPins = exports.useStores = exports.useStoresContext = exports.StoresContextProvider = void 0;

var _react = _interopRequireWildcard(require("react"));

var StoresContext = /*#__PURE__*/(0, _react.createContext)({});

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

var StoresContextProvider = function StoresContextProvider(_ref) {
  var children = _ref.children,
      allStores = _ref.stores,
      type = _ref.type;

  var _useState = (0, _react.useState)(allStores),
      stores = _useState[0],
      setStores = _useState[1];

  var _useState2 = (0, _react.useState)(null),
      selectedCity = _useState2[0],
      setSelectedCity = _useState2[1];

  var _useState3 = (0, _react.useState)(null),
      selectedStore = _useState3[0],
      setSelectedStore = _useState3[1];

  var _useState4 = (0, _react.useState)(null),
      selectedType = _useState4[0],
      setSelectedType = _useState4[1];

  var _useState5 = (0, _react.useState)(function () {
    return getPins(allStores);
  }),
      pinsToFitInMap = _useState5[0],
      setPinsToFitInMap = _useState5[1];

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

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

  var filterByStoreType = function filterByStoreType(storeType) {
    var filteredStores = allStores.filter(function (store) {
      return 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 = function reset() {
    setStores(allStores);
    setSelectedCity(null);
    setSelectedStore(null);
    setSelectedType(null);
    setPinsToFitInMap(getPins(allStores));
  };

  var cities = (0, _react.useMemo)(function () {
    return [].concat(new Set(allStores.map(function (store) {
      return store.city;
    }))).sort();
  }, [allStores]);
  var pins = (0, _react.useMemo)(function () {
    return getPins(allStores);
  }, [allStores]);
  var providerValues = (0, _react.useMemo)(function () {
    return {
      allStores: allStores,
      stores: stores,
      cities: cities,
      pins: pins,
      reset: reset,
      filterByCity: filterByCity,
      selectedCity: selectedCity,
      pinsToFitInMap: pinsToFitInMap,
      selectedStore: selectedStore,
      setSelectedStore: setSelectedStore,
      filterByStoreName: filterByStoreName,
      filterByStoreType: filterByStoreType,
      selectedType: selectedType
    };
  }, [allStores, stores, cities, pins, selectedCity, pinsToFitInMap, selectedStore, selectedType]);
  return /*#__PURE__*/_react["default"].createElement(StoresContext.Provider, {
    value: providerValues
  }, children);
};

exports.StoresContextProvider = StoresContextProvider;

var useStoresContext = function useStoresContext() {
  return (0, _react.useContext)(StoresContext);
};

exports.useStoresContext = useStoresContext;

var useStores = function useStores() {
  return (0, _react.useContext)(StoresContext).stores;
};

exports.useStores = useStores;

var useStoresPins = function useStoresPins() {
  return (0, _react.useContext)(StoresContext).pins;
};

exports.useStoresPins = useStoresPins;

var useStoresPinsToFitInView = function useStoresPinsToFitInView() {
  return (0, _react.useContext)(StoresContext).pinsToFitInMap;
};

exports.useStoresPinsToFitInView = useStoresPinsToFitInView;
//# sourceMappingURL=stores.context.js.map