// KAR-911/P4.1 — pure helpers extracted from qaf-provenance.tsx (the JSX
// component itself is tdd-guard:skip, same as formula-tooltip.tsx — no RTL
// in this repo, see qaf-projection.test.tsx for the established "test the
// extracted pure helper, not the render" pattern).

import { describe, expect, it } from 'vitest'
import { fmtConfidence, fmtOverrideWhen, pickLocaleText, workbookSafetyBadgeLabel } from '../qaf-provenance'

describe('pickLocaleText', () => {
  it('returns DE for locale de', () => {
    expect(pickLocaleText('de', 'Deutsch', 'English')).toBe('Deutsch')
  })

  it('returns EN for locale en when an EN value is given', () => {
    expect(pickLocaleText('en', 'Deutsch', 'English')).toBe('English')
  })

  it('falls back to DE for locale en when no EN value is given (KAR-906 tolerance)', () => {
    expect(pickLocaleText('en', 'Deutsch', null)).toBe('Deutsch')
    expect(pickLocaleText('en', 'Deutsch', undefined)).toBe('Deutsch')
    expect(pickLocaleText('en', 'Deutsch', '')).toBe('Deutsch')
  })

  it('returns DE for any other locale (es/zh) — DE is always the fallback', () => {
    expect(pickLocaleText('es', 'Deutsch', 'English')).toBe('Deutsch')
    expect(pickLocaleText('zh', 'Deutsch', 'English')).toBe('Deutsch')
  })
})

const pctFmt = new Intl.NumberFormat('de-DE', { style: 'percent', maximumFractionDigits: 0 })

describe('fmtConfidence', () => {
  it('formats a 0..1 ratio as a percent string', () => {
    expect(fmtConfidence(1)).toBe(pctFmt.format(1))
    expect(fmtConfidence(0.92)).toBe(pctFmt.format(0.92))
    expect(fmtConfidence(0)).toBe(pctFmt.format(0))
  })

  it('returns an em dash for null/undefined/NaN', () => {
    expect(fmtConfidence(null)).toBe('—')
    expect(fmtConfidence(undefined)).toBe('—')
    expect(fmtConfidence(NaN)).toBe('—')
  })
})

describe('workbookSafetyBadgeLabel', () => {
  it('labels a known macro-present issue bilingually', () => {
    expect(workbookSafetyBadgeLabel('de', 'security_macro_present')).toBe('Makro')
    expect(workbookSafetyBadgeLabel('en', 'security_macro_present')).toBe('Macro')
  })

  it('labels a known external-links issue bilingually', () => {
    expect(workbookSafetyBadgeLabel('de', 'security_external_links')).toBe('Externe Verknüpfung')
    expect(workbookSafetyBadgeLabel('en', 'security_external_links')).toBe('External link')
  })

  it('falls back to the raw issue type for an unrecognized type (never crashes)', () => {
    expect(workbookSafetyBadgeLabel('de', 'some_future_issue')).toBe('some_future_issue')
  })
})

// KAR-912/P4.2 — manual field-mapping override provenance display.
describe('fmtOverrideWhen', () => {
  it('formats a valid ISO-8601 timestamp in de-DE medium/short style', () => {
    const iso = '2026-07-10T08:15:00.000Z'
    const expected = new Intl.DateTimeFormat('de-DE', { dateStyle: 'medium', timeStyle: 'short' }).format(new Date(iso))
    expect(fmtOverrideWhen(iso)).toBe(expected)
  })

  it('falls back to the raw string for an unparsable value (never "Invalid Date")', () => {
    expect(fmtOverrideWhen('not-a-date')).toBe('not-a-date')
  })
})
