"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useRecommendAds = exports.useAds = exports.useAdSettings = exports.AdEngineProvider = exports.AdEngineContext = void 0;
var _react = require("react");
var _AdEngine = require("../../core/engines/AdEngine");
var _TimeWindowTracker = require("../../core/engines/TimeWindowTracker");
const AdEngineContext = exports.AdEngineContext = /*#__PURE__*/(0, _react.createContext)({
  isLoading: true,
  ads: [],
  settings: null,
  saveItemsPagination: () => {},
  getItemsPaginationCache: () => undefined
});
const AdEngineProvider = ({
  children
}) => {
  const [networkAds, setNetworkAds] = (0, _react.useState)(null);
  const [isLoading, setIsLoading] = (0, _react.useState)(true);
  const [itemsPaginationCache, setItemsPaginationCache] = (0, _react.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);
  };
  (0, _react.useEffect)(() => {
    async function init() {
      _AdEngine.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);
};
exports.AdEngineProvider = AdEngineProvider;
const useAds = () => {
  const adContext = (0, _react.useContext)(AdEngineContext);
  return adContext.ads;
};
exports.useAds = useAds;
const useAdSettings = () => {
  const adContext = (0, _react.useContext)(AdEngineContext);
  return adContext.settings;
};
exports.useAdSettings = useAdSettings;
const useRecommendAds = ({
  count,
  placement,
  communityId
}) => {
  const adContext = (0, _react.useContext)(AdEngineContext);
  const ads = adContext.ads;
  const [recommendedAds, setRecommendedAds] = (0, _react.useState)();
  const adSettings = useAdSettings();
  const adFrequency = _AdEngine.AdEngine.instance.getAdFrequencyByPlacement(placement);
  const getRecommendedAds = (0, _react.useCallback)(() => {
    _AdEngine.AdEngine.instance.getRecommendedAds({
      placement,
      count,
      communityId
    }).then(ads => {
      setRecommendedAds(ads);
    });
  }, [count, placement, communityId]);
  (0, _react.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.TimeWindowTracker.instance.hasReachedLimit(placement)) {
      setRecommendedAds([]);
      return;
    }
    getRecommendedAds();
  }, [ads, count, placement, communityId, adFrequency, adSettings, getRecommendedAds]);
  const resetRecommendedAds = (0, _react.useCallback)(() => {
    setRecommendedAds(undefined);
    getRecommendedAds();
  }, [getRecommendedAds]);
  return {
    resetRecommendedAds,
    recommendedAds
  };
};
exports.useRecommendAds = useRecommendAds;
//# sourceMappingURL=AdEngineProvider.js.map