use serde::Serialize;

use super::ast_node::WasmAstNode;
use super::macros::to_wasm;
use super::standard_properties::WasmStandardProperties;
use super::to_wasm::ToWasm;
use crate::types::Document;
use crate::wasm::to_wasm::ToWasmStandardProperties;

#[derive(Debug, Serialize)]
#[serde(tag = "ast_node")]
#[serde(rename = "org-data")]
pub struct WasmDocument<'s> {
    standard_properties: WasmStandardProperties,
    additional_properties: Vec<(String, &'s str)>,
    children: Vec<WasmAstNode<'s>>,
}

to_wasm!(
    WasmDocument<'s>,
    Document<'s>,
    original,
    wasm_context,
    standard_properties,
    {
        let additional_properties: Vec<(String, &str)> = original
            .get_additional_properties()
            .map(|node_property| {
                (
                    format!(":{}", node_property.property_name.to_uppercase()),
                    node_property.value.unwrap_or(""),
                )
            })
            .collect();

        let children = original
            .zeroth_section
            .iter()
            .map(|child| {
                child
                    .to_wasm(wasm_context.clone())
                    .map(Into::<WasmAstNode<'_>>::into)
            })
            .chain(original.children.iter().map(|child| {
                child
                    .to_wasm(wasm_context.clone())
                    .map(Into::<WasmAstNode<'_>>::into)
            }))
            .collect::<Result<Vec<_>, _>>()?;
        // let children = original
        //     .children
        //     .iter()
        //     .map(|child| {
        //         child
        //             .to_wasm(wasm_context.clone())
        //             .map(Into::<WasmAstNode<'_>>::into)
        //     })
        //     .collect::<Result<Vec<_>, _>>()?;

        Ok(WasmDocument {
            standard_properties,
            additional_properties,
            children,
        })
    }
);

impl<'s> Into<WasmAstNode<'s>> for WasmDocument<'s> {
    fn into(self) -> WasmAstNode<'s> {
        WasmAstNode::Document(self)
    }
}