'use strict';
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const joi_1 = __importDefault(require("joi"));
const validator = __importStar(require("./validationWrapper.js"));
const moment_1 = __importDefault(require("moment"));
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const configUtils_js_1 = require("../config/configUtils.js");
const hdbTerms = __importStar(require("../utility/hdbTerms.js"));
const hdbTerms_ts_1 = require("../utility/hdbTerms.js");
const LOG_DATE_FORMAT = 'YYYY-MM-DD hh:mm:ss';
function default_1(object) {
    return validator.validateBySchema(object, readLogSchema);
}
const readLogSchema = joi_1.default.object({
    from: joi_1.default.custom(validateDatetime),
    until: joi_1.default.custom(validateDatetime),
    to: joi_1.default.custom(validateDatetime),
    level: joi_1.default.valid(hdbTerms_ts_1.LOG_LEVELS.NOTIFY, hdbTerms_ts_1.LOG_LEVELS.FATAL, hdbTerms_ts_1.LOG_LEVELS.ERROR, hdbTerms_ts_1.LOG_LEVELS.WARN, hdbTerms_ts_1.LOG_LEVELS.INFO, hdbTerms_ts_1.LOG_LEVELS.DEBUG, hdbTerms_ts_1.LOG_LEVELS.TRACE),
    order: joi_1.default.valid('asc', 'desc'),
    limit: joi_1.default.number().min(1),
    start: joi_1.default.number().min(0),
    log_name: joi_1.default.custom(validateReadLogPath),
    filter: joi_1.default.string(),
});
function validateDatetime(value, helpers) {
    if ((0, moment_1.default)(value, moment_1.default.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 = (0, configUtils_js_1.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