"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MonitoringAlertsSchema = exports.PulseMonitoringAlertsSchema = exports.PulseMonitoringSchema = void 0;
const joi_1 = __importDefault(require("joi"));
const alertDurationLimits_1 = require("../util/alertDurationLimits");
exports.PulseMonitoringSchema = joi_1.default.object({
    applicationId: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Application ID is required',
        'string.empty': 'Application ID is required',
        'string.base': 'Application ID is required',
        'string.min': 'Application ID is required',
    }),
    logLocation: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Log location is required',
        'string.empty': 'Log location is required',
        'string.base': 'Log location is required',
        'string.min': 'Log location is required',
    }),
    serviceProvider: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Service provider is required',
        'string.empty': 'Service provider is required',
        'string.base': 'Service provider is required',
        'string.min': 'Service provider is required',
    }),
    accessKey: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'aws',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'Access key is required',
                    'string.empty': 'Access key is required',
                    'string.base': 'Access key is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    secretKey: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'aws',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'Secret key is required',
                    'string.empty': 'Secret key is required',
                    'string.base': 'Secret key is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    awsRegion: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'aws',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'AWS region is required',
                    'string.empty': 'AWS region is required',
                    'string.base': 'AWS region is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    tenantId: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'azure',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'Tenant ID is required',
                    'string.empty': 'Tenant ID is required',
                    'string.base': 'Tenant ID is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    clientId: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'azure',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'Client ID is required',
                    'string.empty': 'Client ID is required',
                    'string.base': 'Client ID is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    clientSecret: joi_1.default.when('isUpdate', {
        is: false,
        then: joi_1.default.string().when('logLocation', {
            is: 'folder-path',
            then: joi_1.default.string().when('serviceProvider', {
                is: 'azure',
                then: joi_1.default.string().trim().min(1).required().messages({
                    'any.required': 'Client secret is required',
                    'string.empty': 'Client secret is required',
                    'string.base': 'Client secret is required',
                }),
                otherwise: joi_1.default.string().allow('', null),
            }),
            otherwise: joi_1.default.string().allow('', null),
        }),
        otherwise: joi_1.default.string().allow('', null),
    }),
    locationUrl: joi_1.default.string().when('logLocation', {
        is: 'folder-path',
        then: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Location URL is required',
            'string.empty': 'Location URL is required',
            'string.base': 'Location URL is required',
        }),
        otherwise: joi_1.default.string().optional().allow('', null),
    }),
    samplingFrequency: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Sampling frequency is required',
        'string.empty': 'Sampling frequency is required',
        'string.base': 'Sampling frequency is required',
        'string.min': 'Sampling frequency is required',
    }),
    samplingPercentage: joi_1.default.when('samplingFrequency', {
        is: 'percent',
        then: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).required().messages({
            'any.required': 'Sampling percentage is required',
            'number.base': 'Sampling percentage is required',
            'string.base': 'Sampling percentage is required',
            'string.empty': 'Sampling percentage is required',
            'string.min': 'Sampling percentage is required',
        }),
        otherwise: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).optional().allow('', null),
    }),
    samplingCount: joi_1.default.when('samplingFrequency', {
        is: 'count',
        then: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).required().messages({
            'any.required': 'Sampling count is required',
            'number.base': 'Sampling yy count is required',
            'string.base': 'Sampling xx count is required',
            'string.empty': 'Sampling count is required',
            'string.min': 'Sampling count is required',
        }),
        otherwise: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).optional().allow('', null),
    }),
    evaluationWindow: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).required().messages({
        'any.required': 'Evaluation window is required',
        'number.base': 'Evaluation window is required',
        'string.base': 'Evaluation window is required',
        'string.empty': 'Evaluation window is required',
        'string.min': 'Evaluation window is required',
    }),
    metrics: joi_1.default.array().items(joi_1.default.object()
        .keys({
        type: joi_1.default.string().trim().optional().allow('', null).messages({
            'any.required': 'type is required',
            'string.empty': 'type is required',
            'string.base': 'type is required',
            'string.min': 'type is required',
        }),
        value: joi_1.default.alternatives().try(joi_1.default.string(), joi_1.default.number()).optional().allow(''),
    })
        .unknown(true)),
    apiKey: joi_1.default.string().optional().allow('', null),
    pluginsByCategory: joi_1.default.any().optional(),
    isUpdate: joi_1.default.boolean().optional().allow('', null),
    enableFunctionalHealthMonitoring: joi_1.default.boolean().optional().allow('', null),
    enableSecurityMonitoring: joi_1.default.boolean().optional().allow('', null),
}).unknown(true);
exports.PulseMonitoringAlertsSchema = joi_1.default.object({
    applicationId: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Application ID is required',
        'string.empty': 'Application ID is required',
        'string.base': 'Application ID is required',
        'string.min': 'Application ID is required',
    }),
    alerts: joi_1.default.array().items(joi_1.default.object({
        type: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Alert type is required',
            'string.empty': 'Alert type is required',
            'string.base': 'Alert type is required',
            'string.min': 'Alert type is required',
        }),
        metricType: joi_1.default.when('type', {
            is: 'metric',
            then: joi_1.default.string().trim().min(1).required().messages({
                'any.required': 'Metric type is required',
                'string.empty': 'Metric type is required',
                'string.base': 'Metric type is required',
                'string.min': 'Metric type is required',
            }),
            otherwise: joi_1.default.string().allow('').optional(),
        }),
        metricValue: joi_1.default.when('type', {
            is: 'metric',
            then: joi_1.default.number().required().messages({
                'any.required': 'Metric value is required',
                'number.base': 'Metric value is required',
                'string.base': 'Metric value is required',
                'string.empty': 'Metric value is required',
                'string.min': 'Metric value is required',
            }),
            otherwise: joi_1.default.string().allow('').optional(),
        }),
        metricComparator: joi_1.default.alternatives().try(joi_1.default.number().required().messages({
            'any.required': 'Metric comparator is required',
            'number.base': 'Metric comparator must be integer',
            'number.empty': 'Metric comparator must be integer',
        }), joi_1.default.string().pattern(/^\d+$/).required().messages({
            'any.required': 'Metric comparator is required',
            'string.pattern.base': 'Metric comparator must be integer',
            'string.empty': 'Metric comparator must be integer',
        })),
        metricComparatorType: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Metric Comparator type is required',
            'boolean.base': 'Metric Comparator type is required',
            'string.base': 'Metric Comparator type is required',
            'string.empty': 'Metric Comparator type is required',
            'string.min': 'Metric Comparator type is required',
        }),
    }).optional()),
    emails: joi_1.default.string().trim().min(1).optional().messages({
        'any.required': 'Emails are required',
        'string.empty': 'Emails are required',
        'string.base': 'Emails are required',
        'string.min': 'Emails are required',
    }),
    webhook: joi_1.default.string().allow('').trim().optional(),
});
exports.MonitoringAlertsSchema = joi_1.default.object({
    name: joi_1.default.string().trim().required().messages({
        'any.required': 'Alerts name is required',
        'string.empty': 'Alerts name is required',
    }),
    applicationId: joi_1.default.string().trim().min(1).required().messages({
        'any.required': 'Application ID is required',
        'string.empty': 'Application ID is required',
        'string.base': 'Application ID is required',
        'string.min': 'Application ID is required',
    }),
    alert: joi_1.default.object({
        type: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Alert type is required',
            'string.empty': 'Alert type is required',
            'string.base': 'Alert type is required',
            'string.min': 'Alert type is required',
        }),
        subType: joi_1.default.when('type', {
            is: 'system_health',
            then: joi_1.default.string().trim().min(1).required().messages({
                'any.required': 'Sub type is required',
                'string.empty': 'Sub type is required',
                'string.base': 'Sub type is required',
                'string.min': 'Sub type is required',
            }),
            otherwise: joi_1.default.string().allow('').optional(),
        }),
        metricType: joi_1.default.when('type', {
            is: 'metric',
            then: joi_1.default.string().trim().min(1).required().messages({
                'any.required': 'Metric type is required',
                'string.empty': 'Metric type is required',
                'string.base': 'Metric type is required',
                'string.min': 'Metric type is required',
            }),
            otherwise: joi_1.default.string().allow('').optional(),
        }),
        metricValue: joi_1.default.when('type', {
            is: 'metric',
            then: joi_1.default.number().required().messages({
                'any.required': 'Metric value is required',
                'number.base': 'Metric value is required',
                'string.base': 'Metric value is required',
                'string.empty': 'Metric value is required',
                'string.min': 'Metric value is required',
            }),
            otherwise: joi_1.default.alternatives().try(joi_1.default.number().optional(), joi_1.default.string().allow('').optional()),
        }),
        metricComparator: joi_1.default.when('type', {
            is: joi_1.default.string().valid('metric', 'plugins', 'system_health'),
            then: joi_1.default.alternatives()
                .try(joi_1.default.number().required().messages({
                'any.required': 'Metric comparator is required',
                'number.base': 'Metric comparator must be integer',
                'number.empty': 'Metric comparator must be integer',
            }), joi_1.default.string()
                .pattern(/^\d+$/)
                .required()
                .messages({
                'any.required': 'Metric comparator is required',
                'string.pattern.base': 'Metric comparator must be integer',
                'string.empty': 'Metric comparator must be integer',
            }))
                .custom((value, helpers) => {
                const alert = helpers.state.ancestors[0];
                if (alert?.type !== 'system_health' || alert?.subType !== 'cost') {
                    return value;
                }
                const unit = alert.metricComparatorType ?? 'days';
                const max = (0, alertDurationLimits_1.getMaxAlertDurationForUnit)(unit);
                const n = Number(value);
                if (!Number.isFinite(n) || n < 1) {
                    return helpers.error('number.base');
                }
                if (n > max) {
                    return helpers.message({
                        custom: `Duration must be at most ${max} ${unit}`,
                    });
                }
                return value;
            }),
            otherwise: joi_1.default.alternatives().try(joi_1.default.number().optional(), joi_1.default.string().allow('').optional()),
        }),
        condition: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Metric Comparator type is required',
            'string.empty': 'Metric Comparator type is required',
            'string.min': 'Metric Comparator type is required',
        }),
        metricComparatorType: joi_1.default.string().trim().min(1).required().messages({
            'any.required': 'Metric Comparator type is required',
            'boolean.base': 'Metric Comparator type is required',
            'string.base': 'Metric Comparator type is required',
            'string.empty': 'Metric Comparator type is required',
            'string.min': 'Metric Comparator type is required',
        }),
    }).optional(),
    notificationChannel: joi_1.default.string().trim().messages({
        'any.required': 'notificationChannel is required',
        'string.empty': 'notificationChannel cant be empty',
        'string.base': 'notificationChannel is required',
    }),
});
//# sourceMappingURL=pulse.js.map