import { ref } from 'vue'
// import { useUserStore } from '@/stores/modules/user'

interface LoginResult {
  status: boolean
  error?: string
  isFirstLogin?: boolean
}
const isLoginAndDownloadOpen = ref(false)
let resolvePromise: ((value: LoginResult) => void) | null = null

export function useLoginAndDownLoadModal() {
  const openLoginAndDownloadModal = (): Promise<LoginResult> => {
    // const userStore = useUserStore()
    // const { isLogin } = storeToRefs(userStore)
    // if (isLogin.value)
    //   return Promise.resolve({ status: true, isFirstLogin: false })
    isLoginAndDownloadOpen.value = true
    return new Promise((resolve) => {
      resolvePromise = resolve
    })
  }

  const closeLoginAndDownloadModal = (result: LoginResult) => {
    isLoginAndDownloadOpen.value = false
    if (resolvePromise) {
      resolvePromise(result)
      resolvePromise = null
    }
  }

  return {
    isLoginAndDownloadOpen,
    openLoginAndDownloadModal,
    closeLoginAndDownloadModal,
  }
}
