//! WASM configuration wrapper.

#![cfg(target_arch = "wasm32")]
#![allow(clippy::future_not_send)]
#![allow(clippy::use_self)]

use wasm_bindgen::prelude::*;

use super::error::to_js_error;
use crate::config::SDKConfig;

/// JavaScript-friendly SDK configuration.
#[wasm_bindgen]
#[derive(Clone)]
pub struct WasmConfig {
    pub(crate) inner: SDKConfig,
}

#[wasm_bindgen]
impl WasmConfig {
    /// Create a new configuration with the IDP endpoint and client ID.
    ///
    /// # Arguments
    ///
    /// * `idp_endpoint` - The IDP endpoint URL (e.g., `https://human.lastid.co`)
    /// * `client_id` - OAuth client ID for authentication
    #[wasm_bindgen(constructor)]
    pub fn new(idp_endpoint: &str, client_id: &str) -> Result<WasmConfig, JsError> {
        let inner = SDKConfig::builder()
            .idp_endpoint(idp_endpoint)
            .client_id(client_id)
            .build()
            .map_err(to_js_error)?;
        Ok(WasmConfig { inner })
    }

    /// Create configuration with custom settings.
    ///
    /// # Arguments
    ///
    /// * `idp_endpoint` - The IDP endpoint URL
    /// * `client_id` - OAuth client ID for authentication
    /// * `trust_registry_endpoint` - Optional custom trust registry endpoint
    /// * `timeout_seconds` - Optional HTTP timeout in seconds
    #[wasm_bindgen(js_name = withOptions)]
    pub fn with_options(
        idp_endpoint: &str,
        client_id: &str,
        trust_registry_endpoint: Option<String>,
        timeout_seconds: Option<u64>,
    ) -> Result<WasmConfig, JsError> {
        let mut builder = SDKConfig::builder()
            .idp_endpoint(idp_endpoint)
            .client_id(client_id);

        if let Some(tr) = trust_registry_endpoint {
            builder = builder.trust_registry_endpoint(tr);
        }
        if let Some(t) = timeout_seconds {
            builder = builder.timeout_seconds(t);
        }

        let inner = builder.build().map_err(to_js_error)?;
        Ok(WasmConfig { inner })
    }
}
