import { queryOptions, useQuery } from '@tanstack/react-query';
import { QueryConfig } from '../../../lib/reactQuery';
import { api } from '../../../lib/apiClient';
import { ProductSizeResponse } from '../types/api';

type ProductSizesQueryParams = {
    q?: string;
    page?: number;
    limit?: number;
};

export const getProductSizes = ({ q, page, limit }: ProductSizesQueryParams = {}): Promise<ProductSizeResponse> => {
    return api.get('/inventory/sizes', {
        params: { q, page, limit },
    });
};

export const getProductSizesQueryOptions = ({ q, page, limit }: ProductSizesQueryParams = {}) => {
    return queryOptions({
        queryKey: ['productSizes', { q, page, limit }],
        queryFn: () => getProductSizes({ q, page, limit }),
    });
};

type UseProductSizesOptions = {
    queryConfig?: QueryConfig<typeof getProductSizesQueryOptions>;
} & ProductSizesQueryParams;

export const useProductSizes = ({ q, page, limit, queryConfig }: UseProductSizesOptions = {}) => {
    return useQuery({
        ...getProductSizesQueryOptions({ q, page, limit }),
        ...queryConfig,
    });
};
