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

export const userExistsUserid = async (usrid: string) => {
  let headerOptions = {
    menuId: 45,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  const response = await axios.post<UserExistsUseridResponse>(API_URL_USER.userExistsUserid(usrid), options);
  return response.data;
};

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

  return {
    ...query,
  };
};