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

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

export const getProductColours = ({ q, page, limit }: ProductColoursQueryParams = {}): Promise<ProductColourResponse> => {
    return api.get('/inventory/colours', {
        params: { q, page, limit },
    });
};

export const getProductColoursQueryOptions = ({ q, page, limit }: ProductColoursQueryParams = {}) => {
    return queryOptions({
        queryKey: ['productColours', { q, page, limit }],
        queryFn: () => getProductColours({ q, page, limit }),
    });
};

type UseProductColoursOptions = {
    queryConfig?: QueryConfig<typeof getProductColoursQueryOptions>;
} & ProductColoursQueryParams;

export const useProductColours = ({ q, page, limit, queryConfig }: UseProductColoursOptions = {}) => {
    return useQuery({
        ...getProductColoursQueryOptions({ q, page, limit }),
        ...queryConfig,
    });
};
