"use client";

import { useEffect } from "react";
import {
  handleHardwareBack,
  handleHardwareBackSync,
} from "@/hooks/use-hardware-back-handler";

/**
 * Wires the React hardware-back handler stack to the global
 * window.__habibHandleHardwareBack and window.__habibHandleHardwareBackSync functions.
 *
 * Mount this once in the Providers tree (after React hydration).
 * It replaces the bootstrap stub with the real handler that walks
 * the registered stack.
 */
export function HardwareBackBridge() {
  useEffect(() => {
    window.__habibHandleHardwareBack = handleHardwareBack;
    window.__habibHandleHardwareBackSync = handleHardwareBackSync;

    return () => {
      // On unmount (shouldn't happen in practice), restore the stubs.
      window.__habibHandleHardwareBack = () =>
        Promise.resolve({ handled: false });
      window.__habibHandleHardwareBackSync = () => false;
    };
  }, []);

  return null;
}

export default HardwareBackBridge;
