Source: internal/vendor/@carbon/ibmdotcom-utilities/utilities/ipcinfoCookie/ipcinfoCookie.js

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * Copyright IBM Corp. 2020, 2023
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import Cookies from 'js-cookie';

/**
 * Name of cookie needed to grab cc and lc
 *
 * @type {string}
 * @private
 */
var _cookieName = 'ipcInfo';

/**
 * Utility to set and get the ipcInfo cookie needed to determine country and language code
 */
var ipcinfoCookie = /*#__PURE__*/function () {
  function ipcinfoCookie() {
    _classCallCheck(this, ipcinfoCookie);
  }
  return _createClass(ipcinfoCookie, null, [{
    key: "get",
    value:
    /**
     * retreive the ipcInfo cookie that contains the cc and lc
     * decodes and converts to object
     *
     * @example
     * import { ipcinfoCookie } from '@carbon/ibmdotcom-utilities';
     *
     * const info = ipcinfoCookie.get();
     */
    function get() {
      var cookiesDecode = Cookies.withConverter({
        read: function read(value) {
          return decodeURIComponent(value);
        }
      });
      var ipcinfo = cookiesDecode.get(_cookieName);
      if (ipcinfo) {
        var cc;
        var lc;
        var info = ipcinfo.split(';');
        info.map(function (code) {
          var itemParts = code.split('=');
          if (itemParts[0] === 'cc') {
            cc = itemParts[1];
          }
          if (itemParts[0] === 'lc') {
            lc = itemParts[1];
          }
        });
        return {
          cc: cc,
          lc: lc
        };
      }
    }

    /**
     * set the ipcInfo cookie with expiration of a year
     * takes care of converting to string and encoding
     *
     * @param {object} params params object
     * @param {string} params.cc country code
     * @param {string} params.lc language code
     * @example
     * import { ipcinfoCookie } from '@carbon/ibmdotcom-utilities';
     *
     * const locale = {cc: 'us', lc: 'en'}
     * ipcinfoCookie.set(locale);
     */
  }, {
    key: "set",
    value: function set(_ref) {
      var cc = _ref.cc,
        lc = _ref.lc;
      var info = "cc=".concat(cc, ";lc=").concat(lc);
      var cookiesEncode = Cookies.withConverter({
        write: function write(value) {
          return encodeURIComponent(value);
        }
      });
      cookiesEncode.set(_cookieName, info, {
        expires: 365,
        domain: '.ibm.com'
      });
    }
  }]);
}();
export default ipcinfoCookie;