package com.brettonw.bag.expr;

import com.brettonw.bag.BagObject;

import java.util.HashMap;
import java.util.Map;

public class Exprs {
    @FunctionalInterface
    interface ExprSupplier {
        Expr get (BagObject params);
    }

    private static final Map<String, ExprSupplier> exprSuppliers = new HashMap<> ();

    public static void register (String name, ExprSupplier exprSupplier) {
        exprSuppliers.put (name, exprSupplier);
    }

    public static Expr get (Object expr) {
        if (expr != null) {
            BagObject bagObject = null;
            if (expr instanceof BagObject) {
                bagObject = (BagObject) expr;
            } else if (expr instanceof String){
                bagObject = new BagObject ().put (Expr.OPERATOR, Value.VALUE).put (Value.VALUE, expr);
            }
            ExprSupplier exprSupplier = exprSuppliers.get (bagObject.getString (Expr.OPERATOR));
            return (exprSupplier != null) ? exprSupplier.get (bagObject) : null;
        }
        return null;
    }

    static {
        register ("=", Equality::new);
        register ("key", Key::new);
        register ("value", Value::new);
    }

    public static BooleanExpr equality (String key, Object value) {
        return (BooleanExpr) get (Equality.bag (Key.bag (key), Value.bag (value)));
    }

    public static BooleanExpr inequality (String key, Object value) {
        return (BooleanExpr) get (Not.bag (Equality.bag (Key.bag (key), Value.bag (value))));
    }
}

@@ -1063,11 +1063,25 @@
 er (
-%22=%22
+Equality.EQUALITY
 , Eq
@@ -1116,13 +1116,53 @@
 er (
-%22key%22
+Not.NOT, Not::new);%0A        register (Key.KEY
 , Ke
@@ -1192,15 +1192,19 @@
 er (
-%22v
+V
 alue
-%22
+.VALUE
 , Va
@@ -1210,24 +1210,97 @@
 alue::new);%0A
+        register (And.AND, And::new);%0A        register (Or.OR, Or::new);%0A
     %7D%0A%0A    p

update so that all of the query expressions are exposed