'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');
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('./Segment.js');
require('./Beam.js');
var AdSlot = require('./AdSlot.js');

var ADWelcome = function ADWelcome(_ref) {
  var title = _ref.title,
      welcomeAd = _ref.welcomeAd,
      setFlag = _ref.setFlag,
      Website = _ref.Website,
      counter = _ref.counter;
  var className = welcomeAd.className,
      slotId = welcomeAd.slotId,
      adUnit = welcomeAd.adUnit,
      _welcomeAd$targeting = welcomeAd.targeting,
      targeting = _welcomeAd$targeting === undefined ? {} : _welcomeAd$targeting,
      _welcomeAd$position = welcomeAd.position,
      position = _welcomeAd$position === undefined ? 'welcome' : _welcomeAd$position,
      networkID = welcomeAd.networkID,
      sizes = welcomeAd.sizes;

  var adTargeting = {
    content_placement: targeting.content_placement,
    tags: targeting.tags,
    document_group: targeting.document_group,
    document_url: targeting.document_url,
    pos: position,
    campaign_id: targeting.campaign_id,
    issue_url: targeting.issue_url,
    publication_url: targeting.publication_url
  };

  var calculateTimeLeft = function calculateTimeLeft(timeLeft) {
    return --timeLeft;
  };

  var _useState = React.useState(counter),
      _useState2 = slicedToArray._slicedToArray(_useState, 2),
      timeLeft = _useState2[0],
      setTimeLeft = _useState2[1];

  var _useState3 = React.useState(true),
      _useState4 = slicedToArray._slicedToArray(_useState3, 2),
      previewAd = _useState4[0],
      setPreviewAd = _useState4[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');
    }));
  }, []);

  React.useEffect(function () {
    if (timeLeft > 0) {
      setTimeout(function () {
        setTimeLeft(calculateTimeLeft(timeLeft));
      }, 1000);
    } else {
      document.body.style.overflow = 'auto';
      setFlag(true);
    }
  });

  var checkIsAdFound = function checkIsAdFound(isFound, adData) {
    if (isFound) {
      var parent = document.getElementById('welcome-modal');
      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)) {
          // collapsing webcast AD.
          setFlag(true);
          document.body.style.overflow = 'auto';
        } else {
          document.body.style.overflow = 'hidden';
          parent.style.display = 'block';
          calculateTimeLeft(counter + 1);
        }
      } else {
        document.body.style.overflow = 'hidden';
        parent.style.display = 'block';
        calculateTimeLeft(counter + 1);
      }
    } else {
      setFlag(true);
      document.body.style.overflow = 'auto';
    }
  };

  return React__default.createElement(
    'div',
    { style: { outline: 'none', display: 'none' }, className: 'welcome-modal', id: 'welcome-modal' },
    React__default.createElement(
      'div',
      { className: 'welcome-ad-head-wrapper' },
      React__default.createElement(
        'div',
        { className: 'left-top' },
        React__default.createElement('img', { style: { width: '100%' }, src: Website.logo, alt: Website.title })
      ),
      React__default.createElement(
        'div',
        { className: 'right-top' },
        React__default.createElement(
          'span',
          {
            className: 'continue-site',
            style: {
              fontSize: '1.2rem',
              fontWeight: 'bold',
              fontFamily: "'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"
            },
            onClick: function onClick() {
              document.body.style.overflow = 'auto';
              setFlag(true);
            } },
          'CONTINUE TO SITE >'
        ),
        React__default.createElement(
          'p',
          {
            style: {
              fontSize: '0.8rem',
              fontFamily: "'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'"
            },
            className: 'timer-block' },
          'OR WAIT ' + timeLeft + ' SECS'
        )
      )
    ),
    React__default.createElement(
      'div',
      { className: title },
      React__default.createElement(AdSlot, {
        className: className,
        slotId: slotId,
        networkID: networkID,
        sizes: sizes,
        adUnit: adUnit,
        targeting: adTargeting,
        refreshFlag: false
        /* Passing function to check for ad creative only if it is not in preview. */
        , checkIsAdFound: previewAd ? null : checkIsAdFound
      })
    ),
    React__default.createElement(
      'style',
      { jsx: 'true' },
      '\n        .continue-site:hover {\n          cursor: pointer;\n        }\n\n        .welcome-modal {\n          max-width: 100% !important;\n          margin: 0px !important;\n          height: 100vh;\n        }\n        .welcome-ad-head-wrapper {\n          display: flex;\n          justify-content: space-between;\n        }\n        .right-top {\n          max-width: 250px;\n          padding: 10px;\n        }\n        .left-top {\n          max-width: 250px;\n          padding: 10px;\n        }\n        .' + title + ' {\n          width: auto !important;\n          padding-top: 25px;\n          justify-content: center;\n          align-items: center;\n          display: flex;\n        }\n        /* setting height to 0px when the ad slot is empty. */\n        .' + title + ' div[id*=\'google_ads_iframe\']:empty {\n          height: 0px !important;\n        }\n      '
    )
  );
};

module.exports = ADWelcome;
