use camino::Utf8PathBuf;

use crate::event_loop::ExtensionState;
use crate::event_loop::js_function::Functions;

#[tracing::instrument(skip(extension_state, js_functions))]
pub async fn handle(
    repository_path: Utf8PathBuf,
    extension_state: &ExtensionState,
    js_functions: &Functions,
) {
    let Some(repository) = extension_state.repositories.get(&repository_path) else {
        tracing::error!(message = "No repository found", ?repository_path);
        return;
    };

    if let Err(error) = js_functions
        .update_resource_states(
            repository_path,
            repository.repository.iter_path_states().collect(),
            &repository.unrecorded_changes,
            &repository.untracked_paths,
        )
        .await
    {
        tracing::error!(message = "Failed to update resource states", ?error);
    }
}