#include "JsonLogic.h"

namespace json_logic
{
    Json JsonLogic::operation_logic_if(JsonArrayView values, JsonView data) const
    {
        if (values.size() < 3 || values.size() % 2 == 0)
            return Fail(__FUNCTION__, "Expected 3 or more odd number of arguments, but received " +
                                          std::to_string(values.size()));

        for (std::size_t i = 0; i + 1 < values.size(); i += 2)
        {
            const Json condition = ApplyInternal(values[i], data);
            if (HasError()) return Json{};
            if (Truthy(condition)) return ApplyInternal(values[i + 1], data);
        }
        return ApplyInternal(values[values.size() - 1], data);
    }
}
