import { defineStore } from 'pinia'
import { ref } from 'vue'
import {RecurrentOperation} from "@/models/recurrent-operation";
import {recurrentsService} from "@/services/recurrents-service";

export const useRecurrentsStore = defineStore('recurrents', () => {
    const recurrents = ref<RecurrentOperation[]>([])
    const isLoading = ref(false)

    const fetchRecurrents = async (spaceId: number) => {
        isLoading.value = true
        try {
            recurrents.value = await recurrentsService.fetchRecurrents(spaceId)
        } finally {
            isLoading.value = false
        }
    }

    const addRecurrent = (recurrent: RecurrentOperation) => {
        recurrents.value.push(recurrent)
    }

    return {
        recurrents,
        isLoading,
        fetchRecurrents,
        addRecurrent
    }
})