use rapira::Rapira;
use strum::IntoStaticStr;

pub mod builder;
pub mod db;
pub mod raw_tree;
pub mod tree;

pub type ByteValue = fjall::Slice;
pub type KeyValue = (ByteValue, ByteValue);

pub enum MaybeParIter {
    Seq,
    Par(rayon::vec::IntoIter<(GroupBound, GroupBound)>),
    Empty,
}

type GroupBound = core::ops::Bound<[u8; 4]>;

#[derive(Debug, Clone, Rapira, IntoStaticStr)]
pub enum ChangeEvent {
    Upsert(KeyValue),
    /// delete by id
    Delete(ByteValue),
}
impl ChangeEvent {
    pub fn variant(&self) -> &'static str {
        self.into()
    }

    pub fn key(&self) -> &ByteValue {
        match self {
            ChangeEvent::Upsert(kv) => &kv.0,
            ChangeEvent::Delete(key) => key,
        }
    }

    pub fn from_kv(key: ByteValue, val: Option<ByteValue>) -> Self {
        match val {
            Some(val) => ChangeEvent::Upsert((key, val)),
            None => ChangeEvent::Delete(key),
        }
    }
}
