"use strict";

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const arrayOf = require("@validatem/array-of");

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

// FIXME: Allow single field that gets auto-array-wrapped?

module.exports = function (operations) {
	const isCollapsibleField = require("../validators/operations/is-collapsible-field")(operations);
	const isCollapseByClause = require("../validators/operations/is-collapse-by-clause")(operations);

	return function collapseBy(_fields) {
		let [ fields, clauses ] = validateArguments(arguments, {
			fields: [ required, arrayOf([ required, isCollapsibleField ]) ],
			clauses: [ arrayOf([ isCollapseByClause ]) ]
		});

		return node({
			type: "collapseBy",
			fields: node({
				type: "collapseByFields",
				fields: fields
			}),
			clauses: clauses
		});
	};
};
