import { _ as _extends } from './extends-6d8e3924.js';
import { _ as _toConsumableArray } from './toConsumableArray-f7074d7c.js';

var getContentPlacementUrl = function getContentPlacementUrl(cp) {
    var cp_url = '';
    if (cp.ancestor && typeof cp.ancestor == 'string') {
        cp_url += (cp_url === '' ? '' : '/') + cp.ancestor;
    }

    if (!cp.ancestor && cp.parent && typeof cp.parent == 'string') {
        cp_url += (cp_url === '' ? '' : '/') + cp.parent;
    }

    cp_url += (cp_url === '' ? '' : '/') + (cp.adTargetingURL ? adTargetingURL : cp.path);

    return cp_url;
};

var getTargeting = function getTargeting(props) {
  var positionOverride = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  var article = props.article,
      position = props.position,
      prevUrl = props.prevUrl,
      _props$cpModification = props.cpModificationRequired,
      cpModificationRequired = _props$cpModification === undefined ? true : _props$cpModification;


  if (positionOverride) {
    position = positionOverride;
  }

  var _article$internalTag = article.internalTag,
      internalTag = _article$internalTag === undefined ? [] : _article$internalTag,
      _article$userDefinedT = article.userDefinedTag,
      userDefinedTag = _article$userDefinedT === undefined ? [] : _article$userDefinedT,
      _article$SponsorshipT = article.SponsorshipTag,
      SponsorshipTag = _article$SponsorshipT === undefined ? [] : _article$SponsorshipT,
      _article$seoTag = article.seoTag,
      seoTag = _article$seoTag === undefined ? [] : _article$seoTag,
      _article$url = article.url;
  _article$url = _article$url === undefined ? {
    current: ''
  } : _article$url;
  var url = _article$url.current,
      _article$documentGrou = article.documentGroup;
  _article$documentGrou = _article$documentGrou === undefined ? {
    path: ''
  } : _article$documentGrou;
  var document_group = _article$documentGrou.path,
      document_groups = article.document_groups,
      content_placement = article.content_placement;

  var context = url;
  var localHistory = props.historyCookie ? JSON.parse(props.historyCookie) : [];
  if (!context && localHistory) {
    context = localHistory[localHistory.length - 1];
  }
  context = context && context.split('?')[0];
  context = context && context.split('&')[0];
  var content_placement_urls = [];
  if (content_placement && prevUrl && prevUrl.length) {
    content_placement.forEach(function (item) {
      var path = item.path;
      if (cpModificationRequired) {
        path = getContentPlacementUrl(item);
      }

      var previous_url_to_check = prevUrl;
      if (prevUrl.includes('?')) {
        previous_url_to_check = prevUrl.split('?')[0];
      }

      if (previous_url_to_check.endsWith(path)) {
        content_placement_urls.push(path);
      }
    });
  }

  if (content_placement) {
    if (content_placement_urls.length === 0) {
      content_placement_urls = content_placement.map(function (item) {
        return cpModificationRequired ? getContentPlacementUrl(item) : item.path;
      });
    }
  } else {
    content_placement_urls = ['/'];
  }

  var document_group_urls = [];
  if (document_groups && prevUrl && prevUrl.length) {
    document_groups.forEach(function (item) {
      var path = item.path;

      var previous_url_to_check = prevUrl;
      if (prevUrl.includes('?')) {
        previous_url_to_check = prevUrl.split('?')[0];
      }

      if (previous_url_to_check.endsWith(path)) {
        if (item.adTargetingURL) {
          path = item.adTargetingURL;
        }

        if (cpModificationRequired) {
          path = getContentPlacementUrl(item);
        }

        document_group_urls.push(path);
      }
    });
  }

  if (document_groups) {
    if (document_group_urls.length === 0) {
      document_group_urls = document_groups.map(function (item) {
        return cpModificationRequired ? getContentPlacementUrl(item) : item.adTargetingURL ? item.adTargetingURL : item.path;
      });
    }
  } else {
    var path = document_group;
    if (article.documentGroup && article.documentGroup.adTargetingURL) {
      path = article.documentGroup.adTargetingURL;
    }
    document_group_urls = [path];
  }

  // force empty content placement whene there is AD Targeting Group for an article
  if (document_group) {
    console.log('AD targeting group forced empty content placements.');
    content_placement_urls = [];
  }

  var articleKeywords = [].concat(_toConsumableArray(internalTag), _toConsumableArray(userDefinedTag), _toConsumableArray(SponsorshipTag), _toConsumableArray(seoTag));
  var targeting = {
    tags: articleKeywords,
    document_group: document_group_urls,
    document_url: [url],
    content_placement: content_placement_urls,
    rootDocumentGroup: props.rootDocumentGroup ? props.rootDocumentGroup : ''
  };

  if (article.issue && article.issue.path) {
    targeting.issue_url = [article.issue.path];
    if (article.issue.publication && article.issue.publication.path) {
      targeting.publication_url = [article.issue.publication.path];
    }
  }

  if (props.cache && props.cache.hyperTargeting) {
    targeting = _extends({}, targeting, { campaign_id: props.cache.hyperTargeting });
  }

  if (position && position.length) {
    targeting['pos'] = position;
  }
  return targeting;
};

export { getContentPlacementUrl as a, getTargeting as g };
