/*
 * Hetzner Cloud API
 *
 * Copied from the official API documentation for the Public Hetzner Cloud.
 *
 * The version of the OpenAPI document: 4b3f595
 *
 * Generated by: https://openapi-generator.tech
 */

use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Configuration {
    pub base_path_mapping: HashMap<String, String>,
    pub user_agent: Option<String>,
    pub client: reqwest::Client,
    pub basic_auth: Option<BasicAuth>,
    pub oauth_access_token: Option<String>,
    pub bearer_access_token: Option<String>,
    pub api_key: Option<ApiKey>,
    // TODO: take an oauth2 token source, similar to the go one
}

pub type BasicAuth = (String, Option<String>);

#[derive(Debug, Clone)]
pub struct ApiKey {
    pub prefix: Option<String>,
    pub key: String,
}

impl<'a> Configuration {
    pub fn new() -> Configuration {
        Configuration::default()
    }

    pub fn get_default_base_path(&self) -> &str {
        self.get_base_path("https://api.hetzner.cloud/v1")
    }

    pub fn get_base_path(&'a self, url: &'a str) -> &'a str {
        if let Some(mapped) = self.base_path_mapping.get(url) {
            mapped
        } else {
            url
        }
    }
}

impl Default for Configuration {
    fn default() -> Self {
        let base_path_mapping = [
            "https://api.hetzner.cloud/v1", // Hetzner Cloud API
            "https://api.hetzner.com/v1",   // Hetzner API
        ]
        .into_iter()
        .map(|s| (s.to_owned(), s.to_owned()))
        .collect();

        Configuration {
            base_path_mapping,
            user_agent: Some("hcloud-rust/0.26.0".to_owned()),
            client: reqwest::Client::new(),
            basic_auth: None,
            oauth_access_token: None,
            bearer_access_token: None,
            api_key: None,
        }
    }
}
