import { useFocusEffect } from "@react-navigation/native";

import { useNavigation } from "expo-router";
import { useCallback } from "react";
import { NavigationHeader } from "../components/navigation/Header";

export function useNavigationHeaderBack() {
  const navigation = useNavigation();

  useFocusEffect(
    useCallback(() => {
      let nav = navigation;
      while (nav.getId() !== "/(drawer)") {
        nav = nav.getParent();
      }

      nav?.setOptions({
        headerLeft: () => <NavigationHeader navigation={navigation} />,
      });
      return () => {
        nav?.setOptions({ headerLeft: undefined });
      };
    }, [])
  );
}
