"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 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 isIndexType = require("./is-index-type")(operations);
	
	return wrapError("Must be an object of schema fields", anyProperty({
		key: [ required, isLocalFieldName ],
		value: [ required, either([
			isFieldType,
			isIndexType,
			isObjectType("belongsTo"), // FIXME: Correct type? Need to distinguish between reference and definition usage (local vs. remote field name)
			isObjectType("defaultTo"),
			isObjectType("optional"),
			// FIXME: Only allow deleteField/restoreAs for changeCollection, not createCollection
			isObjectType("deleteField"),
			isObjectType("restoreAs"),
		])]
	}), { preserveOriginalErrors: true });
};
