use base64::Engine;
use serde::{Serialize, de::DeserializeOwned};

const HANDLER_NAME: &str = "__nwv__";

pub trait FromBase64: Sized {
    fn from_base64(s: &str) -> Option<Self>;
}

pub trait ToBase64 {
    fn to_base64(&self) -> Option<String>;
}

impl<T: DeserializeOwned> FromBase64 for T {
    fn from_base64(s: &str) -> Option<Self> {
        base64::engine::general_purpose::STANDARD
            .decode(s)
            .ok()
            .and_then(|bytes| postcard::from_bytes(&bytes).ok())
    }
}

impl<T: Serialize> ToBase64 for T {
    fn to_base64(&self) -> Option<String> {
        postcard::to_allocvec(self)
            .ok()
            .map(|bytes| base64::engine::general_purpose::STANDARD.encode(&bytes))
    }
}

#[cfg(all(not(target_arch = "wasm32"), feature = "runtime", feature = "egui"))]
mod host;

#[cfg(all(not(target_arch = "wasm32"), feature = "runtime", feature = "egui"))]
pub use host::*;

#[cfg(target_arch = "wasm32")]
mod client;

#[cfg(target_arch = "wasm32")]
pub use client::*;
