"use strict";

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const defaultTo = require("@validatem/default-to");
const nestedArrayOf = require("@validatem/nested-array-of");
const flatten = require("../../validators/flatten");

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

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

	return function belongsTo(_field, _clauses) {
		let [ field, clauses ] = validateArguments(arguments, {
			field: [ required, isField ],
			clauses: [ defaultTo([]), nestedArrayOf(isRelationClause), flatten ]
		});

		return node({
			type: "relation",
			relationType: "belongsTo",
			field: field,
			clauses: clauses
		});
	};
};
