/**
 * (share/ui/sonner — theme, layout props, icon overrides).
 */
import { render } from '@testing-library/react'
import { Toaster } from '../sonner'

jest.mock('next-themes', () => ({
  useTheme: jest.fn(),
}))

jest.mock('sonner', () => ({
  Toaster: jest.fn(() => {
    return <div data-testid="sonner-toaster" />
  }),
}))

describe('[share/ui/sonner]', () => {
  const { useTheme } = require('next-themes')
  const { Toaster: Sonner } = require('sonner')

  beforeEach(() => {
    jest.clearAllMocks()
  })

  describe('rendering', () => {
    it('renders underlying Toaster', () => {
      useTheme.mockReturnValue({ theme: 'dark' })

      const { getByTestId } = render(<Toaster />)

      expect(getByTestId('sonner-toaster')).toBeInTheDocument()
    })
  })

  describe('theme contract', () => {
    it('passes theme from useTheme', () => {
      useTheme.mockReturnValue({ theme: 'dark' })

      render(<Toaster />)

      expect(Sonner).toHaveBeenCalled()
      const props = Sonner.mock.calls[0][0]
      expect(props.theme).toBe('dark')
    })

    it('defaults to system when theme is undefined', () => {
      useTheme.mockReturnValue({})

      render(<Toaster />)

      const props = Sonner.mock.calls[0][0]
      expect(props.theme).toBe('system')
    })
  })

  describe('layout and icons', () => {
    it('sets position and offset', () => {
      useTheme.mockReturnValue({ theme: 'light' })

      render(<Toaster />)

      const props = Sonner.mock.calls[0][0]
      expect(props.position).toBe('top-right')
      expect(props.offset).toEqual({ top: '74px' })
    })

    it('passes icons configuration', () => {
      useTheme.mockReturnValue({ theme: 'light' })

      render(<Toaster />)

      const props = Sonner.mock.calls[0][0]
      expect(props.icons).toBeDefined()
      expect(props.icons.success).toBeTruthy()
      expect(props.icons.error).toBeTruthy()
    })

    it('allows overriding position via props', () => {
      useTheme.mockReturnValue({ theme: 'light' })

      render(<Toaster position="bottom-left" />)

      const props = Sonner.mock.calls[0][0]
      expect(props.position).toBe('bottom-left')
    })
  })
})
