use std::{borrow::Borrow, hash::Hash};

use self::map::PrefixMap;
pub use self::map::PrefixNode;

mod key;
mod map;

pub use key::KeySeq;

#[derive(Debug)]
pub struct KeyMap<T> {
    map: PrefixMap<KeySeq, T>,
}

impl<T> KeyMap<T> {
    pub fn new() -> Self {
        Self {
            map: PrefixMap::new(),
        }
    }

    pub fn set(&mut self, key: KeySeq, value: T) {
        self.map.insert(key, value);
    }

    pub fn get<N>(&self, key: &N) -> Option<&PrefixNode<KeySeq, T>>
    where
        KeySeq: Borrow<N>,
        N: Eq + Hash + ?Sized,
    {
        self.map.get(key)
    }
}

impl<A> FromIterator<(KeySeq, A)> for KeyMap<A> {
    fn from_iter<T: IntoIterator<Item = (KeySeq, A)>>(iter: T) -> Self {
        Self {
            map: PrefixMap::from_iter(iter),
        }
    }
}
