import { defineStore } from 'pinia';
import { ref } from 'vue';
import { plantsApi } from '@/api/plants';
export const usePlantsStore = defineStore('plants', () => {
    const plants = ref([]);
    const loading = ref(false);
    async function fetchAll(categorie) {
        loading.value = true;
        try {
            plants.value = await plantsApi.list(categorie);
        }
        finally {
            loading.value = false;
        }
    }
    async function create(p) {
        const created = await plantsApi.create(p);
        plants.value.push(created);
        return created;
    }
    async function remove(id) {
        await plantsApi.delete(id);
        plants.value = plants.value.filter(p => p.id !== id);
    }
    return { plants, loading, fetchAll, create, remove };
});
