"use strict";

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

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

module.exports = function (operations) {
	const isObjectType = require("../../validators/operations/is-object-type")(operations);
	const isPossiblyRemoteField = require("../../validators/operations/is-possibly-remote-field")(operations);
	const wrapWithOperation = require("../../validators/operations/wrap-with-operation")(operations);

	return function through(_relations) {
		let [ relations ] = validateArguments(arguments, {
			relations: [ required, arrayOf(either([
				[ isObjectType("relation") ],
				[ isPossiblyRemoteField, either([
					[ isObjectType("field"), wrapWithOperation("belongsTo") ],
					[ isObjectType("remoteField"), wrapWithOperation("has") ],
				])]
			]))]
		});

		return node({
			type: "throughRelations",
			relations: relations
		});
	};
};
