use alloy::primitives::Bytes;
use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WasmProvider {
    pub url: String,
    pub args: Bytes,
}

impl WasmProvider {
    pub fn new(url: String, args: Bytes) -> Self {
        Self { url, args }
    }

    pub fn from_url(url: &str) -> Self {
        Self {
            url: url.to_string(),
            args: Bytes::new(),
        }
    }

    pub fn with_args(url: &str, args: impl Into<Bytes>) -> Self {
        Self {
            url: url.to_string(),
            args: args.into(),
        }
    }

    pub fn args_as_str(&self) -> String {
        if self.args.is_empty() {
            "{}".to_string()
        } else {
            String::from_utf8_lossy(&self.args).to_string()
        }
    }
}

impl fmt::Display for WasmProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "wasm:{}", self.url)
    }
}

impl From<&str> for WasmProvider {
    fn from(url: &str) -> Self {
        Self::from_url(url)
    }
}
