import router from '@/router'
import { sessionStorage } from '@/plugins/storage'
import useFetch from '@/hooks/useFetch'

/** 清理评卷任务 */
export const clearTasks = () => {
  const loginResult = sessionStorage.get('LOGIN_RESULT')
  if (loginResult?.role === 'MARKER') {
    useFetch('clearCachedTasks').fetch()
  }
}

/** loginOutFetch */
export const userLogout = () => {
  const loginResult = sessionStorage.get('LOGIN_RESULT')
  if (loginResult) {
    useFetch('userLogout').fetch()
  }
}

/** 退出登录 */
export const logout = async (clear = true) => {
  if (clear) {
    clearTasks()
  }
  userLogout()
  Promise.resolve().then(() => {
    sessionStorage.remove('LOGIN_RESULT')
    router.replace({ name: 'Login' })
  })
}

/** 关闭程序 */
export const closeApp = () => {
  window.close()
}
