// This program has been developed by students from the bachelor Computer Science at Utrecht
// University within the Software Project course.
// © Copyright Utrecht University (Department of Information and Computing Sciences)
// __tests__/utils/test-utils.tsx
import { render, type RenderOptions } from '@testing-library/react';
import { type ReactElement, type ReactNode } from 'react';
import { ReactFlowProvider } from '@xyflow/react';
import {mockReactFlow} from "../setupFlowTests.ts";

mockReactFlow();

/**
 * Custom render function that wraps components with necessary providers
 * This ensures all components have access to ReactFlow context
 */
export function renderWithProviders(
  ui: ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>
) {
  function Wrapper({ children }: { children: ReactNode }) {
    return <ReactFlowProvider>{children}</ReactFlowProvider>;
  }

  return render(ui, { wrapper: Wrapper, ...options });
}


type SidebarRect = Partial<DOMRect>;

const defaultRect: DOMRect = {
  top: 0,
  left: 0,
  bottom: 100,
  right: 200,
  width: 200,
  height: 100,
  x: 0,
  y: 0,
  toJSON: () => {},
};

/**
 * Renders a component and injects a mock `#draggable-sidebar`
 * element required by Tooltip positioning logic.
 */
export function renderWithSidebar(
  ui: ReactElement,
  rect: SidebarRect = {},
  options?: RenderOptions
) {
  const sidebar = document.createElement('div');
  sidebar.id = 'draggable-sidebar';

  sidebar.getBoundingClientRect = jest.fn(() => ({
    ...defaultRect,
    ...rect,
  }));

  document.body.appendChild(sidebar);

  const result = render(ui, options);

  return {
    ...result,
    sidebar,
  };
}


// Re-export everything from testing library
//eslint-disable-next-line react-refresh/only-export-components
export * from '@testing-library/react';
