"use strict";

const either = require("@validatem/either");
const wrapError = require("@validatem/wrap-error");
const anyProperty = require("@validatem/any-property");
const required = require("@validatem/required");
const ensureArray = require("@validatem/ensure-array");
const arrayOf = require("@validatem/array-of");
const isLocalFieldName = require("../../is-local-field-name");

module.exports = function (operations) {
	const isObjectType = require("../is-object-type")(operations);
	const isFieldType = require("./is-field-type")(operations);
	const isLocalIndexType = require("./is-local-index-type")(operations);
	
	let isItem = either([
		isFieldType,
		isLocalIndexType,
		isObjectType("linkTo"),
		isObjectType("defaultTo"),
		isObjectType("optional"),
		// FIXME: Only allow deleteField/restoreAs for changeCollection, not createCollection
		isObjectType("deleteField")
	]);

	return wrapError("Must be an object of schema fields", anyProperty({
		key: [ required, isLocalFieldName ],
		value: [ required, ensureArray, arrayOf(isItem) ]
	}), { preserveOriginalErrors: true });
};
