import { defineStore } from 'pinia'
import { ref } from 'vue'
import buildsApi from '../api/builds.api'
import { useLoadable } from '../composables/useLoadable'
import type { BuildsData } from '../lib/interfaces/builds.interface'

export const useBuildsStore = defineStore('builds', () => {
  const data = ref<BuildsData | null>(null)
  const { loading, error, withCache, invalidate } = useLoadable()

  async function fetch() {
    await withCache(async () => {
      data.value = await buildsApi.index()
    })
  }

  return { data, loading, error, fetch, invalidate }
})
