#![allow(deprecated)]

use_prelude!();
use super::*;

/// Use [`pending_op.observe_local(...)`] to receive `LiveQueryEvent`s describing document
/// changes.
///
/// Describes the different types of event that you can receive when dealing with live queries.
///
/// [`pending_op.observe_local(...)`]: crate::store::query_builder::PendingCursorOperation::observe_local
#[deprecated(
    since = "4.14.4",
    note = "Will be removed in SDK 5.0. Use DQL queries instead"
)]
#[derive(Debug)]
pub enum LiveQueryEvent {
    /// The first event that will be delivered and it will only be delivered once.
    Initial,

    /// This event will be delivered each time the results of the provided query change. It
    /// contains information about the set of documents that previously matched the query
    /// before the update, along with information about what documents have been inserted,
    /// deleted, updated, or moved, as part of the set of matching documents.
    Update {
        old_documents: Vec<ffi_sdk::BoxedDocument>,
        insertions: Box<[usize]>,
        deletions: Box<[usize]>,
        updates: Box<[usize]>,
        moves: Vec<LiveQueryMove>,
    },
}

impl LiveQueryEvent {
    /// Return an hash of a document
    pub fn hash(&self, docs: &[ffi_sdk::BoxedDocument]) -> Result<u64, DittoError> {
        ffi_sdk::ditto_documents_hash(docs.into()).ok()
    }

    /// Return the hash the `Document`s mnemonic.
    pub fn hash_mnemonic(&self, docs: &[ffi_sdk::BoxedDocument]) -> Result<String, DittoError> {
        let mnemonic_c_str = { ffi_sdk::ditto_documents_hash_mnemonic(docs.into()).ok()? };
        Ok(mnemonic_c_str.into_string())
    }
}
