#include "JsonLogic.h"

namespace json_logic
{
    Json JsonLogic::operation_numeric_subtraction(JsonArrayView values, JsonView data) const
    {
        if (values.size() == 0 || values.size() > 2)
            return Fail(__FUNCTION__, "Expected 1 or 2 arguments, but received " +
                                          std::to_string(values.size()));
        const Json lhs = CastToNumber(ApplyInternal(values[0], data));
        if (HasError()) return Json{};
        if (values.size() == 1) return MakeNumber(-ToDouble(lhs));

        const Json rhs = CastToNumber(ApplyInternal(values[1], data));
        if (HasError()) return Json{};
        return MakeNumber(ToDouble(lhs) - ToDouble(rhs));
    }
}
