import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-ded78eb9.js';
import './es6.string.iterator-9b2af4a5.js';
import './core.get-iterator-method-54d2e411.js';
import './web.dom.iterable-8956f9a5.js';
import React__default, { useState } from 'react';
import { b as lib_1, a as lib_2 } from './index-0e45a0b2.js';
import PropTypes from 'prop-types';
import './isSymbol-807e927e.js';
import { d as debounce_1 } from './debounce-1a993acf.js';
import 'react-dom';
import { V as VisibilitySensor } from './visibility-sensor-0baa4eea.js';
import { _ as _slicedToArray } from './slicedToArray-7021e5df.js';

var AD = function AD(_ref) {
  var networkID = _ref.networkID,
      adUnit = _ref.adUnit,
      sizeMapping = _ref.sizeMapping,
      className = _ref.className,
      slotId = _ref.slotId,
      sizes = _ref.sizes,
      minInViewPercent = _ref.minInViewPercent,
      _ref$targeting = _ref.targeting,
      targeting = _ref$targeting === undefined ? {} : _ref$targeting;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      adRendered = _useState2[0],
      setAdRendered = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      showPlaceholder = _useState4[0],
      setShowPlaceholder = _useState4[1];

  var handleVisibilityChange = debounce_1(function (_ref2) {
    var event = _ref2.event;

    if (event.inViewPercentage <= 0) {
      setShowPlaceholder(true);
    }
  }, 500);

  return React__default.createElement(
    React__default.Fragment,
    null,
    !showPlaceholder && React__default.createElement(
      lib_1,
      {
        dfpNetworkId: networkID,
        targetingArguments: targeting,
        sizeMapping: sizeMapping,
        lazyLoad: { fetchMarginPercent: 500, renderMarginPercent: 200, mobileScaling: 2.0 } },
      React__default.createElement(
        'div',
        { className: className },
        React__default.createElement(lib_2, {
          slotId: slotId,
          sizes: sizes,
          adUnit: adUnit,
          sizeMapping: sizeMapping,
          onSlotRender: function onSlotRender(eventData) {
            setAdRendered(true);
          },
          onSlotVisibilityChanged: handleVisibilityChange
        })
      )
    ),
    adRendered && showPlaceholder && React__default.createElement(
      VisibilitySensor,
      {
        partialVisibility: true,
        onChange: function onChange(isVisible) {
          if (isVisible) {
            setShowPlaceholder(false);
          }
        } },
      React__default.createElement(
        'div',
        { style: { height: 'auto' } },
        '\xA0'
      )
    )
  );
};

AD.propTypes = {
  networkID: PropTypes.string.isRequired,
  adUnit: PropTypes.string.isRequired,
  slotId: PropTypes.string,
  className: PropTypes.string,
  sizeMapping: PropTypes.array,
  sizes: PropTypes.array,
  minInViewPercent: PropTypes.number
};

export default AD;
