import axios from 'axios';
import { API_URL_USER } from '@/shared/api/api-url-user';
import { CBDCAxiosError } from '@/shared/@types/error';
import {
  UserExistsUseridResponse
} from '../model/types';
import {
  useQuery,
  UseQueryOptions
} from '@tanstack/react-query';

export const userExistsUserid = async (usrid: string) => {
  const response = await axios.post<UserExistsUseridResponse>(API_URL_USER.userExistsUserid(usrid));
  return response.data;
};

export const useUserExistsUseridQuery = (
  usrid: string,
  options?: Omit<UseQueryOptions<UserExistsUseridResponse, CBDCAxiosError>, 'queryKey' | 'queryFn'>
) => {
  const query = useQuery<UserExistsUseridResponse, CBDCAxiosError>({
    queryKey: ['userExistsUserid', usrid],
    queryFn: async () => await userExistsUserid(usrid),
    enabled: !!usrid && usrid.trim().length > 0, // usrid가 있을 때만 실행
    ...options,
  });

  return {
    ...query,
  };
};