// REQ-USR-004: LoginPage 组件测试共享渲染工具（Provider + Router + AntD App 上下文）
import type { ReactElement } from 'react';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter } from 'react-router-dom';
import { App as AntdApp, ConfigProvider } from 'antd';
import { configureStore } from '@reduxjs/toolkit';
import authReducer from '../../src/store/slices/authSlice';
import type { RootState } from '../../src/store/store';

export function makeStore() {
  return configureStore({ reducer: { auth: authReducer } });
}

export function renderWithProviders(
  ui: ReactElement,
  options?: { store?: ReturnType<typeof makeStore>; initialEntries?: string[] },
) {
  const store = options?.store ?? makeStore();
  const result = render(
    <Provider store={store}>
      <ConfigProvider>
        <AntdApp>
          <MemoryRouter initialEntries={options?.initialEntries ?? ['/login']}>
            {ui}
          </MemoryRouter>
        </AntdApp>
      </ConfigProvider>
    </Provider>,
  );
  return {
    ...result,
    store,
    getState: () => store.getState() as RootState,
  };
}
