/**
 * (share/ui/textarea — variants, blur trim, disabled branch).
 */
import React from 'react'
import { fireEvent, render, screen } from '@testing-library/react'
import { Textarea, textareaVariants } from '@/share/ui/textarea'

describe('[share/ui/textarea]', () => {
  describe('rendering', () => {
    it('renders native textarea with placeholder', () => {
      render(<Textarea placeholder="Enter text" />)
      expect(document.querySelector('textarea')).toBeInTheDocument()
      expect(screen.getByPlaceholderText('Enter text')).toBeInTheDocument()
    })

    it('supports size, variant, disabled, and className', () => {
      render(
        <Textarea
          placeholder="Describe"
          size="lg"
          variant="error"
          disabled
          className="custom-textarea"
        />,
      )

      const textarea = screen.getByPlaceholderText('Describe')
      expect(textarea).toBeDisabled()
      expect(textarea).toHaveClass('custom-textarea')
    })
  })

  describe('textareaVariants', () => {
    it('returns classes for size/variant and disabled', () => {
      expect(textareaVariants({ size: 'xs', variant: 'success' })).toContain('min-h-18')
      expect(textareaVariants({ disabled: true })).toContain('cursor-not-allowed')
    })
  })

  describe('blur trim (happy / negative)', () => {
    it('trims whitespace on blur', () => {
      const onChange = jest.fn()
      render(<Textarea placeholder="T" defaultValue="  hello  " onChange={onChange} />)
      const textarea = screen.getByPlaceholderText('T') as HTMLTextAreaElement

      fireEvent.change(textarea, { target: { value: 'hello ' } })
      expect(onChange).toHaveBeenCalled()

      fireEvent.blur(textarea)
      expect(textarea.value).toBe('hello')
      expect(onChange).toHaveBeenCalled()
    })

    it('does not trim when disabled', () => {
      render(<Textarea placeholder="D" defaultValue="  hello  " disabled />)
      const textarea = screen.getByPlaceholderText('D') as HTMLTextAreaElement
      fireEvent.blur(textarea)
      expect(textarea.value).toBe('  hello  ')
    })
  })
})
