"use strict";

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const either = require("@validatem/either");
const isString = require("@validatem/is-string");

const node = require("../ast-node");

module.exports = function (operations) {
	const isValueExpression = require("../validators/operations/is-value-expression")(operations);
	const isObjectType = require("../validators/operations/is-object-type")(operations);
	const wrapWithOperation = require("../validators/operations/wrap-with-operation")(operations);

	return function alias(_name, _expression) {
		let [ name, expression ] = validateArguments(arguments, {
			name: [ required, either([
				[ isObjectType("columnName") ],
				[ isString, wrapWithOperation("column") ]
			])],
			expression: [ required, isValueExpression ]
		});

		return node({ type: "alias", column: name, expression: expression });
	};
};
