#include "JsonLogic.h"

#include <unordered_set>

namespace json_logic
{
    Json JsonLogic::operation_data_missing(JsonArrayView values, JsonView data) const
    {
        if (values.size() == 0)
            return Fail(__FUNCTION__, "Expected 1 or more arguments, but received 0");

        Json result;
        auto missing = result.to<ArduinoJson::JsonArray>();
        std::unordered_set<std::string> seen;

        for (JsonView value : values)
        {
            if (!value.is<const char*>()) continue;
            const std::string key = value.as<const char*>();
            Json resolved;
            if (!TryResolveVariable(value, data, resolved) && seen.insert(key).second)
                missing.add(key);
        }
        return result;
    }
}
