use serde::de::DeserializeOwned;
use tauri::{
    plugin::{PluginApi, PluginHandle},
    AppHandle, Runtime,
};

use crate::models::*;

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_ios_shared_token);

// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<IosSharedToken<R>> {
    let handle = api.register_ios_plugin(init_plugin_ios_shared_token)?;
    Ok(IosSharedToken(handle))
}

/// Access to the ios-shared-token APIs.
pub struct IosSharedToken<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> IosSharedToken<R> {
    pub fn save_token(&self, payload: SaveTokenRequest) -> crate::Result<SaveTokenResponse> {
        self.0
            .run_mobile_plugin("save_token", payload)
            .map_err(Into::into)
    }
}
