"use strict";

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

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

module.exports = function (operations) {
	const isObjectType = require("../validators/operations/is-object-type")(operations);
	const isSelectClause = require("../validators/operations/is-select-clause")(operations);
	const wrapWithOperation = require("../validators/operations/wrap-with-operation")(operations);
	
	return function select(_collection, _clauses) {
		let [ collection, clauses ] = validateArguments(arguments, {
			collection: [ required, either([
				[ isObjectType("collection") ],
				[ isString, wrapWithOperation("collection") ]
			])],
			clauses: [ required, nestedArrayOf(isSelectClause), flatten ]
		});

		return node({ type: "select", collection: collection, clauses: clauses });
	};
};
