use std::path::PathBuf;

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, 'p> {
    standard_properties: WasmStandardProperties,
    additional_properties: Vec<(String, &'s str)>,
    children: Vec<WasmAstNode<'s, 'p>>,
    category: Option<&'p str>,
    path: Option<PathBuf>,
}

to_wasm!(
    WasmDocument<'s, 'p>,
    Document<'s>,
    original,
    wasm_context,
    standard_properties,
    {
        let category = original.category.as_ref().map(String::as_str);
        let path = original.path.clone();

        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<_>, _>>()?;

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

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