"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ParamBool () {
        return ParamBool;
    },
    get ParamId () {
        return ParamId;
    },
    get ParamInt () {
        return ParamInt;
    },
    get ParamString () {
        return ParamString;
    }
});
const _validators = require("../handlers/validators");
const _genericvalidation = require("../controllers/validation/generic.validation");
const ParamInt = (paramName)=>createParamDecorator(paramName, Number);
const ParamBool = (paramName)=>createParamDecorator(paramName, Boolean);
const ParamString = (paramName)=>createParamDecorator(paramName, String);
const ParamId = (paramName)=>createParamDecorator(paramName, "id");
function createParamDecorator(paramName, type) {
    return async (req, res, next)=>{
        const paramValue = req.params[paramName];
        if (paramValue === undefined) {
            return res.status(400).send(`Missing parameter: ${paramName}`);
        }
        let convertedValue;
        let validateIdAsType = null;
        if (type === "id") {
            validateIdAsType = Number;
        }
        if (type === Boolean) {
            if (paramValue.toLowerCase() === "true") {
                convertedValue = true;
            } else if (paramValue.toLowerCase() === "false") {
                convertedValue = false;
            } else {
                return res.status(400).send(`Invalid boolean: ${paramName}`);
            }
        } else if (type === Number || validateIdAsType === Number) {
            convertedValue = Number.parseInt(paramValue, 10);
            if (Number.isNaN(convertedValue)) {
                return res.status(400).send(`Invalid number: ${paramName}`);
            }
        } else {
            return res.status(400).send(`Unknown type of: ${paramName}`);
        }
        if (validateIdAsType === Number) {
            try {
                await (0, _validators.validateInput)({
                    id: convertedValue
                }, _genericvalidation.idRulesV2);
            } catch (e) {
                return next(e);
            }
        }
        req.local = req.local || {};
        req.local[paramName] = convertedValue;
        next();
    };
}

//# sourceMappingURL=param-converter.middleware.js.map