import type { Update } from '@tauri-apps/plugin-updater'
import { check } from '@tauri-apps/plugin-updater'
import { createGlobalState } from '@vueuse/core'

export const useUpdater = createGlobalState(() => {
  const lastUpdate = shallowRef<Update>()

  const checking = ref(false)

  async function checkForUpdates() {
    try {
      checking.value = true

      lastUpdate.value = (await check()) ?? undefined
    }
    finally {
      checking.value = false
    }

    return lastUpdate.value
  }

  return {
    lastUpdate,
    checking,
    checkForUpdates,
  }
})
