use xmodel_render::splitter::{split, Form, Segment};

#[test]
fn plain_text_passes_through_unchanged() {
    let input = "hello\nworld\n";
    let segments = split(input);
    assert_eq!(segments, vec![Segment::Prose("hello\nworld\n".to_string())]);
}

#[test]
fn form_only_block_is_isolated() {
    let input = "before\n{form-only: json}\nschema rule\n{/form-only}\nafter\n";
    let segments = split(input);
    assert_eq!(
        segments,
        vec![
            Segment::Prose("before\n".to_string()),
            Segment::FormOnly { form: Form::Json, body: "schema rule\n".to_string() },
            Segment::Prose("after\n".to_string()),
        ]
    );
}

#[test]
fn ail_form_only_block_uses_ail_form() {
    let input = "{form-only: ail}\ngrammar rule\n{/form-only}\n";
    let segments = split(input);
    assert_eq!(
        segments,
        vec![
            Segment::FormOnly { form: Form::Ail, body: "grammar rule\n".to_string() },
        ]
    );
}

#[test]
fn example_marker_is_isolated() {
    let input = "before\n{example: empty_module}\nafter\n";
    let segments = split(input);
    assert_eq!(
        segments,
        vec![
            Segment::Prose("before\n".to_string()),
            Segment::Example("empty_module".to_string()),
            Segment::Prose("after\n".to_string()),
        ]
    );
}

#[test]
fn multiple_directives_in_sequence() {
    let input = "p1\n{example: a}\n{form-only: json}\njs\n{/form-only}\n{form-only: ail}\nax\n{/form-only}\n{example: b}\np2\n";
    let segments = split(input);
    assert_eq!(
        segments,
        vec![
            Segment::Prose("p1\n".to_string()),
            Segment::Example("a".to_string()),
            Segment::FormOnly { form: Form::Json, body: "js\n".to_string() },
            Segment::FormOnly { form: Form::Ail, body: "ax\n".to_string() },
            Segment::Example("b".to_string()),
            Segment::Prose("p2\n".to_string()),
        ]
    );
}

#[test]
fn unknown_form_name_is_an_error() {
    let input = "{form-only: yaml}\nx\n{/form-only}\n";
    let result = std::panic::catch_unwind(|| split(input));
    assert!(result.is_err(), "split should panic on unknown form name");
}

#[test]
fn unclosed_form_only_is_an_error() {
    let input = "{form-only: json}\nopen\n";
    let result = std::panic::catch_unwind(|| split(input));
    assert!(result.is_err(), "split should panic on unclosed form-only block");
}
