use crate::edit::NotebookDocument;
use crate::server::Result;
use crate::server::api::LSPResult;
use crate::server::api::diagnostics::publish_diagnostics_for_document;
use crate::session::{Client, Session};
use lsp_server::ErrorCode;
use lsp_types::{self as types, DidOpenNotebookDocumentNotification};

pub(crate) struct DidOpenNotebook;

impl super::NotificationHandler for DidOpenNotebook {
    type NotificationType = DidOpenNotebookDocumentNotification;
}

impl super::SyncNotificationHandler for DidOpenNotebook {
    fn run(
        session: &mut Session,
        client: &Client,
        types::DidOpenNotebookDocumentParams {
            notebook_document:
                types::NotebookDocument {
                    uri,
                    version,
                    cells,
                    metadata,
                    ..
                },
            cell_text_documents,
        }: types::DidOpenNotebookDocumentParams,
    ) -> Result<()> {
        let notebook = NotebookDocument::new(
            version,
            cells,
            metadata.unwrap_or_default(),
            cell_text_documents,
        )
        .with_failure_code(ErrorCode::InternalError)?;

        session.open_notebook_document(uri.clone(), notebook);

        // publish diagnostics
        let snapshot = session
            .take_snapshot(uri)
            .expect("snapshot should be available");
        publish_diagnostics_for_document(&snapshot, client)?;

        Ok(())
    }
}
