// imports

import { useQuery } from "@tanstack/vue-query";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
import type { ComputedRef } from "vue";

// types

export type GetTransactionResponse = Transaction;

const useGetTransaction = (tracking_code: ComputedRef<string>) => {
    // state

    const { $axios: axios } = useNuxtApp();

    // methods

    const handleGetTransaction = async () => {
        const { data } = await axios.get<GetTransactionResponse>(
            `${API_ENDPOINTS.orders.checkout.transaction}/${tracking_code.value}`
        );
        return data;
    };

    return useQuery({
        queryKey: [QUERY_KEYS.transaction, tracking_code],
        queryFn: () => handleGetTransaction(),
    });
};

export default useGetTransaction;
