"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = (handler, validationsBuilder, apiName, event, context) => __awaiter(void 0, void 0, void 0, function* () {
    let queryStringParameters = event.queryStringParameters || {};
    let multiValueQueryStringParameters = event.multiValueQueryStringParameters || {};
    Object.keys(multiValueQueryStringParameters).forEach(key => {
        if (queryStringParameters[key]) {
            delete queryStringParameters[key];
            const value = multiValueQueryStringParameters[key];
            if (key.match(/.*\[\]$/m)) {
                key = key.slice(0, -2);
            }
            queryStringParameters[key] = value;
        }
    });
    let payload = JSON.parse(event.body || '{}');
    let pathParameters = event.pathParameters || {};
    const validations = validationsBuilder();
    const validationResult = {
        isValid: true,
    };
    if (validations.queryStringParameters) {
        try {
            queryStringParameters = yield validations.queryStringParameters.validate(queryStringParameters);
        }
        catch (e) {
            validationResult.isValid = false;
            validationResult.queryStringParametersErrors = e;
            queryStringParameters = validationResult.queryStringParametersErrors.value;
            validationResult.message = e.message;
        }
    }
    if (validations.payload) {
        try {
            payload = yield validations.payload.validate(payload);
        }
        catch (e) {
            validationResult.isValid = false;
            validationResult.payloadErrors = e;
            payload = validationResult.payloadErrors.value;
            validationResult.message = e.message;
        }
    }
    if (validations.pathParameters) {
        try {
            pathParameters = yield validations.pathParameters.validate(pathParameters);
        }
        catch (e) {
            validationResult.isValid = false;
            validationResult.pathParametersErrors = e;
            pathParameters = validationResult.pathParametersErrors.value;
            validationResult.message = e.message;
        }
    }
    return yield handler({
        validationResult,
        queryStringParameters,
        payload,
        pathParameters,
    });
});
//# sourceMappingURL=index.js.map