/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use crate::{
    CONF, Page,
    locale::{Language, tr, trs},
    node_card::NodeCard,
};
use jellycommon::api::ApiHomeResponse;
use markup::DynRender;

markup::define! {
    HomePage<'a>(lang: &'a Language, r: ApiHomeResponse) {
        h2 { @tr(**lang, "home.bin.root").replace("{title}", &CONF.brand) }
        ul.children.hlist {@for (node, udata) in &r.toplevel {
            li { @NodeCard { node, udata, lang } }
        }}
        @for (name, nodes) in &r.categories {
            @if !nodes.is_empty() {
                h2 { @trs(lang, name) }
                ul.children.hlist {@for (node, udata) in nodes {
                    li { @NodeCard { node, udata, lang } }
                }}
            }
        }
    }
}

impl Page for HomePage<'_> {
    fn title(&self) -> String {
        tr(*self.lang, "home").to_string()
    }
    fn to_render(&self) -> DynRender<'_> {
        markup::new!(@self)
    }
}
