use gents_derives::TS;
use logisheets_base::{CellId, SheetId};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, TS)]
#[ts(file_name = "shadow_kind.ts", rename_all = "camelCase")]
pub enum ShadowKind {
Validation,
UserEditable,
ConditionalFormat,
ConditionalFormatScale,
}
impl Default for ShadowKind {
fn default() -> Self {
ShadowKind::Validation
}
}
#[derive(Debug)]
pub struct ShadowIdAssigner {
next_id: u64,
eid_to_cell_kind: HashMap<u64, (SheetId, CellId, ShadowKind)>,
cell_kind_to_eid: HashMap<(SheetId, CellId, ShadowKind), u64>,
}
impl ShadowIdAssigner {
pub fn new() -> Self {
Self {
next_id: u32::MAX as u64,
eid_to_cell_kind: HashMap::new(),
cell_kind_to_eid: HashMap::new(),
}
}
pub fn find_shadow_id(
&self,
sheet_id: SheetId,
cell_id: CellId,
kind: ShadowKind,
) -> Option<u64> {
self.cell_kind_to_eid
.get(&(sheet_id, cell_id, kind))
.cloned()
}
pub fn get_shawdow_id(&mut self, sheet_id: SheetId, cell_id: CellId, kind: ShadowKind) -> u64 {
if let Some(id) = self.cell_kind_to_eid.get(&(sheet_id, cell_id, kind)) {
*id
} else {
let id = self.next_id;
self.next_id += 1;
self.eid_to_cell_kind.insert(id, (sheet_id, cell_id, kind));
self.cell_kind_to_eid.insert((sheet_id, cell_id, kind), id);
id
}
}
pub fn get_cell_id(&self, eid: u64) -> Option<(SheetId, CellId)> {
self.eid_to_cell_kind.get(&eid).map(|(s, c, _k)| (*s, *c))
}
pub fn get_cell_kind(&self, eid: u64) -> Option<(SheetId, CellId, ShadowKind)> {
self.eid_to_cell_kind.get(&eid).cloned()
}
}