"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.http = void 0;
exports.getControllerMetadata = getControllerMetadata;
exports.getFallbackControllerMetadata = getFallbackControllerMetadata;
exports.getRouteMetadata = getRouteMetadata;
exports.getRouteParameterResolverMetadata = getRouteParameterResolverMetadata;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const controllerMetadata = new WeakMap();
const fallbackControllerMetadata = new WeakMap();
const routeMetadata = new WeakMap();
const controllerMiddlewareMetadata = new WeakMap();
const routeMiddlewareMetadata = new WeakMap();
const routeParameterResolverMetadata = new WeakMap();
function controller(path = '') {
    const middlewares = [];
    const decorator = ((target) => {
        const Controller = target;
        controllerMetadata.set(Controller, {
            path,
            middlewares: [...(controllerMiddlewareMetadata.get(Controller) ?? []), ...middlewares]
        });
    });
    decorator.middleware = (...items) => {
        middlewares.push(...items);
        return decorator;
    };
    return decorator;
}
function fallback() {
    const middlewares = [];
    const decorator = ((target) => {
        const Controller = target;
        fallbackControllerMetadata.set(Controller, {
            middlewares: [...(controllerMiddlewareMetadata.get(Controller) ?? []), ...middlewares]
        });
    });
    decorator.middleware = (...items) => {
        middlewares.push(...items);
        return decorator;
    };
    return decorator;
}
function route(method, path = '') {
    const middlewares = [];
    const decorator = ((_target, propertyKey) => {
        const routes = routeMetadata.get(_target) ?? [];
        routes.push({
            method,
            path,
            propertyKey,
            middlewares: [...getRouteMiddlewares(_target, propertyKey), ...middlewares]
        });
        routeMetadata.set(_target, routes);
    });
    decorator.use = (...items) => {
        middlewares.push(...items);
        return decorator;
    };
    decorator.middleware = decorator.use;
    return decorator;
}
function middleware(...items) {
    return ((target, propertyKey) => {
        if (propertyKey === undefined) {
            const Controller = target;
            const existing = controllerMiddlewareMetadata.get(Controller) ?? [];
            controllerMiddlewareMetadata.set(Controller, [...existing, ...items]);
            const metadata = controllerMetadata.get(Controller);
            if (metadata)
                metadata.middlewares.push(...items);
            const fallbackMetadata = fallbackControllerMetadata.get(Controller);
            if (fallbackMetadata)
                fallbackMetadata.middlewares.push(...items);
            return;
        }
        const existing = getRouteMiddlewares(target, propertyKey);
        setRouteMiddlewares(target, propertyKey, [...existing, ...items]);
        for (const route of routeMetadata.get(target) ?? []) {
            if (route.propertyKey === propertyKey)
                route.middlewares.push(...items);
        }
    });
}
function resolveParameter(type, resolver) {
    return target => {
        const Controller = target;
        const existing = routeParameterResolverMetadata.get(Controller) ?? [];
        routeParameterResolverMetadata.set(Controller, [...existing, { type, resolver }]);
    };
}
exports.http = {
    controller,
    fallback,
    middleware,
    resolveParameter,
    GET: (path) => route('GET', path),
    POST: (path) => route('POST', path),
    PUT: (path) => route('PUT', path),
    PATCH: (path) => route('PATCH', path),
    DELETE: (path) => route('DELETE', path),
    OPTIONS: (path) => route('OPTIONS', path),
    HEAD: (path) => route('HEAD', path)
};
function getControllerMetadata(controllerClass) {
    return controllerMetadata.get(controllerClass);
}
function getFallbackControllerMetadata(controllerClass) {
    return fallbackControllerMetadata.get(controllerClass);
}
function getRouteMetadata(controllerClass) {
    return routeMetadata.get(controllerClass.prototype) ?? [];
}
function getRouteParameterResolverMetadata(controllerClass) {
    return routeParameterResolverMetadata.get(controllerClass) ?? [];
}
function getRouteMiddlewares(target, propertyKey) {
    return routeMiddlewareMetadata.get(target)?.get(propertyKey) ?? [];
}
function setRouteMiddlewares(target, propertyKey, middlewares) {
    const map = routeMiddlewareMetadata.get(target) ?? new Map();
    map.set(propertyKey, middlewares);
    routeMiddlewareMetadata.set(target, map);
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"ControllerMetadata\":{\"kind\":18,\"typeName\":\"ControllerMetadata\",\"types\":[{\"kind\":20,\"name\":\"path\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"middlewares\",\"type\":{\"kind\":14,\"type\":{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"ClassType\",\"typeArguments\":[{\"kind\":16,\"typeName\":\"HttpMiddleware\",\"classType\":{\"$tsf\":0}}]},{\"kind\":2,\"typeName\":\"(request: HttpRequest, response: HttpResponse) =\\u003e HttpMiddlewareResult\",\"typeName\":\"HttpMiddlewareFunction\"}],\"typeName\":\"HttpMiddlewareInput\"}},\"optional\":false}]},\"FallbackControllerMetadata\":{\"kind\":18,\"typeName\":\"FallbackControllerMetadata\",\"types\":[{\"kind\":20,\"name\":\"middlewares\",\"type\":{\"kind\":14,\"type\":{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"ClassType\",\"typeArguments\":[{\"kind\":16,\"typeName\":\"HttpMiddleware\",\"classType\":{\"$tsf\":0}}]},{\"kind\":2,\"typeName\":\"(request: HttpRequest, response: HttpResponse) =\\u003e HttpMiddlewareResult\",\"typeName\":\"HttpMiddlewareFunction\"}],\"typeName\":\"HttpMiddlewareInput\"}},\"optional\":false}]},\"HttpFallbackController\":{\"kind\":18,\"typeName\":\"HttpFallbackController\",\"types\":[]},\"RouteMetadata\":{\"kind\":18,\"typeName\":\"RouteMetadata\",\"types\":[{\"kind\":20,\"name\":\"method\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"GET\"},{\"kind\":10,\"literal\":\"POST\"},{\"kind\":10,\"literal\":\"PUT\"},{\"kind\":10,\"literal\":\"PATCH\"},{\"kind\":10,\"literal\":\"DELETE\"},{\"kind\":10,\"literal\":\"OPTIONS\"},{\"kind\":10,\"literal\":\"HEAD\"}],\"typeName\":\"KnownHttpMethod\"},{\"kind\":13,\"types\":[{\"kind\":6},{\"kind\":18,\"types\":[]}]}],\"typeName\":\"HttpMethod\"},\"optional\":false},{\"kind\":20,\"name\":\"path\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"propertyKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"symbol\"}]},\"optional\":false},{\"kind\":20,\"name\":\"middlewares\",\"type\":{\"kind\":14,\"type\":{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"ClassType\",\"typeArguments\":[{\"kind\":16,\"typeName\":\"HttpMiddleware\",\"classType\":{\"$tsf\":0}}]},{\"kind\":2,\"typeName\":\"(request: HttpRequest, response: HttpResponse) =\\u003e HttpMiddlewareResult\",\"typeName\":\"HttpMiddlewareFunction\"}],\"typeName\":\"HttpMiddlewareInput\"}},\"optional\":false}]},\"RouteParameterResolver\":{\"kind\":18,\"typeName\":\"RouteParameterResolver\",\"types\":[],\"typeParameters\":[\"T\"]},\"RouteParameterResolverContext\":{\"kind\":18,\"typeName\":\"RouteParameterResolverContext\",\"types\":[{\"kind\":20,\"name\":\"token\",\"type\":{\"kind\":12,\"types\":[{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"ClassType\"},{\"kind\":6},{\"kind\":2,\"typeName\":\"symbol\"},{\"kind\":2}]},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"route\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"request\",\"type\":{\"kind\":16,\"typeName\":\"HttpRequest\",\"classType\":{\"$tsfImport\":[1,\"./request\",\"HttpRequest\"]}},\"optional\":false},{\"kind\":20,\"name\":\"response\",\"type\":{\"kind\":16,\"typeName\":\"HttpResponse\",\"classType\":{\"$tsfImport\":[1,\"./response\",\"HttpResponse\"]}},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"value\",\"type\":{\"kind\":2},\"optional\":false},{\"kind\":20,\"name\":\"query\",\"type\":{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}}]}],\"index\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":14,\"type\":{\"kind\":6}}]},\"types\":[]},\"optional\":false},{\"kind\":20,\"name\":\"parameters\",\"type\":{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":2}],\"index\":{\"kind\":2},\"types\":[]},\"optional\":false},{\"kind\":20,\"name\":\"type\",\"type\":{\"kind\":16,\"typeName\":\"ReflectionParameter\",\"classType\":{\"$tsfImport\":[1,\"../reflection/index\",\"ReflectionParameter\"]}},\"optional\":false}]},\"RouteParameterResolverMetadata\":{\"kind\":18,\"typeName\":\"RouteParameterResolverMetadata\",\"types\":[{\"kind\":20,\"name\":\"type\",\"type\":{\"kind\":2,\"typeName\":\"ClassType\"},\"optional\":false},{\"kind\":20,\"name\":\"resolver\",\"type\":{\"kind\":2,\"typeName\":\"RouteParameterResolverInput\"},\"optional\":false}]},\"RouteParameterResolverRegistry\":{\"kind\":18,\"typeName\":\"Record\",\"utilityType\":\"Record\",\"typeArguments\":[{\"kind\":6},{\"kind\":2,\"typeName\":\"RouteParameterResolverInput\"}],\"index\":{\"kind\":2,\"typeName\":\"RouteParameterResolverInput\"},\"types\":[],\"typeName\":\"RouteParameterResolverRegistry\"}}]", [() => (typeof HttpMiddleware !== "undefined" ? HttpMiddleware : (typeof exports !== "undefined" ? exports.HttpMiddleware : undefined)), require]);
//# sourceMappingURL=decorators.js.map