/*
    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>
    Copyright (C) 2023 tpart
*/
use jellyui::{css_bundle, js_bundle, js_bundle_map};
use rocket::{
    get,
    http::{ContentType, Header},
    response::Responder,
};
use std::borrow::Cow;

pub struct CachedAsset<T>(pub T);
impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for CachedAsset<T> {
    fn respond_to(self, request: &'r rocket::Request<'_>) -> rocket::response::Result<'o> {
        let mut res = self.0.respond_to(request)?;
        if cfg!(not(debug_assertions)) {
            res.set_header(Header::new("cache-control", "max-age=86400"));
        }
        Ok(res)
    }
}

#[get("/assets/bundle.css")]
pub fn r_assets_css() -> CachedAsset<(ContentType, Cow<'static, str>)> {
    CachedAsset((ContentType::CSS, css_bundle()))
}

#[get("/assets/cantarell.woff2")]
pub fn r_assets_font() -> CachedAsset<(ContentType, &'static [u8])> {
    CachedAsset((
        ContentType::WOFF2,
        include_bytes!("../../../web/cantarell.woff2"),
    ))
}

#[get("/assets/bundle.js")]
pub fn r_assets_js() -> CachedAsset<(ContentType, Cow<'static, str>)> {
    CachedAsset((ContentType::JavaScript, js_bundle()))
}
#[get("/assets/bundle.js.map")]
pub fn r_assets_js_map() -> CachedAsset<(ContentType, Cow<'static, str>)> {
    CachedAsset((ContentType::JSON, js_bundle_map()))
}
