use std::io::Result;

use urlencoding::decode;

use crate::utils::{Values, merge};

pub fn clean_url(uri: String) -> String {
    if uri == "/" {
        return "".to_string();
    }

    return uri.trim_start_matches("/")
        .trim_end_matches("/")
        .to_string();
}

pub fn uri_to_vec(uri: String) -> Vec<String> {
    return clean_url(uri).split("/").map(|x| x.to_string()).collect();
}

pub fn parse_query_params(query: &str) -> Result<Values> {
    let mut out = Values::new();

    for kv in query.split('&') {
        if kv.is_empty() {
            continue;
        }

        let mut it = kv.splitn(2, '=');
        let k = it.next().unwrap_or("");
        let v = it.next().unwrap_or("");

        out.insert(
            decode(k).unwrap().to_string(), 
            decode(v).unwrap().to_string()
        );
    }

    Ok(out)
} 

pub fn join_paths(one: String, two: String) -> Vec<String> {
    return merge(vec![vec![one], vec![two]]).iter()
        .map(|x| clean_url(x.to_owned()))
        .filter(|x| x != "")
        .collect();
}