#include "application/actions/UiActionDispatcher.h"

#include <stdexcept>
#include <utility>

namespace ta::application
{
    void UiActionDispatcher::bind(std::string action_key, Handler handler)
    {
        if (action_key.empty() || !handler)
            throw std::invalid_argument("UI action binding requires a key and handler");
        if (!handlers_.emplace(std::move(action_key), std::move(handler)).second)
            throw std::logic_error("duplicate UI action handler");
    }

    bool UiActionDispatcher::dispatch(const std::string_view action_key, const CommandContext& context) const
    {
        const auto handler = handlers_.find(std::string(action_key));
        if (handler == handlers_.end())
            return false;
        handler->second(context);
        return true;
    }
}
