"use strict";

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

const isForeignColumnString = require("../validators/is-foreign-column-string");
const node = require("../ast-node");

module.exports = function (_operations) {
	return function foreignColumn(_descriptor) {
		let [ descriptor ] = validateArguments(arguments, {
			descriptor: [ required, either([
				[ isForeignColumnString ],
				// FIXME: Instruct users to use the below format specifically when dynamically specifying a foreign column, rather than string-concatenating
				[{
					table: [ required, isString ],
					column: [ required, isString ]
				}]
			])]
		});

		return node({
			type: "foreignColumnName",
			tableName: descriptor.table,
			columnName: descriptor.column
		});
	};
};
