// imports

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

// types

export type GetAllOrdersResponse = ApiPaginated<Order>;

export type GetAllOrdersRequest = {
    sort: string | undefined;
    status: string | undefined;
    page: string | string[];
};

const useGetAllOrders = () => {
    // state

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

    const { sort, status, page } = useAppParams();

    // methods

    const handleGetAllOrders = async () => {
        const { data } = await axios.get<GetAllOrdersResponse>(API_ENDPOINTS.orders.get_all, {
            params: {
                sort: sort.value ?? "created_at",
                status: status.value,
                offset: Number(page.value) * 10 - 10,
                limit: 10,
            },
        });
        return data;
    };

    return useQuery({
        queryKey: [QUERY_KEYS.orders, sort, status, page],
        queryFn: () => handleGetAllOrders(),
    });
};

export default useGetAllOrders;
