import { a as _asyncToGenerator, r as regenerator } from './asyncToGenerator-5ef104a7.js';
import { _ as _toConsumableArray } from './toConsumableArray-f7074d7c.js';
import React__default, { useEffect } from 'react';
import ReactGA from 'react-ga';
import { l as lib_3, b as lib_1 } from './index-f0fc23eb.js';
import { d as main_24, m as main_43 } from './main-1894c974.js';
import './_commonjsHelpers-0c4b6f40.js';
import './core.get-iterator-method-ea258bb1.js';
import './web.dom.iterable-ab4dea5c.js';
import './_library-528f1934.js';
import './_iter-detect-98db3b07.js';
import './events-6c3a7a63.js';
import 'prop-types';

var _this = undefined;

var AdSlotsProvider = function AdSlotsProvider(_ref) {
  var children = _ref.children,
      networkID = _ref.networkID,
      _ref$targeting = _ref.targeting,
      targeting = _ref$targeting === undefined ? {} : _ref$targeting,
      _ref$singleRequest = _ref.singleRequest,
      singleRequest = _ref$singleRequest === undefined ? true : _ref$singleRequest,
      _ref$autoRefresh = _ref.autoRefresh,
      autoRefresh = _ref$autoRefresh === undefined ? false : _ref$autoRefresh,
      _ref$refreshADs = _ref.refreshADs,
      refreshADs = _ref$refreshADs === undefined ? {} : _ref$refreshADs,
      _ref$refreshInterval = _ref.refreshInterval,
      refreshInterval = _ref$refreshInterval === undefined ? 60 : _ref$refreshInterval,
      _ref$disableInitialLo = _ref.disableInitialLoad,
      disableInitialLoad = _ref$disableInitialLo === undefined ? false : _ref$disableInitialLo;

  var logEvent = function logEvent(action, category, GA_TRACKING_ID) {
    if (!GA_TRACKING_ID && typeof window !== 'undefined') GA_TRACKING_ID = sessionStorage.getItem('GA_TRACKING_ID');

    if (GA_TRACKING_ID && GA_TRACKING_ID.includes(',')) {
      ReactGA.event({
        category: category,
        action: action,
        nonInteraction: true
      }, GA_TRACKING_ID.split(',').map(function (id, index) {
        return 'ga_id_' + ++index;
      }));
    } else {
      ReactGA.event({
        category: category,
        action: action,
        nonInteraction: true
      });
    }
  };
  useEffect(function () {
    // Refresh ADs after every one minute if user didn't scroll.
    // refreshInterval accepts time in seconds and default is 60 seconds
    var generateAdsToRefresh = function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee(ads) {
        var googletag, slots, domAds, adsToRefresh;
        return regenerator.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return lib_3.getGoogletag();

              case 2:
                googletag = _context.sent;

                if (!(googletag && googletag.pubadsReady)) {
                  _context.next = 8;
                  break;
                }

                slots = googletag.pubads().getSlots();
                domAds = slots.map(function (slot) {
                  return slot.getSlotId().getDomId();
                });
                adsToRefresh = domAds.reduce(function (acc, item) {
                  ads.forEach(function (ad) {
                    if (item.includes(ad)) {
                      acc = [].concat(_toConsumableArray(acc), [item]);
                    }
                  });
                  return acc;
                }, []);
                return _context.abrupt('return', adsToRefresh);

              case 8:
                return _context.abrupt('return', ads);

              case 9:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, _this);
      }));

      return function generateAdsToRefresh(_x) {
        return _ref2.apply(this, arguments);
      };
    }();
    var isScrolling = void 0;
    var adRefreshCount = 0;
    var refreshADsAfterInterval = function refreshADsAfterInterval() {
      isScrolling = setInterval(_asyncToGenerator( /*#__PURE__*/regenerator.mark(function _callee2() {
        var _refreshADs$mobileAds, mobileAds, _refreshADs$desktopAd, desktopAds, ads;

        return regenerator.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _refreshADs$mobileAds = refreshADs.mobileAds, mobileAds = _refreshADs$mobileAds === undefined ? ['leaderboard-ad'] : _refreshADs$mobileAds, _refreshADs$desktopAd = refreshADs.desktopAds, desktopAds = _refreshADs$desktopAd === undefined ? [] : _refreshADs$desktopAd;
                ads = [];

                if (!main_43) {
                  _context2.next = 8;
                  break;
                }

                _context2.next = 5;
                return generateAdsToRefresh(mobileAds);

              case 5:
                ads = _context2.sent;
                _context2.next = 12;
                break;

              case 8:
                if (!main_24) {
                  _context2.next = 12;
                  break;
                }

                _context2.next = 11;
                return generateAdsToRefresh(desktopAds);

              case 11:
                ads = _context2.sent;

              case 12:
                lib_3.refresh.apply(lib_3, _toConsumableArray(ads));
                logEvent('AD Refresh', 'AD');
                adRefreshCount++;
                console.log('NumOfTimesAd refreshed', adRefreshCount);
                if (adRefreshCount == 5) clearInterval(isScrolling);

              case 17:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, _this);
      })), parseInt(refreshInterval) * 1000);
    };
    autoRefresh && refreshADsAfterInterval();
    autoRefresh && window.addEventListener('scroll', function () {
      clearInterval(isScrolling);
      refreshADsAfterInterval();
    }, false);
    return function () {
      clearInterval(isScrolling);
    };
  }, []);

  return React__default.createElement(
    lib_1,
    {
      dfpNetworkId: networkID,
      targeting: targeting,
      singleRequest: singleRequest,
      lazyLoad: { fetchMarginPercent: 0, renderMarginPercent: 0, mobileScaling: 1.0 },
      disableInitialLoad: disableInitialLoad },
    children
  );
};

export default AdSlotsProvider;
