"use strict";

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");

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

module.exports = function count(operations) {
	const isTable = require("../validators/operations/is-table")(operations);

	return function count(_table) {
		let [ table ] = validateArguments(arguments, {
			table: [ isTable ]
		});

		// TODO: Investigate whether this can be made more performant by counting a specific column rather than *. That would probably require stateful knowledge of the database schema, though.
		let columnReference = (table != null)
			? operations.foreignColumn({ table: table, column: "*" }) // FIXME: Make sure not to break this (internally) when making column name checks more strict
			: operations.column("*");

		return node({
			type: "aggregrateFunction",
			functionName: "count",
			args: [ columnReference ]
		});
	};
};
