/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
"use client";
import { isInputLike } from "./utils.js";
import { useEffect, useRef, useState } from "react";
//#region src/components/client/BottomSheet/useVirtualKeyboard.ts
/**
* Minimum keyboard height (px) to treat as a real virtual keyboard.
* Filters out small residual values (e.g. iOS safe area insets) that
* don't represent an actual keyboard.
*/
const MIN_VIRTUAL_KEYBOARD_HEIGHT_PX = 100;
/**
* Hook to get the height of the virtual keyboard.
* @param isEnabled - Whether the virtual keyboard is enabled.
* @returns The height of the virtual keyboard in pixels, or 0 if below the minimum threshold.
*/
function useVirtualKeyboard(isEnabled) {
	const [keyboardHeightPx, setKeyboardHeightPx] = useState(0);
	const keyboardIsOpenRef = useRef(false);
	useEffect(() => {
		if (!isEnabled) {
			keyboardIsOpenRef.current = false;
			setKeyboardHeightPx(0);
			return;
		}
		const vv = window.visualViewport;
		if (!vv) return;
		const viewport = vv;
		function onVisualViewportResize() {
			const nextKeyboardHeight = Math.max(window.innerHeight - viewport.height, 0);
			const focused = document.activeElement;
			const keyboardIsOpen = nextKeyboardHeight > 0 && (isInputLike(focused) || keyboardIsOpenRef.current);
			keyboardIsOpenRef.current = keyboardIsOpen;
			setKeyboardHeightPx(keyboardIsOpen ? nextKeyboardHeight : 0);
		}
		onVisualViewportResize();
		viewport.addEventListener("resize", onVisualViewportResize);
		return () => viewport.removeEventListener("resize", onVisualViewportResize);
	}, [isEnabled]);
	const height = isEnabled ? keyboardHeightPx : 0;
	return height >= MIN_VIRTUAL_KEYBOARD_HEIGHT_PX ? height : 0;
}
//#endregion
export { useVirtualKeyboard };
