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

import { useNavigation } from "expo-router";
import { useCallback } from "react";

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

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

      nav?.setOptions({
        headerBackVisible: true,
      });
      return () => {
        nav?.setOptions({ headerBackVisible: false });
      };
    }, [])
  );
}
