import axios from 'axios';
import { API_URL } from '@/shared/api/urls';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  CodesGroupByCodeClParams,
  CodesGroupByCodeClResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';

export const codesGroupByCodeCl = ({ codeCl }: CodesGroupByCodeClParams) => {
  return resultify(
    axios.get<CodesGroupByCodeClResponse>(API_URL.codesGroupByCodeCl(codeCl)),
  );
};

export const useCodesGroupByCodeClMutation = (options?: UseMutationOptions<CodesGroupByCodeClResponse, NiceAxiosError, CodesGroupByCodeClParams>) => {
  const mutation = useMutation<CodesGroupByCodeClResponse, NiceAxiosError, CodesGroupByCodeClParams>({
    ...options,
    mutationFn: ({ 
      codeCl 
    }: CodesGroupByCodeClParams) => codesGroupByCodeCl({
      codeCl
    }),
  });

  return {
    ...mutation,
  };
};
