"use strict";

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

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

const isCollectionName = require("../../validators/is-collection-name");

module.exports = function (operations) {
	const isObjectType = require("../../validators/operations/is-object-type")(operations);
	const isFieldsObject = require("../../validators/operations/schema/is-fields-object")(operations);

	return function changeCollection(_name, _operations) {
		let [ name, collectionOperations ] = validateArguments(arguments, {
			name: [ required, isCollectionName ],
			operations: [ required, either([
				[ isFieldsObject, (fieldsObject) => {
					return [ operations.fields(fieldsObject) ];
				}],
				arrayOf(either([
					isObjectType("schemaFields"),
					isObjectType("schemaIndexes")
				]))
			]) ]
		});

		return node({
			type: "changeCollectionCommand",
			name: name,
			operations: collectionOperations
		});
	};
};
