/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{Object, Tag, TypedTag};
use std::{fmt::Display, str::FromStr};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path(pub Vec<Tag>);

impl Path {
    pub fn get_matching_value<'a>(&self, ob: &'a Object) -> Option<&'a [u8]> {
        fn recurse<'a>(ob: &'a Object, path: &[Tag]) -> Option<&'a [u8]> {
            if path.len() > 1 {
                recurse(ob.get(TypedTag::<Object>::new(path[0]))?, &path[1..])
            } else {
                ob.get(TypedTag::<[u8]>::new(path[0]))
            }
        }
        recurse(ob, &self.0)
    }
    pub fn get_matching_values<'a>(&self, ob: &'a Object) -> Vec<&'a [u8]> {
        fn recurse<'a>(ob: &'a Object, out: &mut Vec<&'a [u8]>, path: &[Tag]) {
            if path.len() > 1 {
                for nested in ob.iter(TypedTag::<Object>::new(path[0])) {
                    recurse(nested, out, &path[1..]);
                }
            } else {
                out.extend(ob.iter(TypedTag::<[u8]>::new(path[0])));
            }
        }
        let mut out = Vec::new();
        recurse(ob, &mut out, &self.0);
        out
    }
}

impl Display for Path {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (i, c) in self.0.iter().enumerate() {
            if i > 0 {
                write!(f, ".")?;
            }
            write!(f, "{c}")?;
        }
        Ok(())
    }
}
impl FromStr for Path {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self(
            s.split(".")
                .map(|e| {
                    e.as_bytes()
                        .try_into()
                        .map_err(|_| "path component not 4 bytes")
                        .map(Tag::new)
                })
                .collect::<Result<Vec<Tag>, &'static str>>()?,
        ))
    }
}
