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

type ProductSizesQueryParams = {
    search?: string | null;
    page?: number;
    limit?: number;
};

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

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

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

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