'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var slicedToArray = require('./slicedToArray-067dd3bc.js');
var React = require('react');
var Router = require('next/router');
var visibilitySensor = require('./visibility-sensor-56020df6.js');
require('./_commonjsHelpers-06173234.js');
require('./web.dom.iterable-d98303e0.js');
require('./core.get-iterator-method-5643aa10.js');
require('react-dom');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);

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

var PixelTrackingLandingPages = React__default["default"].memo(function (_ref) {
  var pixelTracking = _ref.pixelTracking;

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

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

  if (!pixelTracking) return null;

  var router = Router.useRouter();

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

  React.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["default"].createElement('div', { style: { height: '0', opacity: 0 }, key: code, dangerouslySetInnerHTML: { __html: code } });
  };

  return React__default["default"].createElement(
    React__default["default"].Fragment,
    null,
    needTracking && renderCode(trackingCode)
  );
});

// Article page tracking

var PixelTrackingArticle = React__default["default"].memo(function (_ref2) {
  var article = _ref2.article,
      infiniteScroll = _ref2.infiniteScroll;

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

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

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

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

  if (!article) return null;

  React.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);
          }
        }
      });
    }

    //publication issue
    if (article.issue && article.issue.pixelTrackingCode && article.issue.pixelTrackingArticleDetail) {
      if (article.issue.pixelTrackingCode.trackingCode && article.issue.pixelTrackingCode.is_active) {
        if (!trackingArray.includes(article.issue.pixelTrackingCode.trackingCode)) {
          trackingArray.push(article.issue.pixelTrackingCode.trackingCode);
        }
      }
    }

    if (trackingArray.length > 0) {
      setTrackingCode(trackingArray);
    }
  }, []);

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

  var triggerOffset = 20;
  return React__default["default"].createElement(
    visibilitySensor.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["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["default"].createElement('span', { key: code, id: 'pixel-' + (index + 1), dangerouslySetInnerHTML: { __html: code } });
      })
    )
  );
});

exports.PixelTrackingArticle = PixelTrackingArticle;
exports.PixelTrackingLandingPages = PixelTrackingLandingPages;
