use tauri::{
    plugin::{Builder, TauriPlugin},
    Manager, Runtime,
};

pub use models::*;

mod mobile;

mod commands;
mod error;
mod models;

pub use error::{Error, Result};

use mobile::IosSharedToken;

/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the ios-shared-token APIs.
pub trait IosSharedTokenExt<R: Runtime> {
    fn ios_shared_token(&self) -> &IosSharedToken<R>;
}

impl<R: Runtime, T: Manager<R>> crate::IosSharedTokenExt<R> for T {
    fn ios_shared_token(&self) -> &IosSharedToken<R> {
        self.state::<IosSharedToken<R>>().inner()
    }
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
    Builder::new("ios-shared-token")
        .invoke_handler(tauri::generate_handler![commands::save_token])
        .setup(|app, api| {
            let ios_shared_token = mobile::init(app, api)?;
            app.manage(ios_shared_token);
            Ok(())
        })
        .build()
}
