import { _ as y, l as A, p as d, s as gn } from "./vendor-preact-DThS2ZQ_.js";
import { ScreenOrientationProvider, createScreenOrientationObserver, getDeviceClass } from "@incodetech/core/device";
import "@incodetech/core/events";
//#region src/hooks/useManager.ts
/**
* Hook for subscribing to manager state updates
* @param factory - Factory function that creates the manager instance
* @param options - Configuration options
* @returns Tuple of [state, manager]
*/
function useManager(factory, options = {}) {
	const { autoLoad = true, manageLifecycle = true } = options;
	const [manager] = d(factory);
	const [state, setState] = d(() => manager.getState());
	y(() => {
		const unsubscribe = manager.subscribe(setState);
		if (manageLifecycle && autoLoad && manager.load) manager.load();
		return () => {
			unsubscribe();
			if (manageLifecycle) manager.stop?.();
		};
	}, [
		manager,
		autoLoad,
		manageLifecycle
	]);
	return [state, manager];
}
//#endregion
//#region src/hooks/useScreenOrientation.ts
var observerInstance = null;
function getObserver() {
	if (!observerInstance) observerInstance = createScreenOrientationObserver(new ScreenOrientationProvider(), getDeviceClass());
	return observerInstance;
}
/**
* Hook that returns whether the device is in landscape mode on mobile (excluding iPad and desktop).
* Use this to show a landscape blocker overlay when true.
*/
function useScreenOrientation() {
	const [isLandscapeMobile, setIsLandscapeMobile] = d(false);
	y(() => {
		const observer = getObserver();
		setIsLandscapeMobile(observer.isLandscapeMobile);
		return observer.subscribe((value) => {
			gn(() => setIsLandscapeMobile(value));
		});
	}, []);
	return { isLandscapeMobile };
}
//#endregion
//#region src/hooks/useModuleTerminalCallbacks.ts
function isModuleTerminalStatus(status) {
	return status === "finished" || status === "error" || status === "closed" || status === "failed";
}
function isTerminalModuleState(state) {
	return isModuleTerminalStatus(state.status);
}
function useModuleTerminalCallbacks({ status, onFinish, onError, error, errorCode, moduleErrorCode, errorStatuses }) {
	const terminalFiredRef = A(false);
	y(() => {
		if (terminalFiredRef.current) return;
		if (status === "finished") {
			terminalFiredRef.current = true;
			onFinish?.();
			return;
		}
		if (status === "closed") {
			terminalFiredRef.current = true;
			onError?.(void 0);
			return;
		}
		if ((errorStatuses ?? ["error"]).includes(status)) {
			terminalFiredRef.current = true;
			if (moduleErrorCode !== void 0) onError?.(error, errorCode, moduleErrorCode);
			else onError?.(error, errorCode);
		}
	}, [
		status,
		onFinish,
		onError,
		error,
		errorCode,
		moduleErrorCode,
		errorStatuses
	]);
}
//#endregion
//#region src/hooks/useFormScrollable.ts
/**
* Tracks whether an element's content overflows its visible area vertically.
* Re-evaluates on element resize and whenever `depCount` changes
* (e.g. number of form fields).
*/
function useFormScrollable(ref, depCount) {
	const [isScrollable, setIsScrollable] = d(false);
	y(() => {
		const el = ref.current;
		if (!el) return;
		const check = () => setIsScrollable(el.scrollHeight > el.clientHeight);
		check();
		const ro = new ResizeObserver(check);
		ro.observe(el);
		return () => ro.disconnect();
	}, [depCount, ref]);
	return isScrollable;
}
//#endregion
//#region src/hooks/useMediaQuery.ts
var MOBILE_QUERY = "(max-width: 767px)";
function useMediaQuery(query) {
	const [matches, setMatches] = d(() => typeof window !== "undefined" && window.matchMedia(query).matches);
	y(() => {
		if (typeof window === "undefined") return;
		const mql = window.matchMedia(query);
		const onChange = () => setMatches(mql.matches);
		mql.addEventListener("change", onChange);
		return () => mql.removeEventListener("change", onChange);
	}, [query]);
	return matches;
}
function useIsMobileDeviceWidth() {
	return useMediaQuery(MOBILE_QUERY);
}
//#endregion
//#region src/hooks/useEventListener.ts
function useEventListener(eventName, handler) {
	y(() => {
		window.addEventListener(eventName, handler);
		return () => window.removeEventListener(eventName, handler);
	}, [eventName, handler]);
}
//#endregion
//#region src/hooks/useResizeObserver.ts
function useResizeObserver(ref, onResize) {
	y(() => {
		const element = ref.current;
		if (!element || typeof ResizeObserver === "undefined") {
			onResize();
			return;
		}
		onResize();
		const resizeObserver = new ResizeObserver(onResize);
		resizeObserver.observe(element);
		return () => resizeObserver.disconnect();
	}, [ref, onResize]);
}
//#endregion
export { isTerminalModuleState as a, useManager as c, useFormScrollable as i, useEventListener as n, useModuleTerminalCallbacks as o, useIsMobileDeviceWidth as r, useScreenOrientation as s, useResizeObserver as t };
