'use strict';
const Joi = require('joi');
const validator = require('./validationWrapper.js');
const moment = require('moment');
const fs = require('fs-extra');
const path = require('path');
const { getConfigPath } = require('../config/configUtils.js');
const hdbTerms = require("../utility/hdbTerms.js");
const { LOG_LEVELS } = require("../utility/hdbTerms.js");
const LOG_DATE_FORMAT = 'YYYY-MM-DD hh:mm:ss';
module.exports = function (object) {
    return validator.validateBySchema(object, readLogSchema);
};
const readLogSchema = Joi.object({
    from: Joi.custom(validateDatetime),
    until: Joi.custom(validateDatetime),
    to: Joi.custom(validateDatetime),
    level: Joi.valid(LOG_LEVELS.NOTIFY, LOG_LEVELS.FATAL, LOG_LEVELS.ERROR, LOG_LEVELS.WARN, LOG_LEVELS.INFO, LOG_LEVELS.DEBUG, LOG_LEVELS.TRACE),
    order: Joi.valid('asc', 'desc'),
    limit: Joi.number().min(1),
    start: Joi.number().min(0),
    log_name: Joi.custom(validateReadLogPath),
    filter: Joi.string(),
});
function validateDatetime(value, helpers) {
    if (moment(value, moment.ISO_8601).format(LOG_DATE_FORMAT) === 'Invalid date') {
        return helpers.message(`'${helpers.state.path[0]}' date '${value}' is invalid.`);
    }
}
function validateReadLogPath(value, helpers) {
    if (path.posix.basename(value) !== value || path.win32.basename(value) !== value) {
        return helpers.message(`'log_name' '${value}' is invalid.`);
    }
    const ext = path.extname(value);
    if (ext && ext !== '.log') {
        return helpers.message(`'log_name' '${value}' is invalid.`);
    }
    const logName = ext === '.log' ? value : `${value}.log`;
    const logPath = getConfigPath(hdbTerms.HDB_SETTINGS_NAMES.LOG_PATH_KEY);
    const readLogPath = path.join(logPath, logName);
    if (fs.existsSync(readLogPath)) {
        return value;
    }
    return helpers.message(`'log_name' '${value}' does not exist.`);
}
//# sourceMappingURL=readLogValidator.js.map