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

use cosmic_jotdown::JotdownItem;
use databake::Bake;
use jotdown::Event;

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

    let jots = glob::glob("src/*.dj").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 jot = cosmic_jotdown::jotdown_into_buffers(jotdown::Parser::new(&file).chain([
            Event::Start(jotdown::Container::Paragraph, jotdown::Attributes(None)),
            Event::Str("Click anywhere to return to home screen".into()),
            Event::End(jotdown::Container::Paragraph),
        ]))
        .collect::<Vec<_>>();

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