import type { ReactNode } from 'react'
import { render, screen } from '@testing-library/react'
import zhCN from 'antd/locale/zh_CN'
import { describe, expect, it, vi } from 'vitest'

import { ThemeProvider } from './ThemeProvider'

const configProviderMock = vi.fn(
  ({ children }: { children: ReactNode }) => <div data-testid="config-provider">{children}</div>,
)

vi.mock('antd', async () => {
  const actual = await vi.importActual<typeof import('antd')>('antd')
  return {
    ...actual,
    ConfigProvider: (props: { children: ReactNode; theme: unknown; locale: unknown }) => configProviderMock(props),
  }
})

describe('ThemeProvider', () => {
  it('passes the theme tokens and locale to ConfigProvider', () => {
    render(
      <ThemeProvider>
        <div>theme child</div>
      </ThemeProvider>,
    )

    expect(screen.getByTestId('config-provider')).toBeInTheDocument()
    expect(screen.getByText('theme child')).toBeInTheDocument()
    expect(configProviderMock).toHaveBeenCalledWith(
      expect.objectContaining({
        locale: zhCN,
        theme: expect.objectContaining({
          token: expect.objectContaining({
            colorPrimary: '#0e5a6a',
            colorSuccess: '#217a5b',
            colorWarning: '#b7791f',
            colorError: '#b33a3a',
            colorInfo: '#2d6a9f',
            fontFamily: '"IBM Plex Sans", "PingFang SC", "Microsoft YaHei", sans-serif',
            borderRadius: 10,
          }),
          components: expect.objectContaining({
            Table: expect.objectContaining({
              headerBg: '#f8f5ef',
              borderColor: '#d6d0c3',
            }),
            Card: expect.objectContaining({
              borderRadiusLG: 16,
            }),
            Button: expect.objectContaining({
              controlHeightLG: 44,
            }),
            Input: expect.objectContaining({
              controlHeight: 36,
            }),
            Select: expect.objectContaining({
              controlHeight: 36,
            }),
          }),
        }),
      }),
    )
  })
})
