import { defHttp, tmallabHttp } from '/@/utils/http/axios';
import { LoginParams, LoginResultModel } from './model/userModel';
import { getAuthCache } from '/@/utils/auth';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';

import { ErrorMessageMode } from '/#/axios';

enum Api {
  Login = '/login/login',
  Logout = '/login/logout',
  GetUserInfo = '/login/detail',
  GetPermCode = '/getPermCode',
  tmallabLoginApi = '/system/login',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  // return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo }, { errorMessageMode: 'none' });
  return getAuthCache(USER_INFO_KEY) || {};
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
  return defHttp.post({ url: Api.Logout });
}

export function tmallabLoginApi(params: any) {
  return tmallabHttp.post({
    url: Api.tmallabLoginApi,
    params,
  });
}
