import { createNavigationContainerRef } from '@react-navigation/native';
import { RootStackParamList } from 'types/types';

export const navigationRef = createNavigationContainerRef<RootStackParamList>();

// Overloads
export function navigate<RouteName extends keyof RootStackParamList>(
  name: RouteName,
): void;
export function navigate<RouteName extends keyof RootStackParamList>(
  name: RouteName,
  params: RootStackParamList[RouteName],
): void;

// Implementation
export function navigate<RouteName extends keyof RootStackParamList>(
  name: RouteName,
  params?: RootStackParamList[RouteName],
) {
  if (navigationRef.isReady()) {
    // @ts-expect-error — bu yerda overload tiplari bilan moslashadi
    navigationRef.navigate(name, params);
  }
}
