import "@testing-library/jest-dom/vitest";
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";

// Mantine queries matchMedia (used by useMediaQuery, color scheme).
if (typeof window !== "undefined" && !window.matchMedia) {
  Object.defineProperty(window, "matchMedia", {
    writable: true,
    value: (query: string) => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: () => {},
      removeListener: () => {},
      addEventListener: () => {},
      removeEventListener: () => {},
      dispatchEvent: () => false,
    }),
  });
}

// ResizeObserver is referenced by Mantine ScrollArea / popovers under jsdom.
if (typeof window !== "undefined" && !(window as any).ResizeObserver) {
  (window as any).ResizeObserver = class {
    observe() {}
    unobserve() {}
    disconnect() {}
  };
}

// scrollTo is not implemented in jsdom; Mantine ScrollArea calls it.
if (typeof window !== "undefined" && !window.HTMLElement.prototype.scrollTo) {
  // @ts-ignore
  window.HTMLElement.prototype.scrollTo = () => {};
}

afterEach(() => {
  cleanup();
});
