'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');
var main = 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');

// removed static slotID ,because defining static slotID is restricting AD to render only once.
var AD300x250 = function AD300x250(_ref) {
  var networkID = _ref.networkID,
      adUnit = _ref.adUnit,
      _ref$slotId = _ref.slotId,
      slotId = _ref$slotId === undefined ? '' : _ref$slotId,
      _ref$targeting = _ref.targeting,
      targeting = _ref$targeting === undefined ? {} : _ref$targeting,
      _ref$pos = _ref.pos,
      pos = _ref$pos === undefined ? 'rec1' : _ref$pos;

  var mobileView = false;
  if (main.main_36) {
    pos = 'rec1-mobile';
    mobileView = true;
  }

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

  React.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 adTargeting = {
    content_placement: targeting.content_placement,
    tags: targeting.tags,
    document_group: targeting.document_group,
    document_url: targeting.document_url,
    pos: pos,
    campaign_id: targeting.campaign_id,
    issue_url: targeting.issue_url,
    publication_url: targeting.publication_url
  };

  var browserSizes = [[300, 250], [300, 100]];
  var mobileSizes = [[300, 250], [300, 100]];

  // Function to collapse ad
  var collapseAd = function collapseAd(slotID) {
    var parent = document.getElementById(slotID);
    // finding the parent wrapper div
    var parentWrapper = parent && parent.closest('.AD300x250');
    var mediaWrapper = parent && parent.closest('.media');
    // collapsing the parent wrapper div
    if (parentWrapper) {
      parentWrapper.style.display = 'none';
    } else if (mediaWrapper) {
      mediaWrapper.style.display = 'none';
    }

    parent.style.height = '0%';
    parent.style.width = '0%';
    parent.style.display = 'none';
  };

  // CallBack function to check if ad has got creative or not.
  var checkIsAdFound = function checkIsAdFound(isFound, adData) {
    if (isFound) {
      // Dynamically fetching parent from slotID to display or collapse.
      var parent = document.getElementById(adData.slotId);

      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)) {
          // passing slotID to collape
          collapseAd(adData.slotId);
          parent.style.display = 'none';
        } else {
          parent.style.display = 'block';
        }
      }
    } else {
      // collapsing ad if no creative is found
      collapseAd(adData.slotId);
    }
  };

  return React__default.createElement(
    'div',
    null,
    React__default.createElement(AdSlot, {
      networkID: networkID,
      adUnit: adUnit,
      slotId: slotId,
      targeting: adTargeting,
      className: 'AD300x250',
      sizes: mobileView ? mobileSizes : browserSizes
      /* Passing function to check for ad creative only if it is not in preview. */
      , checkIsAdFound: previewAd ? null : checkIsAdFound,
      style: { display: 'none' }
    }),
    React__default.createElement(
      'style',
      { jsx: 'true' },
      '\n        .AD300x250 .div[id*=\'google_ads_iframe\']:empty {\n          height: 0px !important;\n        }\n      '
    )
  );
};

module.exports = AD300x250;
