import axios from 'axios';
import { API_URL_SUPPORT } from '@/shared/api/api-url-support';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  QnaSaveParams,
  QnaSaveResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const qnaSave = (params: QnaSaveParams) => {
  let headerOptions = {
    menuId: 64,
    apiType: 'INSERT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<QnaSaveResponse>(API_URL_SUPPORT.qnaSave(), params, options),
  );
};

export const useQnaSaveMutation = (options?: UseMutationOptions<QnaSaveResponse, NiceAxiosError, QnaSaveParams>) => {
  const mutation = useMutation<QnaSaveResponse, NiceAxiosError, QnaSaveParams>({
    ...options,
    mutationFn: (params: QnaSaveParams) => qnaSave(params),
  });

  return {
    ...mutation,
  };
};
