"use strict";

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

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

module.exports = function (operations) {
	const isIndexClause = require("../validators/operations/is-index-clause")(operations);
	const isField = require("../validators/operations/is-field")(operations);

	return function _index(_options) {
		let { indexType, field, clauses } = validateOptions(arguments, {
			indexType: [ required, oneOf([ "index", "unique", "primaryKey" ]) ],
			field: [ required, isField ],
			clauses: [ arrayOf(isIndexClause) ]
		});

		return node({
			type: "index",
			indexType: indexType,
			isComposite: false,
			field: field,
			clauses: clauses
		});
	};
};
