use_prelude!();

#[derive(Debug)]
/// Provides information about the changes that have occurred in relation to an event delivered when
/// observing the collections in a `DittoStore`. It contains information about the collections that
/// are known about as well as the collections that were previously known about in the previous
/// event, along with information about what collections have been inserted, deleted, updated, or
/// moved since the last event.
pub struct CollectionsEvent {
    pub is_initial: bool,
    pub collections: Vec<Collection>,
    pub old_collections: Vec<Collection>,
    pub insertions: Box<[usize]>,
    pub deletions: Box<[usize]>,
    pub updates: Box<[usize]>,
    pub moves: Vec<LiveQueryMove>,
}

impl CollectionsEvent {
    /// Create a new CollectionsEvent in initial state.
    pub fn initial(collections: Vec<Collection>) -> Self {
        CollectionsEvent {
            is_initial: true,
            collections,
            old_collections: vec![],
            insertions: vec![].into(),
            deletions: vec![].into(),
            updates: vec![].into(),
            moves: vec![],
        }
    }

    /// Update a CollectionsEvent with provided data.
    pub fn update(
        collections: Vec<Collection>,
        old_collections: Vec<Collection>,
        insertions: Box<[usize]>,
        deletions: Box<[usize]>,
        updates: Box<[usize]>,
        moves: Vec<LiveQueryMove>,
    ) -> Self {
        CollectionsEvent {
            is_initial: false,
            collections,
            old_collections,
            insertions,
            deletions,
            updates,
            moves,
        }
    }
}
