"use strict";
import axios from "axios";
import {ElMessage,ElMessageBox} from 'element-plus'
import CryptoJS from './crypto'
import router from '@/router/index'
import { isMobile,copyText } from "../utils/common";

// 复制邮箱
const copyEmail=()=>{
  copyText('stephanie@hzinsights.com').then(res=>{
    ElMessage.success('Email address copied')
  }).catch(()=>{})
}

// 检查返回的code
const checkResCode=(code,msg)=>{
  switch (code) {
    case 400 || 500:
      ElMessage.error(msg)
      break;
    case 401:
      // 非法访问
      ElMessage({
        type:'error',
        message:msg,
        duration:1000
      })
      setTimeout(()=>{
        // 清除token，重新登录
        localStorage.removeItem('yben_token')
        localStorage.removeItem('user_info')
        router.replace('/login')
      },1000)
      break;
    case 4013:
      // 未注册
      ElMessageBox.confirm("This email adress is not registered. Please try again.","Prompt",
      {
        customClass:isMobile()?'mobile-message-confirm':'PC-message-confirm',
        confirmButtonClass:isMobile()?'mobile-confirm-button':'',
        confirmButtonText: 'Create account',
        showCancelButton:false,
      }).then(res=>{
        router.push('/register')
      }).catch(()=>{})
      break;
    case 4014:
      // 未绑定
      ElMessageBox.confirm("This phone number is not registered. Please try again.","Prompt",
      {
        customClass:isMobile()?'mobile-message-confirm':'PC-message-confirm',
        confirmButtonClass:isMobile()?'mobile-confirm-button':'',
        confirmButtonText: 'Got it',
        showCancelButton:false,
      }).then(res=>{
        console.log('This phone number is not registered. Please try again.');
      }).catch(()=>{})
      break;
    case 4012:
      // 权限到期
      localStorage.removeItem('user_info')
      localStorage.removeItem('yben_token')
      ElMessageBox.confirm("Your trial has ended Enjoyed your experience with us? Contact us at stephanie@hzinsights.com to extend your trial.","Prompt",
      {
        customClass:isMobile()?'mobile-message-confirm':'PC-message-confirm',
        confirmButtonClass:isMobile()?'mobile-confirm-button':'',
        confirmButtonText: 'Get the contact email',
        showCancelButton:false,
      }).then(res=>{
        copyEmail()
      }).catch(()=>{})
      break;
    case 4015:
      // 已注册
      ElMessageBox.confirm("There is already a user account associated with this email address. Please log in instead.","Prompt",
      {
        customClass:isMobile()?'mobile-message-confirm':'PC-message-confirm',
        confirmButtonClass:isMobile()?'mobile-confirm-button':'',
        confirmButtonText: 'Log in',
        showCancelButton:false,
      }).then(res=>{
        router.push('/login')
      }).catch(()=>{})
      break;
  }
}

let config = {
  baseURL: import.meta.env.VITE_APP_API_URL,
  timeout: 60 * 1000, // Timeout
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function (config) {
    config.headers.shareEmailId=sessionStorage.getItem('shareId')||0
    config.headers.Authorization=localStorage.getItem('yben_token')||''
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function (response) {
    let data
    if(import.meta.env.MODE==='production'){
      data=JSON.parse(CryptoJS.Des3Decrypt(response.data));//解密
    }else{
      data=response.data
    }

    checkResCode(data.code,data.msg)

    return data;
  },
  function (error) {
    // Do something with response error
    return Promise.reject(error);
  }
);

/**
 * 导出get请求方法
 * @url 请求地址
 * @params get请求参数
 */
export const get = (url, params) => {
  return _axios.get(url, { params })
};

/**
 * 导出post请求方法
 * @url 请求地址
 * @params post请求参数
 */
export const post = (url,params) => {
  return _axios.post(url, params)
};
