// file: kb_lib/src/db/types/liquidity_event_kind.rs

//! Liquidity event kind.

/// Normalized liquidity event kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum LiquidityEventKind {
    /// Liquidity addition.
    Add,
    /// Liquidity removal.
    Remove,
    /// Concentrated-liquidity position opening without a guaranteed amount delta.
    PositionOpen,
    /// Concentrated-liquidity position closing without a guaranteed amount delta.
    PositionClose,
}

impl LiquidityEventKind {
    /// Converts the event kind to its stable integer representation.
    pub fn to_i16(self) -> i16 {
        match self {
            Self::Add => return 0,
            Self::Remove => return 1,
            Self::PositionOpen => return 2,
            Self::PositionClose => return 3,
        }
    }

    /// Restores an event kind from its stable integer representation.
    pub fn from_i16(value: i16) -> Result<Self, crate::Error> {
        match value {
            0 => return Ok(Self::Add),
            1 => return Ok(Self::Remove),
            2 => return Ok(Self::PositionOpen),
            3 => return Ok(Self::PositionClose),
            _ => {
                return Err(crate::Error::Db(format!(
                    "invalid LiquidityEventKind value: {}",
                    value
                )));
            },
        }
    }
}
