use gloo_net::{http::Request, Error};
use log::info;

const BASE_URL: &str = "/api";
pub fn refresh() -> String {
    format!("{BASE_URL}/refresh")
}
pub mod urls {
    use seed::Url;
    pub fn search(query: &str, page: usize) -> Url {
        let query = Url::encode_uri_component(query);
        if page > 0 {
            Url::new().set_hash_path(["s", &query, &format!("p{page}")])
        } else {
            Url::new().set_hash_path(["s", &query])
        }
    }
    pub fn thread(tid: &str) -> Url {
        Url::new().set_hash_path(["t", tid])
    }
}

pub async fn refresh_request() -> Result<(), Error> {
    let t = Request::get(&refresh()).send().await?.text().await?;
    info!("refresh {t}");
    Ok(())
}
