use std::sync::Once;

use indoc::indoc;
use liwe::markdown::MarkdownReader;
use pretty_assertions::assert_str_eq;

use liwe::graph::Graph;

#[test]
fn normalization_meta_block() {
    setup();
    compare(
        indoc! {"
        ---
        key: value
        ---

        # header
        "},
        indoc! {"
        ---
        key: value
        ---

        # header
        "},
    );
}

fn compare(expected: &str, denormalized: &str) {
    setup();

    let mut graph = Graph::new();

    graph.from_markdown("key".into(), denormalized, MarkdownReader::new());

    let normalized = graph.to_markdown(&"key".into());

    println!("actual graph \n{:#?}", graph);
    println!("expected:\n{}", expected);
    println!("normalized:\n{}", normalized);

    assert_str_eq!(expected, normalized);
}

#[test]
fn normalization_meta_empty_frontmatter() {
    setup();
    compare(
        indoc! {"
        ---
        {}
        ---

        # Heading
        "},
        "---\n---\n\n# Heading\n",
    );
}

#[test]
fn normalization_meta_block_crlf() {
    setup();
    compare(
        indoc! {"
        ---
        title: value
        date: 2024-01-01
        ---

        # header
        "},
        "---\r\ntitle: value\r\ndate: 2024-01-01\r\n---\r\n\r\n# header\r\n",
    );
}

static INIT: Once = Once::new();

fn setup() {
    INIT.call_once(|| {
        let _ = env_logger::builder().try_init();
    });
}
