#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Single source of truth for NCRC site navigation."""
from __future__ import annotations

import html
from typing import Iterable

NAVIGATION: tuple[tuple[str, str], ...] = (
    ("Threat Register", "index.html"),
    ("GitHub", "github.html"),
    ("CVE's", "cves.html"),
    ("Papers", "papers.html"),
    ("Signals", "signals.html"),
    ("Incidents", "incidents.html"),
    ("Narratives", "narratives.html"),
    ("Regulatory", "regulatory.html"),
    ("About", "about.html"),
)


def e(value: object) -> str:
    return html.escape(str(value if value is not None else ""), quote=True)


def navigation(active: str, prefix: str = "") -> str:
    """Render the shared site header.

    active: menu label to mark active, e.g. 'Signals' or 'Narratives'.
    prefix: relative path prefix for nested pages, e.g. '../'.
    """
    labels = {label for label, _ in NAVIGATION}
    if active not in labels:
        raise ValueError(f"Unknown navigation label: {active!r}")

    links: list[str] = []
    for label, target in NAVIGATION:
        cls = "site-nav-link active" if label == active else "site-nav-link"
        links.append(
            f'<a class="{cls}" href="{e(prefix + target)}">{e(label)}</a>'
        )

    return (
        '<header class="site-header">'
        '<nav class="site-nav" aria-label="Main navigation">'
        + "".join(links)
        + '</nav>'
        + f'<a class="site-brand" href="{e(prefix + "index.html")}">'
          'Automotive Cybersecurity Dashboard</a>'
        + '</header>'
    )
