import { defineStore } from 'pinia'
import { ref } from 'vue'
import wikiApi from '../api/wiki.api'
import { isNew } from '../lib/softwareUtils'
import { useLoadable } from '../composables/useLoadable'
import type { WikiPage } from '../lib/interfaces/wiki.interface'

export const useHowtosStore = defineStore('howtos', () => {
  const pages = ref<WikiPage[]>([])
  const { loading, error, withCache, invalidate } = useLoadable()

  async function fetch() {
    await withCache(async () => {
      pages.value = await wikiApi.listHowtoPages()
    })
  }

  return { pages, loading, error, fetch, isNew, invalidate }
})
