import { logout as nostrLogout } from 'nostr-login'
import { clear } from '../utils/localStorage'
import { useDispatch } from 'react-redux'
import { Dispatch } from '../store/store'
import { userLogOutAction } from '../store/actions'
import { LoginMethod } from '../store/auth/types'
import { useAppSelector } from './store'

export const useLogout = () => {
  const loginMethod = useAppSelector((state) => state.auth?.loginMethod)
  const dispatch: Dispatch = useDispatch()

  const logout = () => {
    // Log out of the nostr-login
    if (loginMethod === LoginMethod.nostrLogin) {
      nostrLogout()
    }

    // Reset redux state with the logout
    dispatch(userLogOutAction())

    // Clear the local storage states
    clear()
  }

  return logout
}
