import { describe, it, expect } from 'vitest';
import { renderOfferHtml, renderLegalHtml } from './offer';

describe('renderLegalHtml', () => {
  it('renders both language bodies into one standalone document with a switcher', () => {
    const html = renderLegalHtml({
      ru: { md: '# Политика\n\nрусский текст', title: 'Политика — Эрудит' },
      en: { md: '# Policy\n\nenglish text', title: 'Policy — Erudit' },
      canonical: 'https://erudit-game.ru/privacy/',
    });
    expect(html).toContain('<!doctype html>');
    expect(html).toContain('<title>Политика — Эрудит</title>'); // Russian is the default
    expect(html).toContain('href="https://erudit-game.ru/privacy/"');
    // Both bodies are present; English is hidden until toggled.
    expect(html).toContain('<main data-lang="ru">');
    expect(html).toContain('<main data-lang="en" hidden>');
    expect(html).toContain('<h1>Политика</h1>');
    expect(html).toContain('<h1>Policy</h1>');
    // The language + theme switcher and its script.
    expect(html).toContain('id="legal-lang"');
    expect(html).toContain('id="legal-theme"');
    expect(html).toContain('erudit.legal.lang');
    // Titles carried on <html> so the toggle can update document.title.
    expect(html).toContain('data-title-ru="Политика — Эрудит"');
    expect(html).toContain('data-title-en="Policy — Erudit"');
  });

  it('renders markdown headings, bold clause numbers and lists in both languages', () => {
    const html = renderLegalHtml({
      ru: { md: '## 2. Предмет\n\n**2.1.** Текст.\n\n- первый\n- второй', title: 't' },
      en: { md: '## 2. Subject\n\n**2.1.** Text.', title: 't' },
      canonical: 'c',
    });
    expect(html).toContain('<h2>2. Предмет</h2>');
    expect(html).toContain('<strong>2.1.</strong>');
    expect(html).toContain('<li>первый</li>');
    expect(html).toContain('<h2>2. Subject</h2>');
  });

  it('scopes the shrink-to-fit first-column table rule to the offer only', () => {
    // The offer opts into the nowrap product-name column via body.offer; other legal pages (the
    // privacy data table) must NOT get it, or their prose first column cannot wrap.
    const generic = renderLegalHtml({ ru: { md: '# x', title: 't' }, en: { md: '# x', title: 't' }, canonical: 'c' });
    expect(generic).toContain('<body>');
    expect(generic).not.toContain('<body class=');
    expect(generic).toContain('.offer td:first-child');
  });
});

describe('renderOfferHtml', () => {
  it('is the offer preset: both languages, body.offer, offer title + canonical, EN transliteration', () => {
    const html = renderOfferHtml('# Публичная оферта\n\nрусский', '# Public offer\n\nenglish');
    expect(html).toContain('<body class="offer">');
    expect(html).toContain('<title>Публичная оферта — Эрудит</title>');
    expect(html).toContain('data-title-en="Public offer — Erudit"');
    expect(html).toContain('href="https://erudit-game.ru/offer/"');
    expect(html).toContain('<h1>Публичная оферта</h1>');
    expect(html).toContain('<h1>Public offer</h1>');
    // The offer page transliterates the Russian product names left in the English (price-list) view.
    expect(html).toContain('translitEn');
  });
});
