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

export const useLogout = () => {
  const loginMethod = useAppSelector((state) => state.auth?.loginMethod)
  const dispatch = useAppDispatch()

  const logout = useCallback(() => {
    // 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()
  }, [dispatch, loginMethod])

  return logout
}
