import Cookies from 'js-cookie';

const cookieKeys = ['admin_uuid', 'admin_uid'];

/**
 * 判断是否需要储存为Cookie
 * @param key
 * @returns {boolean}
 */
function isCookieKey(key) {
  return cookieKeys.indexOf(key) !== -1;
}

/**
 * SetItem
 * @param key
 * @param value
 * @param options
 */
function setItem(key, value, options = {}) {
  if (options.expires === undefined) {
    options.expires = 365 * 10;
  }
  return isCookieKey(key)
    ? setCookieItem(key, value, options)
    : setLocalItem(key, value, options);
}

/**
 * GetItem
 * @param key
 * @returns {*|string}
 */
function getItem(key) {
  return isCookieKey(key)
    ? getCookieItem(key)
    : getLocalItem(key);
}

/**
 * RemoveItem
 * @param key
 * @param options
 */
function removeItem(key, options) {
  return isCookieKey(key)
    ? removeCookieItem(key, options)
    : removeLocalItem(key);
}

// ========== LocalStorage ========== //

/**
 * 设置LocalStorageItem
 * @param key
 * @param value
 * @param options
 */
function setLocalItem(key, value, options = {}) {
  let { expires } = options;
  if (typeof expires === 'number') {
    expires = new Date().getTime() + expires * 86400 * 1000;
  } else if (typeof expires === 'object') {
    expires = expires.getTime();
  }
  value = JSON.stringify({
    expires,
    data: value
  });
  localStorage.setItem(key, value);
}

/**
 * 获取LocalStorageItem
 * @param key
 * @returns {string}
 */
function getLocalItem(key) {
  let object = localStorage.getItem(key);
  if (!object) return '';
  object = JSON.parse(object);
  const { expires } = object;
  if (typeof expires === 'number' && expires <= Date.now()) {
    localStorage.removeItem(key);
    return '';
  }
  return object.data;
}

/**
 * 移除LocalStorageItem
 * @param key
 */
function removeLocalItem(key) {
  localStorage.removeItem(key);
}

// ========== Cookie ========== //

/**
 * 获取根域名
 * @returns {string}
 */
function getRootDomain() {
  const parts = document.domain.split('.').reverse();
  if (parts.length >= 2) {
    return parts[1] + '.' + parts[0];
  }
  return document.domain;
}

/**
 * 设置CookieItem
 * @param key
 * @param value
 * @param options
 */
function setCookieItem(key, value, options = {}) {
  const domain = getRootDomain();
  options = { domain, ...options };
  Cookies.set(key, value, options);
}

/**
 * 获取CookieItem
 * @param key
 * @returns {*}
 */
function getCookieItem(key) {
  return Cookies.get(key);
}

/**
 * 移除CookieItem
 * @param key
 * @param options
 */
function removeCookieItem(key, options = {}) {
  const domain = getRootDomain();
  options = { domain, ...options };
  Cookies.remove(key, options);
}

export default {
  setItem,
  getItem,
  removeItem
};