import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import React__default, { useState } from 'react';
import PropTypes from 'prop-types';
import { b as lib_1, a as lib_2 } from './index-f0fc23eb.js';
import { V as VisibilitySensor } from './visibility-sensor-72fd8f8b.js';
import { d as debounce_1 } from './debounce-533d9748.js';
import './_commonjsHelpers-0c4b6f40.js';
import './web.dom.iterable-ab4dea5c.js';
import './core.get-iterator-method-ea258bb1.js';
import './events-6c3a7a63.js';
import 'react-dom';
import './isSymbol-a7e9aafe.js';
import './toNumber-5aeba6e7.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;
      _ref.minInViewPercent;
      var _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;
