import { defHttp } from '/@/utils/http/axios';
import {
  LoginParams,
  LoginResultModel,
  GetUserInfoModel,
  UserInfoModel,
  RefreshTokenParams,
  SmsLoginParams,
} from './model/userModel';

import type { ErrorMessageMode } from '/#/axios';
import { getAppEnvConfig } from '/@/utils/env';

enum Api {
  Login = '/auth/login',
  kidLogin = '/singleAuth/getCustomToken',
  SmsCodeLogin = '/auth/code/login',
  Logout = '/auth/logout', 
  GetUserInfo = '/user',
  GetMyInfo = '/user/me/info',
  GetPermCode = '/role/me/permissions',
  RefreshToken = '/auth/token',
  SendLoginSmsCode = '/noauth/send_login_code/',
  ResetCode = '/noauth/reset_code/',
  ResetPassword = '/noauth/reset/',
}

/**
 * @description: user login api
 */
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.Login,
      params,
    },
    {
      errorMessageMode: mode,
      joinPrefix: false,
    }
  );
}
/**
 * @description: 有response_type后的code请求
 */
export function kidloginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.kidLogin,
      params,
    },
    {
      errorMessageMode: mode,
      joinPrefix: false,
    }
  );
}
export function getMyInfo() {
  return defHttp.get<UserInfoModel>({ url: Api.GetMyInfo });
}
/**
 * @description: getUserInfo
 */
export function getUserInfo() {
  return defHttp.get<GetUserInfoModel>({ url: Api.GetUserInfo });
}

export function getPermCode() {
  return defHttp.get<string[]>({ url: Api.GetPermCode });
}
export async function SendLoginSmsCode(phoneNumber: string) {
  return await defHttp.post<boolean>({ url: Api.SendLoginSmsCode + phoneNumber });
}
 
export async function doLogout( ) {
  await defHttp.post(
    {
      url: Api.Logout,
    },
    {
      joinPrefix: false,
    }
  );
}
export function doRefreshToken(params: RefreshTokenParams) {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.RefreshToken,
      params,
    },
    {
      joinPrefix: false,
    }
  );
}
export function smsCodeLoginApi(params: SmsLoginParams, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<LoginResultModel>(
    {
      url: Api.SmsCodeLogin,
      params,
    },
    {
      errorMessageMode: mode,
    }
  );
}

// 密码重置验证码
export function passwordResetCode(mobile) {
  return defHttp.post({
    url: Api.ResetCode + mobile,
  });
}

// 重置密码
export function forgetPasswordApi(data) {
  return defHttp.post({
    url: Api.ResetPassword + data.phoneNumber,
    data,
  });
}

export const getUserToken = (id: string) => {
  return defHttp.get<Record<'token' | 'refreshToken', string>>({
    url: `/third/login/id/${id}`,
  });
};

export async function LoginOut() {
  return new Promise(function (resolve, reject) {
    const { VITE_GLOB_IS_IDC } = getAppEnvConfig();
    if (VITE_GLOB_IS_IDC && VITE_GLOB_IS_IDC.length) {
      var iframe = document.createElement('iframe');
      iframe.src = VITE_GLOB_IS_IDC;
      iframe.style.display = 'none';
      iframe.style.position = 'fixed';
      iframe.style.zIndex = '-9';
      document.body.appendChild(iframe);
      setTimeout(() => window.location.href = VITE_GLOB_IS_IDC, 3000);
      resolve(true)
    }
  })
}
