use std::{borrow::Cow, path::Path};

use databake::Bake;
use jotdown::Event;

fn main() {
    let out = std::env::var("OUT_DIR").unwrap();

    let jots = glob::glob("src/*.jot").unwrap();

    for path in jots.map(|p| p.unwrap()) {
        let file = std::fs::read_to_string(&path).unwrap();
        let name = path.file_name().unwrap();

        let mut jot = jotdown::Parser::new(&file).collect::<Vec<_>>();

        jot.push(Event::Start(
            jotdown::Container::Paragraph,
            jotdown::Attributes(None),
        ));
        jot.push(Event::Str("Click anywhere to return to home screen".into()));
        jot.push(Event::End(jotdown::Container::Paragraph));

        std::fs::write(
            Path::new(&out).join(name),
            Cow::Owned::<'_, [Event<'_>]>(jot)
                .bake(&Default::default())
                .to_string(),
        )
        .unwrap();
    }
}
