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

var ADlgInfeed = function ADlgInfeed(props) {
  var className = props.className,
      slotId = props.slotId,
      adUnit = props.adUnit,
      _props$targeting = props.targeting,
      targeting = _props$targeting === undefined ? {} : _props$targeting,
      networkID = props.networkID,
      sizes = props.sizes,
      index = props.index;

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

  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 checkIsAdFound = function checkIsAdFound(isFound, adData) {
    if (isFound) {
      var parent = document.getElementById('lg-infeed-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 lg-infeed AD.
          collapseAd();
          parent.style.display = 'none';
        } else {
          parent.style.display = 'flex';
        }
      }
    } else {
      collapseAd();
    }
  };

  return React__default.createElement(
    'div',
    { id: 'lg-infeed-wrapper-' + (index + 1), className: 'lg-infeed-wrapper', style: { display: 'none', flex: '1 0 auto' } },
    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        .lg-infeed-wrapper {\n          flex-direction: row;\n          margin-bottom: 1.25rem;\n          padding-bottom: 1.25rem;\n          border-bottom: 1px solid rgb(221, 221, 221);\n        }\n        .lg-infeed-wrapper div[id*=\'google_ads_iframe\']:empty {\n          height: 0px !important;\n        }\n      '
    )
  );
};

export { ADlgInfeed as A };
