import type { ServerList, ServerRes } from '~/service/index'
import httpRequest from "~/service/index";
import { type LoginRequest, type extendRegisterRules, type ForgetPasswordReq } from "~/domain/Login";
import type {
  SignupRequest, updatePassRequest
} from "~/domain/Register";
import type sendVerifyCodeType from "./loginQuery";

export function useLogin() {
  const apiRoot = "/seller/system"
  async function login(req: LoginRequest) {
    return await httpRequest.get(`/user/userLogin/passwordLogin`, req)
  }
  async function codeLogin(req: LoginRequest) {
    return await httpRequest.get(`/user/userLogin/phoneCodeLogin`, req)
  }
  async function getCode(falg: boolean) {
    return await httpRequest.get<ServerRes<any>>(`/captchaImage?force=${falg}`)
  }

  async function signup(req: SignupRequest) {
    return await httpRequest.post(`/user/userLogin/registerUser`, req)
  }

  async function updatePassword(req: updatePassRequest) {
    return await httpRequest.post(`${apiRoot}/changePassword`, req)
  }
  async function userLogout(req: any) {
    return await httpRequest.post(`/logout`, req)
  }

  async function getBaseProfile(req: any) {
    return await httpRequest.post(`${apiRoot}/profile`, req)
  }

  async function sendVerifyCode(req: sendVerifyCodeType) {
    return await httpRequest.get(`/aliyun/sms/sendSMS`, req)
  }

  //邀请码注册设置cookie
  async function inviteRegister(code: string) {
    return await httpRequest.post(`/open/campaign/invite/register?code=${code}`, {})
  }

  async function loginClub(ua: string) {
    return await httpRequest.post(`${apiRoot}/loginClub`, { ua: ua })
  }

  //验证手机号是否已注册
  async function checkPhone(username: string) {
    return await httpRequest.post(`${apiRoot}/exist`, { username: username })
  }
  return {
    login,
    getCode,
    signup,
    updatePassword,
    userLogout,
    getBaseProfile,
    sendVerifyCode,
    inviteRegister,
    loginClub,
    checkPhone,codeLogin
  }
}
