import { defineStore } from 'pinia'
import { ref } from 'vue'
import { categoriesService } from '@/services/categories-service'
import { Category } from '@/models/category'

export const useCategoriesStore = defineStore('categories', () => {
    const categories = ref<Category[]>([])
    const isLoading = ref(false)

    const fetchCategories = async (spaceId: number) => {
        isLoading.value = true
        try {
            categories.value = await categoriesService.fetchCategories(spaceId)
        } finally {
            isLoading.value = false
        }
    }

    const addCategory = (category: Category) => {
        categories.value.push(category)
    }

    return {
        categories,
        isLoading,
        fetchCategories,
        addCategory
    }
})