//
//  MapUtils.cpp
//  eagle0
//
//  Created by Dan Crosby on 4/8/18.
//  Copyright © 2018 none. All rights reserved.
//

#include "MapUtils.hpp"

#include <algorithm>

static inline std::string StringForKey(
        const std::unordered_map<std::string, std::string>& map,
        const std::string& key);

std::string StringForKey(
        const std::unordered_map<std::string, std::string>& map,
        const std::string& key) {
    try {
        return map.at(key);
    } catch (const std::out_of_range& e) {
        throw std::out_of_range("key " + key + " not found (" + e.what() + ")");
    }
}

double DoubleForKey(
        const std::unordered_map<std::string, std::string>& map,
        const std::string& key) {
    return std::stod(StringForKey(map, key));
}

int IntForKey(const std::unordered_map<std::string, std::string>& map, const std::string& key) {
    return std::stoi(StringForKey(map, key));
}

bool BoolForKey(const std::unordered_map<std::string, std::string>& map, const std::string& key) {
    std::string value = StringForKey(map, key);
    if (value.length() < 4) return false;
    std::transform(value.begin(), value.begin() + 4, value.begin(), ::tolower);

    return value == "true";
}
