import {defineStore} from "pinia";
import {ref} from "vue";
import {Transaction} from "@/models/transaction";
import {TransactionService} from "@/services/transactions-service";

const transactionsService = TransactionService

export const useTransactionStore = defineStore('transactions', () => {
    const transactions = ref<Transaction[]>([])
    const isLoading = ref(false)

    const fetchTransactions = async (spaceId: number) => {
        isLoading.value = true
        try {
            transactions.value = await transactionsService.getTransactions(spaceId)
        } finally {
            isLoading.value = false
        }
    }

    const addTransaction = (transaction: Transaction) => {
        transactions.value.push(transaction)
    }

    return {
        transactions,
        isLoading,
        fetchTransactions,
        addTransaction
    }
})