"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.STATUS_DEFAULT = exports.STATUS_ALLOWED = exports.STATUS_SCHEMAS = void 0;
exports.validateStatus = validateStatus;
const joi_1 = __importDefault(require("joi"));
const validator = __importStar(require("./validationWrapper.js"));
const definitions_ts_1 = require("../server/status/definitions.js");
// Re-export constants for backward compatibility
exports.STATUS_SCHEMAS = definitions_ts_1.STATUS_DEFINITIONS;
exports.STATUS_ALLOWED = definitions_ts_1.STATUS_IDS;
exports.STATUS_DEFAULT = definitions_ts_1.DEFAULT_STATUS_ID;
/**
 * Pregenerate error messages to avoid repeated string concatenation
 */
const ERROR_MESSAGES = Object.entries(definitions_ts_1.STATUS_DEFINITIONS).reduce((messages, [id, definition]) => {
    if (definition.allowedValues) {
        messages[id] = `Status "${id}" only accepts these values: ${definition.allowedValues.join(', ')}`;
    }
    return messages;
}, {});
/**
 * Creates the status validation schema using the STATUS_DEFINITIONS
 */
const createStatusValidationSchema = () => {
    // Start with base schema
    let statusSchema = joi_1.default.string().min(1).max(512);
    // Add conditional validations for each status type that has allowedValues
    Object.entries(definitions_ts_1.STATUS_DEFINITIONS).forEach(([id, definition]) => {
        if (definition.allowedValues) {
            statusSchema = statusSchema.when('id', {
                is: id,
                then: joi_1.default.string()
                    .valid(...definition.allowedValues)
                    .messages({
                    'any.only': ERROR_MESSAGES[id],
                }),
            });
        }
    });
    return statusSchema.required();
};
/**
 * Joi schema for validating status operations
 */
const setStatusSchema = joi_1.default.object({
    id: joi_1.default.string()
        .valid(...exports.STATUS_ALLOWED)
        .required(),
    status: createStatusValidationSchema(),
});
/**
 * Validates the status operation parameters
 * @param obj The status operation parameters to validate
 * @returns Error if validation fails, null otherwise
 */
function validateStatus(obj) {
    return validator.validateBySchema(obj, setStatusSchema);
}
//# sourceMappingURL=statusValidator.js.map