var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { HttpContext } from '@adonisjs/core/http';
import { asyncBlock, method } from '../observability/tracing.js';
import { AKVineHandlerMetaData } from './vine_handler_metadata.js';
export class AKVineRequestData {
}
export default class AKVineValidateMiddleware {
    async handle({ route, request, containerResolver }, next) {
        const handler = route?.handler;
        if (handler) {
            const requestBodyValidator = (AKVineHandlerMetaData.getRequestSchema(handler) || {}).validator;
            const requestQueryValidator = (AKVineHandlerMetaData.getRequestQueryParameters(handler) || {})
                .validator;
            const body = request.body();
            const query = request.qs();
            if (requestBodyValidator || requestQueryValidator) {
                const bodyData = requestBodyValidator
                    ? await asyncBlock('VineValidateMiddleware.requestBodyValidator', async () => await requestBodyValidator.validate(body))
                    : {};
                const queryData = requestQueryValidator
                    ? await asyncBlock('VineValidateMiddleware.requestQueryValidator', async () => await requestQueryValidator.validate(query))
                    : {};
                containerResolver.bindValue(AKVineRequestData, {
                    ...queryData,
                    ...bodyData,
                });
            }
            await next();
        }
        else
            await next();
    }
}
__decorate([
    method(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [HttpContext, Function]),
    __metadata("design:returntype", Promise)
], AKVineValidateMiddleware.prototype, "handle", null);
//# sourceMappingURL=vine_validate_middleware.js.map