import { describe, expect, it } from 'vitest';
import { buildLocalizedPath, defaultLocaleRewrites } from '../localizedPath';

describe('buildLocalizedPath', () => {
  const supported = ['de', 'en'];

  it('prefixes path with locale', () => {
    expect(buildLocalizedPath('/packages', 'en', supported)).toBe('/en/packages');
    expect(buildLocalizedPath('/packages', 'de', supported)).toBe('/de/packages');
  });

  it('applies rewrite rules between locales', () => {
    expect(buildLocalizedPath('/kontakt', 'en', supported, 'de', defaultLocaleRewrites)).toBe('/en/contact');
    expect(buildLocalizedPath('/contact', 'de', supported, 'de', defaultLocaleRewrites)).toBe('/de/kontakt');
  });

  it('preserves query strings', () => {
    expect(buildLocalizedPath('/contact?ref=ad', 'de', supported)).toBe('/de/kontakt?ref=ad');
  });

  it('rewrites localized prefixes with dynamic segments', () => {
    expect(buildLocalizedPath('/bestellen/123', 'en', supported, 'de', defaultLocaleRewrites))
      .toBe('/en/checkout/123');
    expect(buildLocalizedPath('/checkout/123', 'de', supported, 'de', defaultLocaleRewrites))
      .toBe('/de/bestellen/123');
  });

  it('falls back to default locale when target not supported', () => {
    expect(buildLocalizedPath('/demo', 'fr', supported)).toBe('/de/demo');
  });

  it('handles empty or invalid paths gracefully', () => {
    expect(buildLocalizedPath('', 'en', supported)).toBe('/de');
    expect(buildLocalizedPath(undefined, 'en', supported)).toBe('/de');
  });
});
