import './_commonjsHelpers-0c4b6f40.js';
import './_to-object-a4107da3.js';
import './es6.string.iterator-c990c18c.js';
import './_library-528f1934.js';
import './_iter-detect-5d49a330.js';
import './core.get-iterator-method-e1de7503.js';
import './_object-pie-33c40e79.js';
import { _ as _extends } from './extends-eb69e8ac.js';
import './web.dom.iterable-4439f05a.js';
import React__default, { useState, useEffect } from 'react';
import './events-6c3a7a63.js';
import { a as lib_2 } from './index-41a678ea.js';
import PropTypes from 'prop-types';
import './isSymbol-1981418f.js';
import { d as debounce_1 } from './debounce-9207d8ee.js';
import './toNumber-2f7a84cd.js';
import { d as main_16 } from './main-e4a934b2.js';
import { _ as _slicedToArray } from './slicedToArray-641f241f.js';
import './stringify-4330ccdc.js';
import './asyncToGenerator-fc1c2e29.js';
import Segment from './Segment.js';
import Beam from './Beam.js';

var DFPAdSlot = function DFPAdSlot(_ref) {
  var adUnit = _ref.adUnit,
      sizeMapping = _ref.sizeMapping,
      className = _ref.className,
      slotId = _ref.slotId,
      sizes = _ref.sizes,
      _ref$targeting = _ref.targeting,
      targeting = _ref$targeting === undefined ? {} : _ref$targeting,
      _ref$refreshFlag = _ref.refreshFlag,
      refreshFlag = _ref$refreshFlag === undefined ? true : _ref$refreshFlag,
      checkIsAdFound = _ref.checkIsAdFound,
      _ref$setStickyAd = _ref.setStickyAd,
      setStickyAd = _ref$setStickyAd === undefined ? function () {} : _ref$setStickyAd;

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

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

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      adHasFocus = _useState6[0],
      setAdHasFocus = _useState6[1];

  targeting.ksg = typeof window !== 'undefined' ? window.Krux.segments : null;
  targeting.kuid = typeof window !== 'undefined' ? window.Krux.user : null;

  var adTargeting = _extends({}, targeting, { hostname: typeof window !== 'undefined' ? window.location.hostname : null });

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

    if (event.inViewPercentage <= 0) {
      setAdRefreshable(false);
    } else {
      setAdRefreshable(true);
    }
  }, 500);

  useEffect(function () {
    // Setting hostname into AD targetting
    if (window && window.location && window.location.hostname) {
      adTargeting.hostname = window.location.hostname;
    }
  }, [adTargeting]);

  var _useState7 = useState(null),
      _useState8 = _slicedToArray(_useState7, 2),
      eventData = _useState8[0],
      setEventData = _useState8[1];

  return React__default.createElement(
    'div',
    { className: className, style: { textAlign: 'center' } },
    React__default.createElement(lib_2, {
      style: { marginBottom: '1rem' },
      slotId: slotId,
      sizes: sizes,
      adUnit: adUnit,
      sizeMapping: sizeMapping,
      targetingArguments: adTargeting,
      onSlotIsViewable: function onSlotIsViewable(e) {
        setStickyAd(true);
        Beam.report_beam_ad('Impression', main_16, eventData.cID, eventData.lID, eventData.campID, eventData.adSize, eventData.slot_id);
        Segment.adViewed(eventData.domain, eventData.advertiserId, eventData.campID, eventData.cID, eventData.lID, eventData.adSize, eventData.isEmpty, eventData.sourceAgnosticCreativeId, eventData.sourceAgnosticLineItemId);
      },
      onSlotRender: function onSlotRender(eventData) {
        // Updating the condition to remove the dependency on static slotId.
        if (eventData && eventData.event && checkIsAdFound && checkIsAdFound !== null) {
          if (eventData.event.size !== null && eventData.event.size.length) {
            checkIsAdFound(true, eventData);
          } else {
            checkIsAdFound(false, eventData);
          }
        }

        setAdRendered(true);
        if (eventData && eventData.event) {
          var cID = eventData.event.creativeId;
          var lID = eventData.event.lineItemId;
          var campID = eventData.event.campaignId;
          var adSize = eventData.event.size && eventData.event.size.length > 1 ? eventData.event.size[0] + 'x' + eventData.event.size[1] : '';
          var slot_id = eventData.slotId ? eventData.slotId : '';
          var isEmpty = eventData.event.isEmpty;
          var sourceAgnosticCreativeId = eventData.event.sourceAgnosticCreativeId;
          var sourceAgnosticLineItemId = eventData.event.sourceAgnosticLineItemId;
          var advertiserId = eventData.event.advertiserId;
          var domain = typeof window !== 'undefined' && window.location ? window.location.hostname : '';

          setEventData({
            cID: cID,
            lID: lID,
            campID: campID,
            adSize: adSize,
            slot_id: slot_id,
            isEmpty: isEmpty,
            sourceAgnosticCreativeId: sourceAgnosticCreativeId,
            sourceAgnosticLineItemId: sourceAgnosticLineItemId,
            advertiserId: advertiserId,
            domain: domain
          });

          var slot_div = document.getElementById(eventData.slotId);
          if (slot_div) {
            slot_div.addEventListener('mouseenter', function () {
              var timeoutId = slot_div.getAttribute('timeoutId');
              clearTimeout(timeoutId);
              slot_div.setAttribute('hasFocus', 'yes');
            });

            slot_div.addEventListener('mouseleave', function () {
              var timeoutId = setTimeout(function () {
                slot_div.setAttribute('hasFocus', 'no');
              }, 60);
              slot_div.setAttribute('timeoutId', timeoutId);
            });

            document.addEventListener('visibilitychange', function () {
              if (!document.hidden) {
                return;
              }

              var hasFocus = slot_div.getAttribute('hasFocus');
              if (hasFocus == 'yes' && document.hidden) {
                slot_div.setAttribute('hasFocus', 'no');
                Beam.report_beam_ad('Click', main_16, cID, lID, campID, adSize, slotId);
                Segment.adClicked(domain, advertiserId, campID, cID, lID, adSize, isEmpty, sourceAgnosticCreativeId, sourceAgnosticLineItemId);
              }
            });
          } else {
            console.error('slot div not found:', slotId);
          }
        }
      },
      onSlotVisibilityChanged: handleVisibilityChange,
      shouldRefresh: function shouldRefresh(event) {
        if (adRendered && adRefreshable && refreshFlag) return true;else return false;
      }
    })
  );
};

DFPAdSlot.propTypes = {
  adUnit: PropTypes.string.isRequired,
  slotId: PropTypes.string,
  className: PropTypes.string,
  sizeMapping: PropTypes.array,
  sizes: PropTypes.array
};

export default DFPAdSlot;
