use std::collections::HashMap;

#[derive(Default, Debug)]
pub struct Store {
    chunks: HashMap<String, (String, Vec<u8>)>,
}

impl Store {
    pub fn put(
        &mut self,
        id: impl Into<String>,
        label: impl Into<String>,
        chunk: impl Into<Vec<u8>>,
    ) {
        let id = id.into();
        let label = label.into();
        let chunk = chunk.into();
        self.chunks.insert(id, (label, chunk));
    }

    pub fn get(&self, id: impl AsRef<str>) -> Option<(String, Vec<u8>)> {
        let id = id.as_ref();
        self.chunks
            .get(id)
            .map(|(label, chunk)| (label.to_string(), chunk.to_vec()))
    }

    pub fn find(&self, wanted: impl AsRef<str>) -> Vec<&str> {
        let wanted = wanted.as_ref();
        self.chunks
            .iter()
            .filter_map(|(id, (label, _chunk))| {
                if label == wanted {
                    Some(id.as_str())
                } else {
                    None
                }
            })
            .collect()
    }

    pub fn delete(&mut self, id: impl AsRef<str>) -> bool {
        let id = id.as_ref();
        self.chunks.remove(id).is_some()
    }
}

#[derive(Debug, thiserror::Error)]
pub enum StoreError {}
