// imports

import { useQuery } from "@tanstack/vue-query";
import { useAppParams } from "~/composables/global/useAppParams";
import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";

// types

export type GetSavedProductsResponse = ApiPaginated<ProductListItem>;

// composable

const useGetSavedProducts = () => {
    // state

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

    const { page } = useAppParams();

    // methods

    const handleGetSavedProducts = async ({ signal }: { signal: AbortSignal }) => {
        const { data } = await axios.get<GetSavedProductsResponse>(`${API_ENDPOINTS.products.saved}`, {
            params: {
                offset: Number(page.value) * 15 - 15,
                limit: 15,
            },
            signal,
        });

        return data;
    };

    return useQuery({
        queryKey: [QUERY_KEYS.saved_products, page],
        queryFn: ({ signal }) => handleGetSavedProducts({ signal }),
    });
};

export default useGetSavedProducts;
