import express, { Request, Response, NextFunction } from "express";

// eslint-disable-next-line new-cap
const router = express.Router();

export function routeNormal(method, route, svc, func, args) {
	router[method](
		route,
		async function (req: Request, res: Response, next: NextFunction) {
			const arg1 = _.has(args[0], args[0][0][args[0][1]]);
			const arg2 = _.has(args[1], args[1][0][args[1][1]]);
			const arg3 = _.has(args[2], args[2][0][args[2][1]]);

			try {
				// res.json(await itemsService.remove(req.params.id));
				res.json(await svc[func](arg1, arg2, arg3));
			} catch (err) {
				console.error(`  Error while processing: `, err.message);

				next(err);
			}
		},
	);
}

export function errorHandler(route, err, next) {
	console.error(`  Error in path ${route}: `, err.message);
	next(err);
}
