import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-cedb5b78.js';
import './es6.string.iterator-1577cb76.js';
import './core.get-iterator-method-227107e6.js';
import './web.dom.iterable-ace8ed30.js';
import React__default, { useState } from 'react';
import './events-6c3a7a63.js';
import { b as lib_1, a as lib_2 } from './index-41a678ea.js';
import PropTypes from 'prop-types';
import './isSymbol-1981418f.js';
import { d as debounce_1 } from './debounce-89969f66.js';
import './toNumber-e3dcad26.js';
import 'react-dom';
import { V as VisibilitySensor } from './visibility-sensor-72fd8f8b.js';
import { _ as _slicedToArray } from './slicedToArray-f0edf3d6.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;
