"use strict";

const either = require("@validatem/either");
const wrapError = require("@validatem/wrap-error");

const isLiteralValue = require("../is-literal-value");

// NOTE: This validator should typically come last in an `either`, since it will catch various types of inputs (sqlExpression, literal values, etc.) that might need to be interpreted differently in specific contexts.
// FIXME: Rename valueExpression -> value?

module.exports = function (operations) {
	const isObjectType = require("./is-object-type")(operations);
	const isAnyFieldObject = require("./is-any-field-object")(operations);
	const isAggregrateFunction = require("./is-aggregrate-function")(operations);
	const wrapWithOperation = require("./wrap-with-operation")(operations);

	return wrapError("Must be a type of value", either([
		[ isObjectType("sqlExpression") ],
		[ isObjectType("literalValue") ],
		[ isObjectType("placeholder") ], // TODO: Verify that this also works for the `alias` method
		[ isAggregrateFunction ], // FIXME: Make sure to check that this is only permitted when collapsing
		[ isAnyFieldObject ],
		[ isLiteralValue, wrapWithOperation("value") ]
	]));
};
