import { useMemberStore } from '@/stores'
import type { loginResult } from '@/types/member'
import { request } from '@/utils/request'

type loginParams = {
  code: string
  login_code: string
}

export const testLoginApi = (phone: string) => {
  return request<loginResult>({
    method: 'POST',
    url: 'shop/login/testLogin',
    data: {
      phone,
    },
  })
}

/**
 * 获取登录用户session
 * @param login_code
 * @returns
 */
export const getSessionByLoginCodeApi = (login_code: string) => {
  return request<{
    openid: string
    unionid: string
  }>({
    method: 'GET',
    url: 'shop/login/getSessionByLoginCode',
    data: {
      login_code,
    },
  })
}

/**
 * 微信登录
 */
export const miniAppLoginApi = (data: loginParams) => {
  return request<loginResult>({
    method: 'POST',
    url: 'shop/login/miniAppLogin',
    data,
  })
}

export const checkLoginIegalityApi = (login_code: string) => {
  return request<{
    login_by_phone: boolean
  }>({
    method: 'POST',
    url: 'shop/login/checkLoginIegality',
    data: {
      login_code,
    },
  })
}

export const quickLoginApi = (login_code: string) => {
  return request<loginResult>(
    {
      method: 'POST',
      url: 'shop/login/quickLogin',
      data: {
        login_code,
      },
    },
    false,
    false,
  )
}

export const logout = () => {
  return request({
    method: 'POST',
    url: 'shop/login/logout',
    data: {
      refresh_token: useMemberStore().profile?.refresh_token,
    },
  })
}

export const loginByPhone = (data: { phone: string; sms_captcha_code: string }) => {
  return request<loginResult>({
    method: 'POST',
    url: 'shop/login/phone',
    data,
  })
}

export const loginByPassword = (data: { username: string; password: string }) => {
  return request<loginResult>({
    method: 'POST',
    url: '/login/password',
    data,
  })
}

export const registerApi = (data: {
  username: string
  password: string
  phone: string
  sms_captcha_code: string
}) => {
  return request<loginResult>({
    method: 'POST',
    url: 'shop/login/register',
    data,
  })
}

export const bindWxInfo = (data: {
  // 小程序登录code
  login_code: string
}) => {
  return request({
    method: 'POST',
    url: 'shop/login/bind',
    data,
  })
}
