import { useMutation, useQueryClient } from '@tanstack/react-query';
import { addressesApi } from '../apis';
import { addressKeys } from '../keys';
import type { UpdateAddressInput } from '../types';

/** Edits an owned address, then invalidates the lists so the card reflects the change. */
export function useUpdateAddress() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, input }: { id: number; input: UpdateAddressInput }) => addressesApi.update(id, input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: addressKeys.lists() });
    },
  });
}
