import { _ as _slicedToArray } from './slicedToArray-6ebcbfea.js';
import React__default, { useState, useEffect } from 'react';
import { useRouter } from 'next/router';
import { V as VisibilitySensor } from './visibility-sensor-72fd8f8b.js';
import './_commonjsHelpers-0c4b6f40.js';
import './web.dom.iterable-ab4dea5c.js';
import './core.get-iterator-method-ea258bb1.js';
import 'react-dom';

var checkIfLasso = function checkIfLasso(code) {
  return code.lastIndexOf('trc.lhmos.com') !== -1;
};

var PixelTrackingLandingPages = React__default.memo(function (_ref) {
  var pixelTracking = _ref.pixelTracking;

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

  var _useState3 = useState(pixelTracking && pixelTracking.trackingCode ? pixelTracking.trackingCode : null),
      _useState4 = _slicedToArray(_useState3, 2),
      trackingCode = _useState4[0];
      _useState4[1];

  if (!pixelTracking) return null;

  var router = useRouter();

  useEffect(function () {
    if (trackingCode && pixelTracking.is_active) {
      setNeedTracking(true);
    }
  }, [trackingCode]);

  useEffect(function () {
    router.events.on('routeChangeComplete', function () {
      handleReload();
    });
    return function () {
      router.events.off('routeChangeComplete', function () {});
    };
  }, [router.events]);

  var handleReload = function handleReload() {
    setNeedTracking(false);
    setTimeout(function () {
      setNeedTracking(true);
    }, 100);
  };

  var renderCode = function renderCode(code) {
    if (window.LassoImpressionID && code && checkIfLasso(code)) {
      var delimiter = code.lastIndexOf('?' === -1) ? '?' : '&';
      code += delimiter + 'LassoImpressionID=' + window.LassoImpressionID;
    }
    return React__default.createElement('div', { style: { height: '0', opacity: 0 }, key: code, dangerouslySetInnerHTML: { __html: code } });
  };

  return React__default.createElement(
    React__default.Fragment,
    null,
    needTracking && renderCode(trackingCode)
  );
});

// Article page tracking

var PixelTrackingArticle = React__default.memo(function (_ref2) {
  var article = _ref2.article,
      infiniteScroll = _ref2.infiniteScroll;

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

  var _useState7 = useState([]),
      _useState8 = _slicedToArray(_useState7, 2),
      trackingCode = _useState8[0],
      setTrackingCode = _useState8[1];

  var _useState9 = useState(false),
      _useState10 = _slicedToArray(_useState9, 2),
      inView = _useState10[0],
      setInView = _useState10[1];

  var _useState11 = useState(0),
      _useState12 = _slicedToArray(_useState11, 2),
      containerHeight = _useState12[0],
      setContainerHeight = _useState12[1];

  if (!article) return null;

  useEffect(function () {
    setContainerHeight(window.innerHeight);
    var trackingArray = [];

    //content_placement
    if (article.content_placement && article.content_placement.length) {
      article.content_placement.forEach(function (cp) {
        if (cp.pixelTrackingCode && cp.pixelTrackingCode.trackingCode && cp.pixelTrackingCode.is_active) {
          //  console.log('has pixel code: content placement :', cp.pixelTrackingCode.trackingCode)
          if (!trackingArray.includes(cp.pixelTrackingCode.trackingCode)) {
            trackingArray.push(cp.pixelTrackingCode.trackingCode);
          }
        }
      });
    }

    //documentGroup
    if (article.documentGroup && article.documentGroup.pixelTrackingCode) {
      if (article.documentGroup.pixelTrackingCode.trackingCode && article.documentGroup.pixelTrackingCode.is_active) {
        // console.log('has pixel code: docGroup :', article.documentGroup.pixelTrackingCode.trackingCode)
        if (!trackingArray.includes(article.documentGroup.pixelTrackingCode.trackingCode)) {
          trackingArray.push(article.documentGroup.pixelTrackingCode.trackingCode);
        }
      }
    }

    //documentGroupMapping
    if (article.documentGroupMapping && article.documentGroupMapping.length) {
      article.documentGroupMapping.forEach(function (dg) {
        if (dg.pixelTrackingCode && dg.pixelTrackingCode.trackingCode && dg.pixelTrackingCode.is_active) {
          // console.log('has pixel code: DG  Mapping :', dg.pixelTrackingCode.trackingCode)
          if (!trackingArray.includes(dg.pixelTrackingCode.trackingCode)) {
            trackingArray.push(dg.pixelTrackingCode.trackingCode);
          }
        }
      });
    }
    if (trackingArray.length > 0) {
      setTrackingCode(trackingArray);
    }
  }, []);

  useEffect(function () {
    if (trackingCode && trackingCode.length > 0) {
      setNeedTracking(true);
    }
  }, [trackingCode]);

  var triggerOffset = 20;
  return React__default.createElement(
    VisibilitySensor,
    {
      partialVisibility: true,
      offset: { bottom: triggerOffset, top: containerHeight && containerHeight },
      onChange: function onChange(isVisible) {

        if (isVisible && !inView) {
          setInView(true);
        } else if (infiniteScroll) {
          if (inView) setInView(false);
        }
      } },
    React__default.createElement(
      'div',
      { style: { height: '100%', position: 'absolute', width: '1px', opacity: 0 } },
      needTracking && inView && trackingCode && trackingCode.map(function (code, index) {
        if (window.LassoImpressionID && code && checkIfLasso(code)) {
          var delimiter = code.lastIndexOf('?') === -1 ? '?' : '&';
          code += delimiter + 'LassoImpressionID=' + window.LassoImpressionID;
        }
        return React__default.createElement('span', { key: code, id: 'pixel-' + (index + 1), dangerouslySetInnerHTML: { __html: code } });
      })
    )
  );
});

export { PixelTrackingArticle, PixelTrackingLandingPages };
