import { createContext, type PropsWithChildren, useContext, useMemo, useState } from 'react'

// 控制loginForm显示的登录类型
export enum LoginStateEnum {
  LOGIN = 0,
  REGISTER = 1,
  RESET_PASSWORD = 2,
  MOBILE = 3,
  QR_CODE = 4
}

interface LoginStateContextType {
  loginState: LoginStateEnum
  setLoginState: (loginState: LoginStateEnum) => void
  backToLogin: () => void
}
const LoginStateContext = createContext<LoginStateContextType>({
  loginState: LoginStateEnum.LOGIN,
  setLoginState: () => {},
  backToLogin: () => {}
})

export function useLoginStateContext() {
  const context = useContext(LoginStateContext)
  return context
}

export function LoginStateProvider({ children }: PropsWithChildren) {
  const [loginState, setLoginState] = useState(LoginStateEnum.LOGIN)

  function backToLogin() {
    setLoginState(LoginStateEnum.LOGIN)
  }

  // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
  const value: LoginStateContextType = useMemo(
    () => ({ loginState, setLoginState, backToLogin }),
    [loginState]
  )

  return <LoginStateContext.Provider value={value}>{children}</LoginStateContext.Provider>
}
