"use strict";

const { validateArguments } = require("@validatem/core");
const required = require("@validatem/required");
const anyProperty = require("@validatem/any-property");

const node = require("../../ast-node");
const isLocalFieldName = require("../../validators/is-local-field-name");

// NOTE: `withRelations` structurally functions like a `compute`, except the computation value is a has/belongsTo relation specifier, which gets resolved at query time
// FIXME: Actually implement relation fetching logic. Start by generating relational queries after retrieving the initial data, eventually change this to pre-compute most of the relational queries and leave placeholders for table/column/etc. names. Implement the query generation as a stand-alone "generate relational query" function that takes in the current DB schema + relational specifier (+ clauses).

module.exports = function (operations) {
	const isObjectType = require("../../validators/operations/is-object-type")(operations);

	return function withRelations(_items) {
		let [ items ] = validateArguments(arguments, {
			items: [ required, anyProperty({
				key: [ required, isLocalFieldName ], // FIXME: Support dot-path notation for nested relation specification? Or let this be handled by a relation's clauses?
				value: [ required, isObjectType("relation") ]
			})]
		});

		return node({
			type: "withRelations",
			items: items
		});
	};
};
