import { defineStore } from 'pinia';
import { ref } from 'vue';
import { tasksApi } from '@/api/tasks';
export const useTasksStore = defineStore('tasks', () => {
    const tasks = ref([]);
    const loading = ref(false);
    async function fetchAll(params) {
        loading.value = true;
        tasks.value = await tasksApi.list(params);
        loading.value = false;
    }
    async function create(t) {
        const created = await tasksApi.create(t);
        tasks.value.push(created);
        return created;
    }
    async function updateStatut(id, statut) {
        const t = tasks.value.find(t => t.id === id);
        if (!t)
            return;
        const updated = await tasksApi.update(id, { ...t, statut });
        Object.assign(t, updated);
    }
    async function remove(id) {
        await tasksApi.delete(id);
        tasks.value = tasks.value.filter(t => t.id !== id);
    }
    return { tasks, loading, fetchAll, create, updateStatut, remove };
});
