import { describe, it, expect } from 'vitest'
import { ref } from 'vue'
import { mount } from '@vue/test-utils'
import { TVU_LOCALE_KEY } from '../src/locale'
import Pagination from '../src/canonical/Pagination.vue'
import PopupBox from '../src/canonical/PopupBox.vue'
import InputNumber from '../src/canonical/InputNumber.vue'

const zh = ref({
  previous: '上一页',
  next: '下一页',
  pageUnit: '页',
  close: '关闭',
  decrement: '减少',
  increment: '增加',
})
const withZh = { global: { provide: { [TVU_LOCALE_KEY]: zh } } }

describe('locale integration', () => {
  it('Pagination localizes Previous/Next aria-labels + page unit', () => {
    const w = mount(Pagination, { props: { total: 100, modelValue: 2, pageSize: 10 }, ...withZh })
    const html = w.html()
    expect(w.find('[aria-label="上一页"]').exists()).toBe(true)
    expect(w.find('[aria-label="下一页"]').exists()).toBe(true)
    expect(html).toContain('/页')
    expect(html).not.toContain('aria-label="Previous"')
  })

  it('Pagination keeps English aria-labels with no provider', () => {
    const w = mount(Pagination, { props: { total: 100, modelValue: 2, pageSize: 10 } })
    expect(w.find('[aria-label="Previous"]').exists()).toBe(true)
    expect(w.find('[aria-label="Next"]').exists()).toBe(true)
    expect(w.html()).toContain('/Page')
  })

  it('PopupBox localizes the close button aria-label', () => {
    // PopupBox teleports its modal to <body> (INFRA-F43), so assert on document.body.
    const w = mount(PopupBox, { props: { visible: true, title: 'x' }, attachTo: document.body, ...withZh })
    expect(document.body.querySelector('[aria-label="关闭"]')).not.toBeNull()
    expect(document.body.querySelector('[aria-label="Close"]')).toBeNull()
    w.unmount()
  })

  it('InputNumber localizes decrement/increment aria-labels', () => {
    const w = mount(InputNumber, { props: { modelValue: 1 }, ...withZh })
    expect(w.find('[aria-label="减少"]').exists()).toBe(true)
    expect(w.find('[aria-label="增加"]').exists()).toBe(true)
  })
})
