import Cookie from 'js-cookie'
import SystemConfig from '@/utils/SystemConfig'

// 认证类型
const AUTH_TYPE = {
  BASIC: 'basic',
};

/**
 * 移出认证信息
 * @param authType {AUTH_TYPE} 认证类型
 */
function removeAuthorization (authType = AUTH_TYPE.BASIC) {
  switch (authType) {
    case AUTH_TYPE.BASIC:
      Cookie.remove(SystemConfig.token);
      break;
    default:
      break
  }
}

/**
 * 检查认证信息
 * @param authType
 * @returns {boolean}
 */
function checkAuthorization (authType = AUTH_TYPE.BASIC) {
  switch (authType) {
    case AUTH_TYPE.BASIC:
      if (Cookie.get(xsrfHeaderName)) {
        return true
      }
      break;
    case AUTH_TYPE.BASIC:
      if (Cookie.get(SystemConfig.token)) {
        return true
      }
      break;
    default:
      break
  }
  return false
}

/**
 * 设置认证信息
 * @param auth {Object}
 * @param authType {AUTH_TYPE} 认证类型，默认：{AUTH_TYPE.BEARER}
 */
function setAuthorization (auth, authType = AUTH_TYPE.BASIC) {
  switch (authType) {
    case AUTH_TYPE.BASIC:
    case AUTH_TYPE.BASIC:
      Cookie.set("__vt_param__", auth[SystemConfig.token], { expires: auth['expireAt'] });
      break;
    default:
      break
  }
}

function getToken () {
  return Cookie.get(process.env.VUE_APP_TOKEN_KEY);
}

export {
  AUTH_TYPE,
  setAuthorization,
  removeAuthorization,
  checkAuthorization,
  getToken
}
