import { Handler, Hono, MiddlewareHandler } from "hono";
import { Variables } from "../../types/HonoTypes";

/**
 * Create a route.
 *
 * This method exists to serve the purpose of allowing us to split all of our api routes into different files and
 * easily and quickly be able to rope them back into the main api server instance.
 *
 * One of the sortfallings of this method is that I was not able to figure out how to integrate the middleware to come
 * before the handler, so if somebody feels upto it please figure out a way to have the middleware come before the handler
 * method naturally as you would if you using a plain hono method.
 *
 * @TODO Move middleware handlers to come before primary handler naturally.
 *
 * @param method - HTTP Method
 * @param path - URL Path
 * @param handler - Handler function for Hono
 * @param middleware - Array of Middleware Handlers for Hono
 * @returns {Hono} - A new Hono instance containing the newly created route.
 */
export function createRoute(
  method: string | string[],
  path: string[],
  handler: Handler<{
    Variables: Variables;
  }>,
  ...middleware: MiddlewareHandler<{
    Variables: Variables;
  }>[]
): Hono<{ Variables: Variables }> {
  if (middleware)
    return new Hono<{ Variables: Variables }>().on(
      method as any,
      path,
      ...middleware,
      handler
    );
  return new Hono<{ Variables: Variables }>().on(method as any, path, handler);
}
