import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-a4107da3.js';
import './es6.string.iterator-c990c18c.js';
import './_library-528f1934.js';
import './_iter-detect-5d49a330.js';
import { _ as _toConsumableArray } from './toConsumableArray-43aa614d.js';
import './core.get-iterator-method-e1de7503.js';
import './web.dom.iterable-4439f05a.js';
import React__default, { useEffect } from 'react';
import { l as lib_3, b as lib_1 } from './index-a9e53f80.js';
import 'prop-types';
import { e as main_22, m as main_36 } from './main-e4a934b2.js';
import { a as _asyncToGenerator, r as regenerator } from './asyncToGenerator-502f13b4.js';
import './_set-species-3f8319f5.js';

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;

  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 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_36) {
                  _context2.next = 8;
                  break;
                }

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

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

              case 8:
                if (!main_22) {
                  _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));

              case 13:
              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 } },
    children
  );
};

export default AdSlotsProvider;
