//! Hidden helpers for doc tests. Not part of the public API.

use crate::{
    fs::{DittoRoot, TempRoot},
    prelude::{DatabaseId, Ditto, DittoConfig, DittoConfigConnect},
};

/// Create a [`Ditto`] instance for synchronous doc tests using `SmallPeersOnly` mode.
///
/// Returns `(TempRoot, Ditto)` -- keep `TempRoot` alive for the duration of the test.
/// Panics on failure.
pub fn doctest_ditto() -> (TempRoot, Ditto) {
    let root = TempRoot::new();
    let config = DittoConfig::new(
        DatabaseId::generate().to_string(),
        DittoConfigConnect::SmallPeersOnly { private_key: None },
    )
    .with_persistence_directory(root.root_path());
    let ditto = Ditto::open_sync(config).expect("doctest: failed to open Ditto");
    ditto
        .set_license_from_env("DITTO_LICENSE")
        .expect("doctest: DITTO_LICENSE env var required");
    (root, ditto)
}

/// Create a [`Ditto`] instance for doc tests that require `Server` mode (auth examples).
///
/// Returns `(TempRoot, Ditto)` with a no-op expiration handler already set.
/// Panics on failure.
pub fn doctest_online_ditto() -> (TempRoot, Ditto) {
    let database_id = DatabaseId::generate();
    let root = TempRoot::new();
    let config = DittoConfig::new(
        database_id.to_string(),
        DittoConfigConnect::Server {
            url: format!("https://{}.cloud.ditto.live", database_id)
                .parse()
                .unwrap(),
        },
    )
    .with_persistence_directory(root.root_path());
    let ditto = Ditto::open_sync(config).expect("doctest: failed to open Ditto");
    ditto
        .auth()
        .expect("Server mode should have auth")
        .set_expiration_handler(async |_ditto: &Ditto, _duration_remaining| {});
    (root, ditto)
}
