"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkMimetypeRegex = exports.isFileValid = exports.toIntArray = exports.areUUIDsValid = exports.toIntOrNull = exports.isBooleanValid = exports.toBooleanOrNull = exports.toValueOrNull = exports.isDateValid = exports.isIdOrUUIDValid = exports.toCompleteUUID = exports.toCompleteUUIDs = exports.isUUIDValid = exports.isNotEmptyStringArray = exports.isSafePath = exports.isIdValid = exports.isIntOrNull = exports.isArray = exports.isNotEmptyIntArray = exports.isArrayOf = exports.exists = void 0;
const tslib_1 = require("tslib");
require("multer");
const path_1 = require("path");
const validator_1 = tslib_1.__importDefault(require("validator"));
const extra_utils_1 = require("../../../shared/extra-utils");
function exists(value) {
    return value !== undefined && value !== null;
}
exports.exists = exists;
function isSafePath(p) {
    return exists(p) &&
        (p + '').split(path_1.sep).every(part => {
            return ['..'].includes(part) === false;
        });
}
exports.isSafePath = isSafePath;
function isArray(value) {
    return Array.isArray(value);
}
exports.isArray = isArray;
function isNotEmptyIntArray(value) {
    return Array.isArray(value) && value.every(v => validator_1.default.isInt('' + v)) && value.length !== 0;
}
exports.isNotEmptyIntArray = isNotEmptyIntArray;
function isNotEmptyStringArray(value) {
    return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0;
}
exports.isNotEmptyStringArray = isNotEmptyStringArray;
function isArrayOf(value, validator) {
    return isArray(value) && value.every(v => validator(v));
}
exports.isArrayOf = isArrayOf;
function isDateValid(value) {
    return exists(value) && validator_1.default.isISO8601(value);
}
exports.isDateValid = isDateValid;
function isIdValid(value) {
    return exists(value) && validator_1.default.isInt('' + value);
}
exports.isIdValid = isIdValid;
function isUUIDValid(value) {
    return exists(value) && validator_1.default.isUUID('' + value, 4);
}
exports.isUUIDValid = isUUIDValid;
function areUUIDsValid(values) {
    return isArray(values) && values.every(v => isUUIDValid(v));
}
exports.areUUIDsValid = areUUIDsValid;
function isIdOrUUIDValid(value) {
    return isIdValid(value) || isUUIDValid(value);
}
exports.isIdOrUUIDValid = isIdOrUUIDValid;
function isBooleanValid(value) {
    return typeof value === 'boolean' || (typeof value === 'string' && validator_1.default.isBoolean(value));
}
exports.isBooleanValid = isBooleanValid;
function isIntOrNull(value) {
    return value === null || validator_1.default.isInt('' + value);
}
exports.isIntOrNull = isIntOrNull;
function isFileValid(options) {
    const { files, mimeTypeRegex, field, maxSize, optional = false } = options;
    if (!files)
        return optional;
    const fileArray = isArray(files)
        ? files
        : files[field];
    if (!fileArray || !isArray(fileArray) || fileArray.length === 0) {
        return optional;
    }
    const file = fileArray[0];
    if (!(file === null || file === void 0 ? void 0 : file.originalname))
        return false;
    if ((maxSize !== null) && file.size > maxSize)
        return false;
    if (mimeTypeRegex === null)
        return true;
    return checkMimetypeRegex(file.mimetype, mimeTypeRegex);
}
exports.isFileValid = isFileValid;
function checkMimetypeRegex(fileMimeType, mimeTypeRegex) {
    return new RegExp(`^${mimeTypeRegex}$`, 'i').test(fileMimeType);
}
exports.checkMimetypeRegex = checkMimetypeRegex;
function toCompleteUUID(value) {
    if ((0, extra_utils_1.isShortUUID)(value))
        return (0, extra_utils_1.shortToUUID)(value);
    return value;
}
exports.toCompleteUUID = toCompleteUUID;
function toCompleteUUIDs(values) {
    return values.map(v => toCompleteUUID(v));
}
exports.toCompleteUUIDs = toCompleteUUIDs;
function toIntOrNull(value) {
    const v = toValueOrNull(value);
    if (v === null || v === undefined)
        return v;
    if (typeof v === 'number')
        return v;
    return validator_1.default.toInt('' + v);
}
exports.toIntOrNull = toIntOrNull;
function toBooleanOrNull(value) {
    const v = toValueOrNull(value);
    if (v === null || v === undefined)
        return v;
    if (typeof v === 'boolean')
        return v;
    return validator_1.default.toBoolean('' + v);
}
exports.toBooleanOrNull = toBooleanOrNull;
function toValueOrNull(value) {
    if (value === 'null')
        return null;
    return value;
}
exports.toValueOrNull = toValueOrNull;
function toIntArray(value) {
    if (!value)
        return [];
    if (isArray(value) === false)
        return [validator_1.default.toInt(value)];
    return value.map(v => validator_1.default.toInt(v));
}
exports.toIntArray = toIntArray;
//# sourceMappingURL=misc.js.map