"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getOrCreateUid = exports.createUid = exports.deleteUid = exports.getUid = void 0;

var _milliseconds = _interopRequireDefault(require("milliseconds"));

var _cookie = require("./cookie");

var _uuid = require("./uuid");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// @ts-check
const domain = globalThis.location && globalThis.location.hostname.split('.').slice(-2).join('.') || 'xchart.com';

const getUid = () => (0, _cookie.readCookie)('uid');

exports.getUid = getUid;

const deleteUid = () => (0, _cookie.deleteCookie)('uid', domain);

exports.deleteUid = deleteUid;

const createUid = () => {
  const uid = (0, _uuid.uuid)();

  try {
    (0, _cookie.writeCookie)('uid', uid, {
      maxAge: _milliseconds.default.years(10),
      domain
    });
  } catch (e) {// intentionally not used
  }

  return uid;
};

exports.createUid = createUid;

const getOrCreateUid = () => getUid() || createUid();

exports.getOrCreateUid = getOrCreateUid;