"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HotRoute = void 0;
const HotRouteMethod_1 = require("./HotRouteMethod");
/**
 * The route to use.
 */
class HotRoute {
    constructor(connection, route, methods = []) {
        /**
         * Executes before all routes have been registered.
         */
        this.onPreRegister = null;
        /**
         * Executes when first registering this route with Express. If
         * this returns false, the route will not be registered.
         */
        this.onRegister = null;
        /**
         * Executes after all routes have been registered.
         */
        this.onPostRegister = null;
        /**
         * Executes when authorizing a called method.
         * The value returned from here will be passed to onExecute in the
         * called HotRouteMethod. Undefined returning from here will mean
         * the authorization failed.
         */
        this.onAuthorizeUser = null;
        this.connection = connection;
        this.logger = null;
        if (this.connection != null) {
            if (this.connection.processor != null)
                this.logger = this.connection.processor.logger;
        }
        this.route = route;
        this.description = "";
        this.openAPI = null;
        this.version = "v1";
        this.prefix = "";
        this.methods = methods;
        this.errors = {
            "not_authorized": HotRoute.createError("Not authorized."),
            "no_server_execute_function": HotRoute.createError("Missing server execute function.", 500),
        };
    }
    /**
     * Create an error JSON object.
     */
    static createError(message, errorCode = 400) {
        return ({ error: message, errorCode: errorCode });
    }
    /**
     * Add an API method to this route.
     *
     * @param method The name of the method to add. If a HotRouteMethod is supplied, the
     * rest of the arguments supplied will be ignored.
     */
    addMethod(method, executeFunction = null, type = HotRouteMethod_1.HotEventMethod.POST, testCases = null) {
        if (typeof (method) === "string")
            method = HotRouteMethod_1.HotRouteMethod.create(this, method, executeFunction, type, null, null, testCases);
        if (method instanceof HotRouteMethod_1.HotRouteMethod)
            this.methods.push(method);
        else {
            if (method.route == null)
                method.route = this;
            method = HotRouteMethod_1.HotRouteMethod.create(method);
            this.methods.push(method);
        }
        return (method);
    }
    /**
     * Get a method by it's name.
     */
    getMethod(name) {
        let foundMethod = null;
        for (let iIdx = 0; iIdx < this.methods.length; iIdx++) {
            let method = this.methods[iIdx];
            if (method.name === name) {
                foundMethod = method;
                break;
            }
        }
        return (foundMethod);
    }
}
exports.HotRoute = HotRoute;
//# sourceMappingURL=HotRoute.js.map