"use strict";

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

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

module.exports = function (_operations) {
	return function remoteField(_descriptor) {
		let [ descriptor ] = validateArguments(arguments, {
			descriptor: [ required, either([
				[ isRemoteFieldName ], // NOTE: This returns a parsed version of the name
				// FIXME: Instruct users to use the below format specifically when dynamically specifying a remote field, rather than string-concatenating
				[{
					collection: [ required, isCollectionName ],
					field: [ required, isLocalFieldName ]
				}]
			])]
		});

		return node({
			type: "remoteField",
			collectionName: descriptor.collection,
			fieldName: descriptor.field
		});
	};
};
