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

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

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

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

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

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