import { describe, it, expect } from 'vitest';
import { en } from '@i18n/dictionary.en';
import { no } from '@i18n/dictionary.no';
import { interpolate } from '@i18n/t';

/** Collect all leaf key paths of a nested object. */
function keyPaths(obj: unknown, prefix = ''): string[] {
  if (obj === null || typeof obj !== 'object') return [prefix];
  return Object.entries(obj as Record<string, unknown>).flatMap(([k, v]) =>
    keyPaths(v, prefix ? `${prefix}.${k}` : k),
  );
}

describe('dictionary parity (I18N_SPEC §4.1)', () => {
  it('EN and NO expose exactly the same key paths', () => {
    const enKeys = keyPaths(en).sort();
    const noKeys = keyPaths(no).sort();
    expect(noKeys).toEqual(enKeys);
  });

  it('no dictionary value is empty', () => {
    for (const [dict, name] of [
      [en, 'en'],
      [no, 'no'],
    ] as const) {
      for (const path of keyPaths(dict)) {
        const value = path
          .split('.')
          .reduce<unknown>((acc, k) => (acc as Record<string, unknown>)[k], dict);
        expect(typeof value, `${name}.${path}`).toBe('string');
        expect((value as string).length, `${name}.${path}`).toBeGreaterThan(0);
      }
    }
  });
});

describe('interpolate', () => {
  it('replaces named tokens', () => {
    expect(interpolate('© {year} X', { year: 2026 })).toBe('© 2026 X');
    expect(interpolate('pdf · {size} kB', { size: 132 })).toBe('pdf · 132 kB');
  });
  it('leaves unknown tokens untouched', () => {
    expect(interpolate('{a} {b}', { a: '1' })).toBe('1 {b}');
  });
});
