Source: sync/site/session/referring-site.js

'use strict';

/** Set `reffering_site` cookie (filter certain domains)
 * @module referringSite
 */

// FE-2453: This line should not be necessary
export const n = DEBUG ? 'ReferringSite' : undefined; // eslint-disable-line no-undef, no-undefined, id-length

// FE-2673: The `locationTools` is not used, do we want to use it (add method to it)?
//          Mike Doug may know; he is the original author of this file and that file.
// import {locationTools} from '../../utility/location-tools';
import {domains}  from '../../../utility/domains';

//
// Definitions
//

/** List of domains that are ignored when determining referring site
 * @type {array.<string>}
 */
const ignoredDomains = [
  'paypal.com',
  'jobvite.com',
  'gcsip.nl',      // Ingenico ePayments Staging
  'gcsip.com',     // Ingenico ePayments Production
  'alipaydev.com', // Alipay Staging
  'alipay.com',    // Alipay Production
];

//
// Exports
//

/**
 * Logic for needing to inject the cookie here for testability
 *
 * @param {Document.referrer} referrer - The referring site
 * @param {module:domains.Domains} _domains - An instance of {@link module:domains.Domains}
 * @return {boolean}
 */
export function _needsCookie(referrer, {_domains = domains} = {}) {
  // FE-2673: Avoid URL regex;
  //          use [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL).
  //          When lines with `m` are less obtuse,
  //          remove `// eslint-disable-line …` on the lines.
  const m = (referrer || '').match(/^[a-z]+:\/\/([^\/:]*)/i); // eslint-disable-line no-useless-escape, id-length
  DEBUG && console.log(m); /* FE-2856 */// eslint-disable-line
  const referrerHost = (m !== null) ? m[1] : ''; // eslint-disable-line no-negated-condition
  DEBUG && console.log(referrerHost); /* FE-2856 */// eslint-disable-line
  const referrerDomain = referrerHost.split('.').splice(-2).join('.');
  DEBUG && console.log(referrerDomain); /* FE-2856 */// eslint-disable-line

  // FE-2673: What is the significance of `hasReferrerHost`?
  //          Name the variable after that significance, not its logic,
  //          because we want to know why we care about the value.
  const hasReferrerHost = (referrerHost !== '');
  // FE-2673: Use `includes`, instead of `indexOf`.
  //          - Add `!String.includes` to list in `…/templates-hidden/_site.js.html`.
  const isRelevantReferrer = (ignoredDomains.indexOf(referrerDomain) === -1);
  const isThirdPartyReferrer = !_domains.isOurDomain(referrerDomain);

  return (hasReferrerHost && isRelevantReferrer && isThirdPartyReferrer);
}
/* eslint-enable */

/** If our referrer is not us and not one of the above ignored domains, set the `referring_site` cookie.
 *
 * Expiration is unset (expires when browser is closed)
 * because this was a "session" based cookie originally in Scala.
 */
export default function referringSite() {
  DEBUG && console.log('ReferringSite'); /* FE-2856 */// eslint-disable-line
  // This is for testing hash code
  DEBUG && domains.isOurDomain(window.location.hostname); // eslint-disable-line
  if (_needsCookie(document.referrer)) {
    DEBUG && console.log('Setting `referring_site` cookie to : ' + document.referrer); /* FE-2856 */// eslint-disable-line
    window.jsCookies.set('referring_site', document.referrer);
  }
}