import { useContext, useEffect, useState, createContext, useCallback } from 'react';
import { AdEngine } from '../../core/engines/AdEngine';
import { TimeWindowTracker } from '../../core/engines/TimeWindowTracker';
export const AdEngineContext = /*#__PURE__*/createContext({
  isLoading: true,
  ads: [],
  settings: null,
  saveItemsPagination: () => {},
  getItemsPaginationCache: () => undefined
});
export const AdEngineProvider = ({
  children
}) => {
  const [networkAds, setNetworkAds] = useState(null);
  const [isLoading, setIsLoading] = useState(true);
  const [itemsPaginationCache, setItemsPaginationCache] = useState(new Map());
  const getCacheKey = ({
    targetId,
    placement,
    frequencyType
  }) => {
    if (targetId) {
      return `${frequencyType}-${placement}-${frequencyType}`;
    } else {
      return `global-${placement}-${frequencyType}`;
    }
  };
  const saveItemsPagination = ({
    targetId,
    placement,
    adId,
    referenceId,
    frequencyType
  }) => {
    setItemsPaginationCache(prev => {
      const cacheKey = getCacheKey({
        targetId,
        placement,
        frequencyType
      });
      const prevValue = prev.get(cacheKey);
      if (prevValue) {
        prev.set(cacheKey, [...prevValue, {
          adId,
          referenceId
        }]);
      } else {
        prev.set(cacheKey, [{
          adId,
          referenceId
        }]);
      }
      return new Map(prev);
    });
  };
  const getItemsPaginationCache = ({
    targetId,
    placement,
    frequencyType
  }) => {
    const cacheKey = getCacheKey({
      targetId,
      placement,
      frequencyType
    });
    return itemsPaginationCache.get(cacheKey);
  };
  useEffect(() => {
    async function init() {
      AdEngine.instance.onNetworkAdsData(networkAds => {
        setNetworkAds(networkAds);
        setIsLoading(false);
      });
    }
    init();
  }, []);
  return /*#__PURE__*/React.createElement(AdEngineContext.Provider, {
    value: {
      isLoading,
      ads: (networkAds === null || networkAds === void 0 ? void 0 : networkAds.ads) || [],
      settings: (networkAds === null || networkAds === void 0 ? void 0 : networkAds.settings) || null,
      saveItemsPagination,
      getItemsPaginationCache
    }
  }, children);
};
export const useAds = () => {
  const adContext = useContext(AdEngineContext);
  return adContext.ads;
};
export const useAdSettings = () => {
  const adContext = useContext(AdEngineContext);
  return adContext.settings;
};
export const useRecommendAds = ({
  count,
  placement,
  communityId
}) => {
  const adContext = useContext(AdEngineContext);
  const ads = adContext.ads;
  const [recommendedAds, setRecommendedAds] = useState();
  const adSettings = useAdSettings();
  const adFrequency = AdEngine.instance.getAdFrequencyByPlacement(placement);
  const getRecommendedAds = useCallback(() => {
    AdEngine.instance.getRecommendedAds({
      placement,
      count,
      communityId
    }).then(ads => {
      setRecommendedAds(ads);
    });
  }, [count, placement, communityId]);
  useEffect(() => {
    if (!(adSettings !== null && adSettings !== void 0 && adSettings.enabled) || ads.length === 0) {
      setRecommendedAds([]);
      return;
    }
    if ((adFrequency === null || adFrequency === void 0 ? void 0 : adFrequency.type) === 'time-window' && TimeWindowTracker.instance.hasReachedLimit(placement)) {
      setRecommendedAds([]);
      return;
    }
    getRecommendedAds();
  }, [ads, count, placement, communityId, adFrequency, adSettings, getRecommendedAds]);
  const resetRecommendedAds = useCallback(() => {
    setRecommendedAds(undefined);
    getRecommendedAds();
  }, [getRecommendedAds]);
  return {
    resetRecommendedAds,
    recommendedAds
  };
};
//# sourceMappingURL=AdEngineProvider.js.map