mod commands;

use radicle_types::AppState;

use commands::{auth, cob, diff, inbox, profile, repo, startup, thread};

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    // Raise the open-file limit, matching heartwood's binaries (which all
    // default to 4096). Listing a repo's refs for the peer selector resolves
    // every remote's signed refs through libgit2, opening a descriptor per
    // packfile it reads. Radicle storage can hold thousands of packfiles, so
    // under the low default limit (e.g. a Finder-launched bundle) these reads
    // exhaust descriptors and objects in the unopened packs spuriously fail.
    if let Err(e) = radicle::io::set_file_limit(4096) {
        log::warn!("Unable to set open file limit: {e}");
    }

    #[cfg(debug_assertions)]
    let builder = tauri::Builder::default().plugin(
        tauri_plugin_log::Builder::new()
            .level(log::LevelFilter::Info)
            .build(),
    );
    #[cfg(not(debug_assertions))]
    let builder = tauri::Builder::default();

    builder
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_shell::init())
        .plugin(tauri_plugin_clipboard_manager::init())
        .plugin(tauri_plugin_window_state::Builder::default().build())
        .invoke_handler(tauri::generate_handler![
            auth::authenticate,
            auth::init,
            cob::get_embed,
            cob::issue::activity_by_issue,
            cob::issue::comment_threads_by_issue_id,
            cob::issue::create_issue,
            cob::issue::edit_issue,
            cob::issue::issue_by_id,
            cob::issue::list_issues,
            cob::issue::rebuild_issue_cache,
            cob::job::list_jobs,
            cob::patch::activity_by_patch,
            cob::patch::delete_patch,
            cob::patch::edit_patch,
            cob::patch::list_patches,
            cob::patch::patch_by_id,
            cob::patch::create_patch_review,
            cob::patch::rebuild_patch_cache,
            cob::patch::revisions_by_patch,
            cob::patch::revisions_by_patch,
            cob::save_embed_by_bytes,
            cob::save_embed_by_clipboard,
            cob::save_embed_by_path,
            cob::save_embed_to_disk,
            diff::get_diff,
            diff::get_diff_text,
            diff::save_diff_to_disk,
            repo::get_commit_diff,
            inbox::clear_notifications,
            inbox::notification_count,
            inbox::list_notifications,
            profile::alias,
            profile::config,
            repo::diff_stats,
            repo::list_commits,
            repo::list_repo_commits,
            repo::list_repo_refs,
            repo::list_repos,
            repo::list_repos_summary,
            repo::repo_by_id,
            repo::repo_commit_count,
            repo::repo_commit,
            repo::repo_count,
            repo::repo_readme,
            repo::repo_tree,
            repo::repo_blob,
            repo::seed,
            repo::seeded_not_replicated,
            repo::unseed,
            startup::startup,
            startup::version,
            startup::check_radicle_cli,
            thread::create_issue_comment,
            thread::create_patch_comment,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
