@* Components/NavRailSection.razor — CSS-only collapsible (no JS, works in static SSR).
   Apps that want cookie-persisted expand state keep their own interactive NavSection. *@
@namespace ZB.MOM.WW.Theme
<details class="rail-section" open="@Expanded" data-nav-key="@ResolvedKey">
    @* aria-expanded mirrors the native <details open> state so tests and assistive
       tech have a stable, queryable attribute (kit issue #1). It is rendered from
       Expanded at SSR time and kept in sync by nav-state.js on restore and toggle. *@
    <summary class="rail-eyebrow-toggle" aria-expanded="@(Expanded ? "true" : "false")">@Title</summary>
    <div class="rail-section-body">@ChildContent</div>
</details>

@code {
    /// <summary>
    /// Eyebrow label shown in the section's <c>&lt;summary&gt;</c> header. Required.
    /// </summary>
    [Parameter, EditorRequired] public string Title { get; set; } = string.Empty;

    /// <summary>
    /// Initial open state of the collapsible section. Defaults to <c>true</c> (expanded).
    /// Backed by the native <c>&lt;details open&gt;</c> attribute — no JavaScript required.
    /// </summary>
    [Parameter] public bool Expanded { get; set; } = true;

    /// <summary>
    /// Stable identifier used to persist this section's open/closed state in
    /// localStorage (via the kit's nav-state.js). Defaults to a slug of <see cref="Title"/>.
    /// </summary>
    [Parameter] public string? Key { get; set; }

    /// <summary>
    /// Section content — typically <see cref="NavRailItem"/> children.
    /// </summary>
    [Parameter] public RenderFragment? ChildContent { get; set; }

    private string ResolvedKey => string.IsNullOrWhiteSpace(Key) ? Slug(Title) : Key!;

    private static string Slug(string? s)
    {
        if (string.IsNullOrWhiteSpace(s)) return string.Empty;
        var chars = s.Trim().ToLowerInvariant()
            .Select(c => char.IsLetterOrDigit(c) ? c : '-').ToArray();
        return string.Join('-', new string(chars).Split('-', StringSplitOptions.RemoveEmptyEntries));
    }
}
