use mnote_editor_core::{
    export_markdown, import_markdown, import_plain_text, BlockType, ReferenceKind,
};

#[test]
fn markdown_import_export_round_trip_preserves_block_kinds() {
    let source = r#"# 根标题
- 列表项
- [x] 已完成
> 引用
---
```rust
fn main() {}
```
[[page_1|页面一]]
((block_1|块一))
普通段落
"#;

    let document = import_markdown(source).expect("markdown import should succeed");
    assert_eq!(
        document
            .blocks()
            .iter()
            .map(|block| block.block_type.clone())
            .collect::<Vec<_>>(),
        vec![
            BlockType::Heading,
            BlockType::BulletListItem,
            BlockType::Todo,
            BlockType::Quote,
            BlockType::Divider,
            BlockType::CodeBlock,
            BlockType::PageReference,
            BlockType::BlockReference,
            BlockType::Paragraph,
        ]
    );

    let exported = export_markdown(&document);
    let reparsed = import_markdown(&exported).expect("markdown re-import should succeed");
    assert_eq!(
        reparsed
            .blocks()
            .iter()
            .map(|block| block.block_type.clone())
            .collect::<Vec<_>>(),
        document
            .blocks()
            .iter()
            .map(|block| block.block_type.clone())
            .collect::<Vec<_>>()
    );
}

#[test]
fn markdown_export_renders_reference_tokens_and_code_fence() {
    let source = r#"[[page_42|设计文档]]
((block_99|引用块))
```ts
console.log("ok");
```
"#;
    let document = import_markdown(source).expect("markdown import should succeed");

    let page_ref = document.blocks()[0]
        .content
        .reference
        .as_ref()
        .expect("page reference");
    assert_eq!(page_ref.kind, ReferenceKind::Page);
    let block_ref = document.blocks()[1]
        .content
        .reference
        .as_ref()
        .expect("block reference");
    assert_eq!(block_ref.kind, ReferenceKind::Block);

    let exported = export_markdown(&document);
    assert!(exported.contains("[[page_42|设计文档]]"));
    assert!(exported.contains("((block_99|引用块))"));
    assert!(exported.contains("```ts"));
    assert!(exported.contains("console.log(\"ok\");"));
}

#[test]
fn plain_text_import_splits_paragraphs_and_normalizes_line_breaks() {
    let document =
        import_plain_text("第一段\n继续\n\n第二段").expect("plain text import should succeed");
    assert_eq!(document.blocks().len(), 2);
    assert_eq!(document.blocks()[0].content.text, "第一段 继续");
    assert_eq!(document.blocks()[1].content.text, "第二段");
    assert_eq!(document.blocks()[0].id, "imported_1");
    assert_eq!(document.blocks()[1].id, "imported_2");
}
