import React__default, { useState, useEffect } from 'react';
import Card from 'react-bootstrap/Card';
import { _ as _JSON$stringify } from './stringify-008c8802.js';
import { _ as _slicedToArray } from './slicedToArray-7021e5df.js';
import DFPAdSlot from './AdSlot.js';

var ADInfeed = function ADInfeed(_ref) {
  var index = _ref.index,
      infeedAd = _ref.infeedAd;
  var className = infeedAd.className,
      slotId = infeedAd.slotId,
      adUnit = infeedAd.adUnit,
      _infeedAd$targeting = infeedAd.targeting,
      targeting = _infeedAd$targeting === undefined ? {} : _infeedAd$targeting,
      networkID = infeedAd.networkID,
      sizes = infeedAd.sizes;

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      previewAd = _useState2[0],
      setPreviewAd = _useState2[1];

  useEffect(function () {
    var path = window && window.location && window.location.search;
    // Setting flag for Ad-preview URLs
    setPreviewAd(path.split('&').some(function (param) {
      return param.includes('creativeId');
    }));
  }, []);

  var collapseAd = function collapseAd() {
    var parent = document.getElementById('brand-insight-wrapper-' + (index + 1));
    parent.style.height = '0%';
    parent.style.width = '0%';
    parent.style.display = 'none';
  };

  var checkIsAdFound = function checkIsAdFound(isFound, adData) {
    if (isFound) {
      var parent = document.getElementById('brand-insight-wrapper-' + (index + 1));

      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 _JSON$stringify(size) === _JSON$stringify(adData.event.size);
        };

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

  return React__default.createElement(
    'div',
    { id: 'brand-insight-wrapper-' + (index + 1), className: 'brand-insight-wrapper', style: { display: 'none', flex: '1 0 auto' } },
    React__default.createElement(
      Card,
      { className: 'ad-wrapper', style: { display: 'block !important', flexDirection: index === 0 || index % 4 === 0 ? 'top' : 'row' } },
      React__default.createElement(DFPAdSlot, {
        className: className,
        networkID: networkID,
        adUnit: adUnit,
        slotId: slotId,
        targeting: targeting,
        sizes: sizes
        /* Passing function to check for ad creative only if it is not in preview. */
        , checkIsAdFound: previewAd ? null : checkIsAdFound,
        refreshFlag: false
      })
    ),
    React__default.createElement(
      'style',
      { jsx: 'true' },
      '\n        .brand-insight-wrapper div[id*=\'google_ads_iframe\']:empty {\n          height: 0px !important;\n        }\n      '
    )
  );
};

export { ADInfeed as A };
