#include "configuration.h"
#include <stdexcept>

template <>
callable_when(unconsumed) [[nodiscard]] std::optional<QString> Configuration::value(const char *path) const
{
    const auto v = value<std::string>(path);
    return v ? std::make_optional(QString::fromStdString(v.value())) : std::nullopt;
}

template <>
callable_when(unconsumed) [[nodiscard]] std::optional<QStringList> Configuration::value(const char *path) const
{
    const auto v = value<std::string>(path);
    return v ? std::make_optional(QString::fromStdString(v.value()).split(';')) : std::nullopt;
}

template <>
callable_when(unconsumed) QAction &Configuration::shortcut(QAction &action, const char *name) const
{
    if(const auto shortcut = value<QString>(name)) {
        const QString old_tooltip = action.toolTip();
        action.setShortcut(QKeySequence::fromString(shortcut.value()));
        action.setToolTip(QString("%1 (%2)").arg(old_tooltip, shortcut.value()));
    }
    return action;
}

template <>
callable_when(unconsumed) QKeySequence &Configuration::shortcut(QKeySequence &sequence, const char *name) const
{
    if(const auto shortcut = value<QString>(name)) {
        sequence = QKeySequence::fromString(shortcut.value());
    }
    return sequence;
}