"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    getExtendedValidator: function() {
        return getExtendedValidator;
    },
    getScopedPrinter: function() {
        return getScopedPrinter;
    },
    validateInput: function() {
        return validateInput;
    },
    validateMiddleware: function() {
        return validateMiddleware;
    }
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _printer = require("../middleware/printer");
const _normalizeurl = require("../utils/normalize-url");
const _nodeinputvalidator = /*#__PURE__*/ _interop_require_wildcard(require("node-input-validator"));
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
function getExtendedValidator() {
    (0, _nodeinputvalidator.extend)("wsurl", ({ value, args }, validator)=>{
        if (!value) return false;
        try {
            const url = (0, _normalizeurl.normalizeUrl)(value);
            return url.startsWith("ws://") || url.startsWith("wss://");
        } catch (e) {
            return false;
        }
    });
    (0, _nodeinputvalidator.extend)("httpurl", ({ value, args }, validator)=>{
        if (!value) return false;
        try {
            if (!value.startsWith("http://") && !value.startsWith("https://")) {
                return false;
            }
            return new URL((0, _normalizeurl.normalizeUrl)(value));
        } catch (e) {
            return false;
        }
    });
    (0, _nodeinputvalidator.extend)("not", ({ value, args }, validator)=>{
        return !value && value !== false;
    });
    (0, _nodeinputvalidator.extendMessages)({
        not: "The :attribute field may not be present."
    });
    return _nodeinputvalidator.default;
}
function getScopedPrinter(req) {
    const tokens = [
        _printer.printerApiToken,
        _printer.printerLoginToken,
        _printer.currentPrinterToken,
        _printer.printerIdToken
    ];
    let resolvedDependencies = {};
    let errors = [];
    tokens.forEach((t)=>{
        try {
            const dependency = req.container.resolve(t);
            if (!dependency) {
                errors.push(`Scoped Dependency '${t}' was not resolved. Please ensure the route requires a :id param and the printerId was provided.`);
            }
            resolvedDependencies[t] = dependency;
        } catch (e) {
            throw new _runtimeexceptions.InternalServerException(`Dependency ${t} could not be resolved. Aborted request.`);
        }
    });
    if (errors.length > 0) {
        throw new _runtimeexceptions.ValidationException(errors);
    }
    return resolvedDependencies;
}
async function validateInput(data, rules) {
    const localNIV = getExtendedValidator();
    const v = new localNIV.Validator(data, rules);
    const matched = await v.check();
    if (!matched) {
        throw new _runtimeexceptions.ValidationException(v.errors);
    }
    return v.inputs;
}
async function validateMiddleware(req, rules) {
    return validateInput(req.body, rules);
}

//# sourceMappingURL=validators.js.map