import type { HttpRequest } from '@yaakapp-internal/models';
import { router } from '../lib/router';
import { invokeCmd } from '../lib/tauri';
import { useFastMutation } from './useFastMutation';

export function useDuplicateHttpRequest({
  id,
  navigateAfter,
}: {
  id: string | null;
  navigateAfter: boolean;
}) {
  return useFastMutation<HttpRequest, string>({
    mutationKey: ['duplicate_http_request', id],
    mutationFn: async () => {
      if (id === null) throw new Error("Can't duplicate a null request");
      return invokeCmd('cmd_duplicate_http_request', { id });
    },
    onSuccess: async (request) => {
      if (navigateAfter) {
        await router.navigate({
          to: '/workspaces/$workspaceId',
          params: { workspaceId: request.workspaceId },
          search: (prev) => ({ ...prev, request_id: request.id }),
        });
      }
    },
  });
}
