'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('./_commonjsHelpers-06173234.js');
require('./_to-object-329a650b.js');
require('./es6.string.iterator-cc0bbaa4.js');
require('./_library-dd23b178.js');
require('./_iter-detect-60b2f026.js');
require('./core.get-iterator-method-f62321d4.js');
require('./web.dom.iterable-a0e279c1.js');
var React = require('react');
var React__default = _interopDefault(React);
require('./index-fa8ed292.js');
require('prop-types');
require('./isSymbol-04666465.js');
require('./debounce-eb287781.js');
require('./main-a4cb15bb.js');
var stringify = require('./stringify-7c5aff47.js');
var slicedToArray = require('./slicedToArray-8f385299.js');
require('./asyncToGenerator-533d476a.js');
require('./_set-species-f92c67c5.js');
require('./beam-7217b7e7.js');
var AdSlot = require('./AdSlot.js');

var ADSponsoredResources = function ADSponsoredResources(_ref) {
  var sponsoredAd = _ref.sponsoredAd,
      _ref$minScreenWidth = _ref.minScreenWidth,
      minScreenWidth = _ref$minScreenWidth === undefined ? 1200 : _ref$minScreenWidth;
  var className = sponsoredAd.className,
      slotId = sponsoredAd.slotId,
      adUnit = sponsoredAd.adUnit,
      _sponsoredAd$targetin = sponsoredAd.targeting,
      targeting = _sponsoredAd$targetin === undefined ? {} : _sponsoredAd$targetin,
      _sponsoredAd$position = sponsoredAd.position,
      position = _sponsoredAd$position === undefined ? 'sponsor' : _sponsoredAd$position,
      networkID = sponsoredAd.networkID,
      sizes = sponsoredAd.sizes;


  var adTargeting = {
    content_placement: targeting.content_placement,
    tags: targeting.tags,
    document_group: targeting.document_group,
    document_url: targeting.document_url,
    pos: position,
    campaign_id: targeting.campaign_id,
    issue_url: targeting.issue_url,
    publication_url: targeting.publication_url
  };

  var _useState = React.useState(false),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      widthFlag = _useState2[0],
      setWidthFlag = _useState2[1];

  React.useEffect(function () {
    if (window && window.innerWidth >= minScreenWidth) {
      setWidthFlag(true);
    }
  }, [minScreenWidth]);

  var collapseAd = function collapseAd() {
    var parent = document.getElementById('sponsored-ad-wrapper');
    parent.style.height = '0%';
    parent.style.width = '0%';
    parent.style.display = 'none';
  };

  var checkIsAdFound = function checkIsAdFound(isFound, adData) {
    if (isFound) {
      var parent = document.getElementById('sponsored-ad-wrapper');

      var adSizes = [[1, 1]];
      // Collapsing ad if the as size is [1,1]
      if (adData && adData.event && adData.event.size) {
        var checkAdSize = function checkAdSize(size) {
          return stringify._JSON$stringify(size) === stringify._JSON$stringify(adData.event.size);
        };

        if (adSizes.some(checkAdSize)) {
          // collapsing sponsoredResources AD.
          collapseAd();
          parent.style.display = 'none';
        } else {
          parent.style.display = 'block';
        }
      }
    } else {
      collapseAd();
    }
  };

  if (widthFlag) {
    return React__default.createElement(
      React__default.Fragment,
      null,
      React__default.createElement(
        'div',
        { id: 'sponsored-ad-wrapper', className: 'sponsored-ad-wrapper' },
        React__default.createElement(
          'div',
          { className: 'sponsored-ad' },
          React__default.createElement(AdSlot, {
            className: className,
            slotId: slotId,
            networkID: networkID,
            sizes: sizes,
            adUnit: adUnit,
            targeting: adTargeting,
            refreshFlag: false,
            checkIsAdFound: checkIsAdFound
          })
        ),
        React__default.createElement(
          'style',
          { jsx: 'true' },
          '\n            /* setting height to 0px when the ad slot is empty. */\n            .sponsored-ad-wrapper div[id*=\'google_ads_iframe\']:empty {\n              height: 0px !important;\n            }\n          '
        )
      )
    );
  } else return null;
};

module.exports = ADSponsoredResources;
