// Vitest global setup: registers @testing-library/jest-dom matchers
// (toBeInTheDocument, toHaveAttribute, …) for the jsdom environment.
// The `/vitest` entry both registers the matchers at runtime and provides the
// TypeScript module augmentation for vitest's `expect` so tsc typechecks them.
import "@testing-library/jest-dom/vitest";

// jsdom does not implement ResizeObserver, but several Radix primitives that
// shadcn wraps (ScrollArea, Select via react-popper/react-use-size, DropdownMenu,
// Tabs, etc.) reference it at module-load or render time. Without a stub, any
// component test whose render tree pulls one of these in fails with
// `ReferenceError: ResizeObserver is not defined`. Stub a no-op observer so the
// whole suite (and future component tests) is resilient to cross-test module
// loading in the Vitest pool.
class ResizeObserverStub {
	observe() {}
	unobserve() {}
	disconnect() {}
}

globalThis.ResizeObserver =
	ResizeObserverStub as unknown as typeof ResizeObserver;

// Radix popper also probes `requestAnimationFrame`; jsdom provides it, but some
// primitives defer layout reads through rAF that never flush in jsdom. Keep the
// default rAF; this guard is intentionally minimal.

// Radix Select uses pointer capture APIs that jsdom does not implement.
// Stub them on HTMLElement so opening/closing selects in tests does not throw.
if (typeof window !== "undefined" && window.HTMLElement) {
	const proto = window.HTMLElement.prototype;
	if (!proto.hasPointerCapture) {
		proto.hasPointerCapture = () => false;
	}
	if (!proto.setPointerCapture) {
		proto.setPointerCapture = () => {};
	}
	if (!proto.releasePointerCapture) {
		proto.releasePointerCapture = () => {};
	}
}

// Radix Select also calls scrollIntoView on items when opening; jsdom lacks it.
if (typeof window !== "undefined" && window.Element) {
	const proto = window.Element.prototype;
	if (!proto.scrollIntoView) {
		proto.scrollIntoView = () => {};
	}
}
