import axios from 'axios';
import { API_URL_USER } from '@/shared/api/api-url-user';
import { resultify } from '@/shared/lib/resultify';
import { NiceAxiosError } from '@/shared/@types/error';
import { 
  UserMenuPermissionsParams,
  UserMenuPermissionsResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const userMenuPermissions = (params: UserMenuPermissionsParams) => {
  let headerOptions = {
    menuId: 45,
    apiType: 'SELECT'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<UserMenuPermissionsResponse>(API_URL_USER.findMenuPermissions(), params, options),
  );
};

export const useUserMenuPermissionsMutation = (options?: UseMutationOptions<UserMenuPermissionsResponse, NiceAxiosError, UserMenuPermissionsParams>) => {
  const mutation = useMutation<UserMenuPermissionsResponse, NiceAxiosError, UserMenuPermissionsParams>({
    ...options,
    mutationFn: (params: UserMenuPermissionsParams) => userMenuPermissions(params),
  });
  return {
    ...mutation,
  };
};
