import { describe, it, expect } from 'vitest';
import { assembleBaseChrome } from './base-chrome';
import { DEFAULT_SELECTION } from '../lib/theme';
import { SETTINGS_KEYS } from '../lib/settings';
import { DEFAULT_PRIMARY } from '../lib/menus';
import type { ActiveSelection } from '@platform-modules/ui-tokens/theme-engine';

describe('assembleBaseChrome', () => {
  it('resolves appearance from settings even when menus unavailable (resolvedNav null)', () => {
    const settings: Record<string, unknown> = {
      [SETTINGS_KEYS.theme]: {
        themeId: 'editorial',
        paletteId: 'slate',
        mode: 'dark',
      },
      [SETTINGS_KEYS.siteName]: 'Audit Test Site',
      [SETTINGS_KEYS.siteLogoUrl]: 'https://example.com/logo.png',
      [SETTINGS_KEYS.consent]: {
        enabled: true,
        version: 'audit-v2',
        policyHref: '/custom-privacy',
        categories: ['analytics', 'marketing'],
      },
      [SETTINGS_KEYS.hideModeToggle]: true,
    };

    const chrome = assembleBaseChrome({ settings, resolvedNav: null });

    expect(chrome.selection.paletteId).toBe('slate');
    expect(chrome.selection.mode).toBe('dark');
    expect(chrome.selection.themeId).toBe('editorial');
    expect(chrome.identity).toEqual({
      name: 'Audit Test Site',
      logoUrl: 'https://example.com/logo.png',
    });
    expect(chrome.consent).toEqual({
      enabled: true,
      version: 'audit-v2',
      policyHref: '/custom-privacy',
      categories: ['analytics', 'marketing'],
    });
    expect(chrome.hideModeToggle).toBe(true);
    expect(chrome.navLinks).toEqual(DEFAULT_PRIMARY);
  });

  it('falls back to DEFAULT_SELECTION when settings is null', () => {
    const chrome = assembleBaseChrome({ settings: null, resolvedNav: null });
    expect(chrome.selection).toEqual(DEFAULT_SELECTION);
  });

  it('uses resolvedNav when present', () => {
    const resolvedNav = [
      { label: 'About', href: '/about' },
      { label: 'Contact', href: '/contact' },
    ];

    const chrome = assembleBaseChrome({ settings: null, resolvedNav });

    expect(chrome.navLinks).toEqual(resolvedNav);
  });

  it('selectionProp overrides settings appearance', () => {
    const settings: Record<string, unknown> = {
      [SETTINGS_KEYS.theme]: {
        themeId: 'editorial',
        paletteId: 'slate',
        mode: 'dark',
      },
    };
    const selectionProp: ActiveSelection = {
      themeId: 'magazine',
      paletteId: 'default',
      mode: 'light',
    };

    const chrome = assembleBaseChrome({ settings, resolvedNav: null, selectionProp });

    expect(chrome.selection).toEqual(selectionProp);
  });
});