"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.COOKIE_MAX_AGE = exports.COOKIE_KEY = exports.COOKIE_DOMAIN = void 0;
exports.getCookieDomain = getCookieDomain;
exports.getPrivacySettings = getPrivacySettings;
exports.setPrivacySettings = setPrivacySettings;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var COOKIE_KEY = 'OPTOUTMULTI';
exports.COOKIE_KEY = COOKIE_KEY;
var COOKIE_MAX_AGE = 365 * 3; // 3 years
exports.COOKIE_MAX_AGE = COOKIE_MAX_AGE;
var COOKIE_DOMAIN = getCookieDomain();

/**
 * Returns a string for the cookie's domain that will work for all subdomains
 * of the current domain (e.g. "healthcare.gov") unless we're not on a .gov
 * site right now.
 */
exports.COOKIE_DOMAIN = COOKIE_DOMAIN;
function getCookieDomain() {
  if (typeof window === 'undefined') {
    return;
  }
  var parts = window.location.hostname.split('.');
  // Starting from the end and working back means that this logic will now support
  // any number of subdomains in the hostname (including zero).
  var tld = parts[parts.length - 1]; // Last item
  var domain = parts[parts.length - 2]; // Second to last item

  if (domain && tld === 'gov') {
    // From MDN:
    //   Contrary to earlier specifications, leading dots in domain names are ignored,
    //   but browsers may decline to set the cookie containing such dots.
    return "".concat(domain, ".").concat(tld);
  }
}
function readCookie(name) {
  if (typeof document === 'undefined') {
    return;
  }
  var item = document.cookie.split(';').map(function (item) {
    return item.trim().split('=');
  }).find(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 1),
      itemName = _ref2[0];
    return itemName === name;
  });
  var value = item === null || item === void 0 ? void 0 : item[1];
  if (typeof value === 'string') {
    return decodeURI(value);
  }
}
function writeCookie(name, value) {
  if (typeof document === 'undefined') {
    return;
  }

  // See https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
  var base = "".concat(name, "=").concat(encodeURI(value));
  var age = "; max-age=".concat(COOKIE_MAX_AGE);
  var domain = COOKIE_DOMAIN ? "; domain=".concat(COOKIE_DOMAIN) : '';
  var path = '; path=/'; // Should apply to any path on the site

  document.cookie = "".concat(base).concat(age).concat(domain).concat(path);
}
function getPrivacySettings() {
  var cookieString = readCookie(COOKIE_KEY);
  if (!cookieString && COOKIE_DOMAIN) {
    throw new Error("Privacy settings error: ".concat(COOKIE_KEY, " is not set. Check to make sure your app has Tealium enabled."));
  }
  var pairs = cookieString.split('|');
  var settings = pairs.reduce(function (obj, pair) {
    var _pair$split = pair.split(':'),
      _pair$split2 = _slicedToArray(_pair$split, 2),
      key = _pair$split2[0],
      value = _pair$split2[1];
    obj[key] = value;
    return obj;
  }, {});
  return settings;
}
function setPrivacySettings(settings) {
  var cookieString = Object.keys(settings).map(function (key) {
    return "".concat(key, ":").concat(settings[key]);
  }).join('|');
  writeCookie(COOKIE_KEY, cookieString);
}

// Set a default if we're not on a .gov environment
if (!COOKIE_DOMAIN && !readCookie(COOKIE_KEY)) {
  setPrivacySettings({
    0: '0',
    c3: '0',
    c2: '0',
    c1: '0',
    c4: '0'
  });
}