use kifu_core::{CoreRequest, CoreResponse};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    pub fn log(s: &str);
}

#[wasm_bindgen]
#[derive(Debug)]
pub struct CoreApp(kifu_core::CoreApp);

#[wasm_bindgen]
impl CoreApp {
    #[wasm_bindgen(constructor)]
    pub fn new() -> Self {
        Self(kifu_core::CoreApp::new())
    }

    #[wasm_bindgen]
    pub async fn dispatch(&self, param: &JsValue) -> JsValue {
        let request: CoreRequest = serde_wasm_bindgen::from_value(param.clone()).unwrap();
        serde_wasm_bindgen::to_value(&self.0.dispatch(request).await).unwrap()
    }
}
