use super::protocol;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageTreeRenderRow {
    pub node_id: String,
    pub title: String,
    pub depth: u32,
    pub expandable: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PageTreeDomRow {
    pub test_id: &'static str,
    pub action_create_test_id: &'static str,
    pub action_rename_test_id: &'static str,
    pub action_move_up_test_id: &'static str,
    pub node_id: String,
    pub depth: u32,
    pub title: String,
    pub expandable: bool,
}

pub fn build_page_tree_dom_rows(rows: &[PageTreeRenderRow]) -> Vec<PageTreeDomRow> {
    rows.iter()
        .map(|row| PageTreeDomRow {
            test_id: protocol::TEST_ID_TREE_NODE_OPEN,
            action_create_test_id: protocol::TEST_ID_TREE_ACTION_CREATE,
            action_rename_test_id: protocol::TEST_ID_TREE_ACTION_RENAME,
            action_move_up_test_id: protocol::TEST_ID_TREE_ACTION_MOVE_UP,
            node_id: row.node_id.clone(),
            depth: row.depth,
            title: row.title.clone(),
            expandable: row.expandable,
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::{build_page_tree_dom_rows, PageTreeRenderRow};

    #[test]
    fn tree_shell_page_renderer_builds_rows_with_stable_testids() {
        let rows = build_page_tree_dom_rows(&[
            PageTreeRenderRow {
                node_id: "page_root".into(),
                title: "首页".into(),
                depth: 0,
                expandable: true,
            },
            PageTreeRenderRow {
                node_id: "page_child".into(),
                title: "子页".into(),
                depth: 1,
                expandable: false,
            },
        ]);

        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].test_id, "tree-node-open");
        assert_eq!(rows[0].action_create_test_id, "tree-action-create");
        assert_eq!(rows[0].action_rename_test_id, "tree-action-rename");
        assert_eq!(rows[0].action_move_up_test_id, "tree-action-move-up");
        assert_eq!(rows[1].depth, 1);
    }
}
