'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

/**
 * 获取cookie
 * @param {string} key cookie键值
 * @returns {string} cookie值
 *
 * @example
 *
 * const res = getCookie('name')
 *
 * // => mike
 *
 */
function getCookie(key) {
  var cookie = '';
  if (typeof window !== 'undefined' && window.document) {
    cookie = window.document.cookie;
  }
  var result = '';
  var re = new RegExp("".concat(key, "=(.*?)(;|$)")); // 浏览器端，两个母子域名同名cookie会只取第一个值
  var ma = cookie.match(re);
  result = (ma === null || ma === void 0 ? void 0 : ma[1]) || '';
  return result;
}
/**
 * 设置cookie
 * @param {string} key cookie键值
 * @param {string} value cookie值
 * @param {number} [hours] 过期时间，单位小时
 *
 * @example
 *
 * setCookie('name', 'mike')
 */
function setCookie(name, value) {
  var Hours = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  var date = new Date(); // 获取当前时间
  var expiresDays = Hours; // 将date设置为n天以后的时间
  date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000); // 格式化为cookie识别的时间
  document.cookie = "".concat(name, "=").concat(value, ";expires=").concat(date.toUTCString()); // 设置cookie
}
/**
 * 清除cookie
 * @param {string} key cookie键
 *
 * @example
 *
 * clearCookie('name');
 *
 */
function clearCookie(name) {
  setCookie(name, '', -1);
}
/**
 * 清除全部cookie
 *
 * @param {string} domain 域名
 *
 * @example
 *
 * clearAll()
 */
function clearAll() {
  var domain = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var cookies = document.cookie.split(';');
  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf('=');
    var name = eqPos > -1 ? cookie.slice(0, eqPos) : cookie;
    document.cookie = "".concat(name, "=;expires=Thu, 01 Jan 1970 00:00:00 GMT; Domain=.").concat(domain, "; path=/");
  }
}

exports.clearAll = clearAll;
exports.clearCookie = clearCookie;
exports.getCookie = getCookie;
exports.setCookie = setCookie;
