/*! © 2026 Yahoo, Inc. UDS v0.0.0-development */
import { useEffect, useState } from "react";

//#region src/hooks/useRtl.ts
/**
* Gets the current RTL state from document direction
*/
const getDocumentDirection = () => {
	if (typeof document === "undefined") return false;
	return getComputedStyle(document.documentElement).direction === "rtl";
};
/**
* Creates a mutation observer to watch for direction changes
*/
const createDirectionObserver = (callback) => {
	const observer = new MutationObserver((mutations) => {
		if (mutations.some((mutation) => mutation.type === "attributes" && mutation.attributeName === "dir")) callback();
	});
	observer.observe(document.documentElement, {
		attributes: true,
		attributeFilter: ["dir"]
	});
	return observer;
};
/**
* Hook to detect RTL context from document direction or explicit override
* @default reads from document direction
*/
const useRtl = (options) => {
	const [isRtl, setIsRtl] = useState(getDocumentDirection);
	useEffect(() => {
		if (options?.explicit) {
			setIsRtl(options.explicit);
			return;
		}
		const observer = createDirectionObserver(() => {
			setIsRtl(getDocumentDirection());
		});
		return () => observer.disconnect();
	}, [options?.explicit]);
	return isRtl;
};

//#endregion
export { createDirectionObserver, getDocumentDirection, useRtl };