import {navigationRef} from '@navigation/navigator';
import {Screens} from '@shared/constants';

import {ChatsStackParamList} from './interfaces';

/**
 * Get the current conversation id from the navigation stack
 * @returns The current conversation id or null if the current screen is not a chat screen
 */
export const getCurrentConversation = () => {
  let currentRoute = null;
  try {
    currentRoute = navigationRef.getCurrentRoute();
  } catch (error) {
    // do nothing
  }

  const chatId =
    currentRoute?.name === Screens.CHAT_MESSAGES &&
    (currentRoute?.params as ChatsStackParamList[Screens.CHAT_MESSAGES])?.id;
  return chatId || null;
};
