import { describe, expect, it } from 'vitest'
import { buildHrefLangs, LOCALE_BCP47 } from './hreflang.js'

describe('@platform-modules/seo/hreflang', () => {
  it('buildHrefLangs emits x-default and per-locale BCP47 tags', () => {
    const links = buildHrefLangs(['en', 'he'], 'he', (locale) => `https://shop.test/${locale}/deals`)
    expect(links).toEqual(
      expect.arrayContaining([
        { hreflang: 'en', href: 'https://shop.test/en/deals' },
        { hreflang: LOCALE_BCP47.he, href: 'https://shop.test/he/deals' },
        { hreflang: 'x-default', href: 'https://shop.test/he/deals' },
      ]),
    )
    expect(links.filter((l) => l.hreflang === 'x-default')).toHaveLength(1)
  })

  it('falls back x-default to the first locale when current is not in the list', () => {
    const links = buildHrefLangs(['en', 'he'], 'fr' as 'en' | 'he', (locale) => `https://shop.test/${locale}`)
    const xDefault = links.find((l) => l.hreflang === 'x-default')
    expect(xDefault?.href).toBe('https://shop.test/en')
  })

  it('single-locale list still emits that locale plus x-default', () => {
    const links = buildHrefLangs(['en'], 'en', (locale) => `https://shop.test/${locale}`)
    expect(links).toEqual([
      { hreflang: 'en', href: 'https://shop.test/en' },
      { hreflang: 'x-default', href: 'https://shop.test/en' },
    ])
  })

  it('unknown locale with no BCP47 mapping falls back to the raw code', () => {
    const links = buildHrefLangs(['es'], 'es', (locale) => `https://shop.test/${locale}`)
    expect(links[0]).toEqual({ hreflang: 'es', href: 'https://shop.test/es' })
  })
})
