"use strict";

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

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

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

	return function collapseBy(_columns) {
		let [ columns, clauses ] = validateArguments(arguments, {
			columns: [ required, arrayOf([ required, isCollapsibleColumn ]) ],
			clauses: [ arrayOf([ isCollapseByClause ]) ]
		});

		return node({
			type: "collapseBy",
			columns: node({
				type: "collapseByColumns",
				columns: columns
			}),
			clauses: clauses
		});
	};
};
