import { parseActiveSelection } from '../lib/theme';
import {
  parseSiteIdentity,
  parseConsentConfig,
  parseHideModeToggle,
  SETTINGS_KEYS,
} from '../lib/settings';
import { DEFAULT_PRIMARY } from '../lib/menus';
import type { ActiveSelection } from '@platform-modules/ui-tokens/theme-engine';

type SettingsMap = Awaited<ReturnType<typeof import('../lib/settings').getAllSettings>>;
export type NavLink = { label: string; href: string };

export interface BaseChrome {
  selection: ActiveSelection;
  identity: ReturnType<typeof parseSiteIdentity>;
  consent: ReturnType<typeof parseConsentConfig>;
  navLinks: NavLink[];
  hideModeToggle: boolean;
}

export function assembleBaseChrome(args: {
  settings: SettingsMap | null;
  resolvedNav: NavLink[] | null;
  selectionProp?: ActiveSelection;
}): BaseChrome {
  const { settings, resolvedNav, selectionProp } = args;
  const selectionFromSettings = parseActiveSelection(settings?.[SETTINGS_KEYS.theme]);
  return {
    selection: selectionProp ?? selectionFromSettings,
    identity: parseSiteIdentity(
      settings?.[SETTINGS_KEYS.siteName],
      settings?.[SETTINGS_KEYS.siteLogoUrl],
    ),
    consent: parseConsentConfig(settings?.[SETTINGS_KEYS.consent]),
    hideModeToggle: parseHideModeToggle(settings?.[SETTINGS_KEYS.hideModeToggle]),
    navLinks: resolvedNav && resolvedNav.length > 0 ? resolvedNav : DEFAULT_PRIMARY,
  };
}