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 { 
  UserUpdatePermissionsParams,
  UserUpdatePermissionsResponse
} from '../model/types';
import { 
  useMutation,
  UseMutationOptions
} from '@tanstack/react-query';
import { getHeaderUserAgent } from '@/shared/constants/url';

export const userUpdatePermissions = (params: UserUpdatePermissionsParams) => {
  let headerOptions = {
    menuId: 45,
    apiType: 'UPDATE'
  };
  let options = {
    headers: {
      'X-User-Agent': getHeaderUserAgent(headerOptions)
    }
  };
  return resultify(
    axios.post<UserUpdatePermissionsResponse>(API_URL_USER.updatePermissions(), params, options),
  );
};

export const useUserUpdatePermissionsMutation = (options?: UseMutationOptions<UserUpdatePermissionsResponse, NiceAxiosError, UserUpdatePermissionsParams>) => {
  const mutation = useMutation<UserUpdatePermissionsResponse, NiceAxiosError, UserUpdatePermissionsParams>({
    ...options,
    mutationFn: (params: UserUpdatePermissionsParams) => userUpdatePermissions(params),
  });

  return {
    ...mutation,
  };
};
