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

use crate::command::Action;

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

mod key;
mod map;

pub use key::KeySeq;
use libterm::TermKey;

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

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

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

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

pub fn bindn<I: Into<KeySeq>, V: IntoIterator<Item = Action>>(
    key: I,
    actions: V,
) -> (KeySeq, Vec<Action>) {
    (key.into(), actions.into_iter().collect())
}

pub fn bind1<I: Into<TermKey>, V: IntoIterator<Item = Action>>(
    key: I,
    actions: V,
) -> (KeySeq, Vec<Action>) {
    (KeySeq::one(key), actions.into_iter().collect())
}

#[cfg(test)]
mod tests {
    #[test]
    fn from_iter() {}
}
