import DatabaseProperty from "../Database/DatabaseProperty";
import BadDataException from "../Exception/BadDataException";
import FilterCondition from "../Filter/FilterCondition";
import { ObjectType } from "../JSON";
import JSONFunctions from "../JSONFunctions";
import ObjectID from "../ObjectID";
import Typeof from "../Typeof";
import { CriteriaAlertSchema } from "./CriteriaAlert";
import { CheckOn, FilterType, EvaluateOverTimeType, CriteriaFilterUtil, CriteriaFilterSchema, } from "./CriteriaFilter";
import { CriteriaIncidentSchema } from "./CriteriaIncident";
import MonitorType from "./MonitorType";
import Zod from "../../Utils/Schema/Zod";
export default class MonitorCriteriaInstance extends DatabaseProperty {
    constructor() {
        super();
        this.data = undefined;
        this.data = {
            id: ObjectID.generate().toString(),
            monitorStatusId: undefined,
            filterCondition: FilterCondition.All,
            filters: [
                {
                    checkOn: CheckOn.IsOnline,
                    filterType: undefined,
                    value: undefined,
                },
            ],
            createIncidents: false,
            createAlerts: false,
            changeMonitorStatus: false,
            isEnabled: true,
            incidents: [],
            alerts: [],
            name: "",
            description: "",
        };
    }
    static getDefaultOnlineMonitorCriteriaInstance(arg) {
        if (arg.monitorType === MonitorType.IncomingRequest) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.IncomingRequest,
                        filterType: FilterType.RecievedInMinutes,
                        value: 30,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.IncomingEmail) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.EmailReceivedAt,
                        filterType: FilterType.RecievedInMinutes,
                        value: 30,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Logs) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.LogCount,
                        filterType: FilterType.GreaterThan,
                        value: 0, // if there are some logs then monitor is online.
                    },
                ],
                incidents: [],
                alerts: [],
                changeMonitorStatus: true,
                createIncidents: false,
                createAlerts: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Exceptions) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.ExceptionCount,
                        filterType: FilterType.EqualTo,
                        value: 0,
                    },
                ],
                incidents: [],
                alerts: [],
                changeMonitorStatus: true,
                createIncidents: false,
                createAlerts: false,
                name: `Check if ${arg.monitorName} has no exceptions`,
                description: `This criteria checks if the ${arg.monitorName} has no exceptions.`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Metrics) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.MetricValue,
                        filterType: FilterType.GreaterThan,
                        metricMonitorOptions: {
                            metricAggregationType: EvaluateOverTimeType.AnyValue,
                            metricAlias: arg.metricOptions &&
                                arg.metricOptions.metricAliases &&
                                arg.metricOptions.metricAliases.length > 0
                                ? arg.metricOptions.metricAliases[0]
                                : undefined,
                        },
                        value: 0, // if there are some logs then monitor is online.
                    },
                ],
                incidents: [],
                alerts: [],
                changeMonitorStatus: true,
                createIncidents: false,
                createAlerts: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Kubernetes) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.MetricValue,
                        filterType: FilterType.GreaterThan,
                        metricMonitorOptions: {
                            metricAggregationType: EvaluateOverTimeType.AnyValue,
                            metricAlias: arg.metricOptions &&
                                arg.metricOptions.metricAliases &&
                                arg.metricOptions.metricAliases.length > 0
                                ? arg.metricOptions.metricAliases[0]
                                : undefined,
                        },
                        value: 0,
                    },
                ],
                incidents: [],
                alerts: [],
                changeMonitorStatus: true,
                createIncidents: false,
                createAlerts: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Traces) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.SpanCount,
                        filterType: FilterType.GreaterThan,
                        value: 0, // if there are some logs then monitor is online.
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.SSLCertificate) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.IsValidCertificate,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.CustomJavaScriptCode ||
            arg.monitorType === MonitorType.SyntheticMonitor) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.Error,
                        filterType: FilterType.IsEmpty,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Server) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.IsOnline,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Website ||
            arg.monitorType === MonitorType.API ||
            arg.monitorType === MonitorType.Ping ||
            arg.monitorType === MonitorType.IP ||
            arg.monitorType === MonitorType.Port) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.IsOnline,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} is online`,
            };
            if (arg.monitorType === MonitorType.Website ||
                arg.monitorType === MonitorType.API) {
                monitorCriteriaInstance.data.filters.push({
                    checkOn: CheckOn.ResponseStatusCode,
                    filterType: FilterType.EqualTo,
                    value: 200,
                });
            }
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.SNMP) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.SnmpIsOnline,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} SNMP device is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.DNS) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.DnsIsOnline,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} DNS resolution is online`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.Domain) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.DomainIsExpired,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is not expired`,
                description: `This criteria checks if the ${arg.monitorName} domain registration is not expired`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.DNSSEC) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.DnssecChainValid,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} DNSSEC chain is valid`,
                description: `This criteria checks if the ${arg.monitorName} DNSSEC chain is valid end-to-end`,
            };
            return monitorCriteriaInstance;
        }
        if (arg.monitorType === MonitorType.ExternalStatusPage) {
            const monitorCriteriaInstance = new MonitorCriteriaInstance();
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.All,
                filters: [
                    {
                        checkOn: CheckOn.ExternalStatusPageIsOnline,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [],
                alerts: [],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: false,
                name: `Check if ${arg.monitorName} is online`,
                description: `This criteria checks if the ${arg.monitorName} external status page is reachable`,
            };
            return monitorCriteriaInstance;
        }
        return null;
    }
    static getDefaultOfflineMonitorCriteriaInstance(arg) {
        const monitorCriteriaInstance = new MonitorCriteriaInstance();
        if (arg.monitorType === MonitorType.Ping ||
            arg.monitorType === MonitorType.IP ||
            arg.monitorType === MonitorType.Port ||
            arg.monitorType === MonitorType.Server) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.IsOnline,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.SNMP) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.SnmpIsOnline,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} SNMP device is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} SNMP device is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} SNMP device is offline`,
            };
        }
        if (arg.monitorType === MonitorType.DNS) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.DnsIsOnline,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} DNS resolution is currently failing.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} DNS resolution is currently failing.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} DNS resolution is failing`,
            };
        }
        if (arg.monitorType === MonitorType.Domain) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.DomainIsExpired,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} domain is expired`,
                        description: `${arg.monitorName} domain registration has expired.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} domain is expired`,
                        description: `${arg.monitorName} domain registration has expired.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} domain is expired`,
                description: `This criteria checks if the ${arg.monitorName} domain registration has expired`,
            };
        }
        if (arg.monitorType === MonitorType.DNSSEC) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.DnssecChainValid,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} DNSSEC chain is broken`,
                        description: `${arg.monitorName} DNSSEC validation is currently failing.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} DNSSEC chain is broken`,
                        description: `${arg.monitorName} DNSSEC validation is currently failing.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} DNSSEC chain is broken`,
                description: `This criteria checks if the ${arg.monitorName} DNSSEC chain is broken`,
            };
        }
        if (arg.monitorType === MonitorType.ExternalStatusPage) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.ExternalStatusPageIsOnline,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} external status page is currently unreachable.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                createAlerts: false,
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} external status page is currently unreachable.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} external status page is unreachable`,
            };
        }
        if (arg.monitorType === MonitorType.API ||
            arg.monitorType === MonitorType.Website) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.IsOnline,
                        filterType: FilterType.False,
                        value: undefined,
                    },
                    {
                        checkOn: CheckOn.ResponseStatusCode,
                        filterType: FilterType.NotEqualTo,
                        value: 200,
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.Logs) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.LogCount,
                        filterType: FilterType.EqualTo,
                        value: 0, // if there are no logs then the monitor is offline
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.Exceptions) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.ExceptionCount,
                        filterType: FilterType.GreaterThan,
                        value: 0,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} has exceptions`,
                        description: `${arg.monitorName} has active exceptions.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} has exceptions`,
                        description: `${arg.monitorName} has active exceptions.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} has exceptions`,
                description: `This criteria checks if the ${arg.monitorName} has exceptions.`,
            };
        }
        if (arg.monitorType === MonitorType.Metrics) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.MetricValue,
                        filterType: FilterType.EqualTo,
                        metricMonitorOptions: {
                            metricAggregationType: EvaluateOverTimeType.AnyValue,
                            metricAlias: arg.metricOptions &&
                                arg.metricOptions.metricAliases &&
                                arg.metricOptions.metricAliases.length > 0
                                ? arg.metricOptions.metricAliases[0]
                                : undefined,
                        },
                        value: 0, // if there are no logs then the monitor is offline
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.Kubernetes) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.MetricValue,
                        filterType: FilterType.EqualTo,
                        metricMonitorOptions: {
                            metricAggregationType: EvaluateOverTimeType.AnyValue,
                            metricAlias: arg.metricOptions &&
                                arg.metricOptions.metricAliases &&
                                arg.metricOptions.metricAliases.length > 0
                                ? arg.metricOptions.metricAliases[0]
                                : undefined,
                        },
                        value: 0,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.Traces) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.SpanCount,
                        filterType: FilterType.EqualTo,
                        value: 0, // if there are no logs then the monitor is offline
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.IncomingRequest) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.IncomingRequest,
                        filterType: FilterType.NotRecievedInMinutes,
                        value: 30, // if the request is not recieved in 30 minutes, then the monitor is offline
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.IncomingEmail) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.EmailReceivedAt,
                        filterType: FilterType.NotRecievedInMinutes,
                        value: 30, // if email is not received in 30 minutes, then the monitor is offline
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline. No email received.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline. No email received.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.CustomJavaScriptCode ||
            arg.monitorType === MonitorType.SyntheticMonitor) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                filters: [
                    {
                        checkOn: CheckOn.Error,
                        filterType: FilterType.IsNotEmpty,
                        value: undefined,
                    },
                ],
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        if (arg.monitorType === MonitorType.SSLCertificate) {
            monitorCriteriaInstance.data = {
                id: ObjectID.generate().toString(),
                monitorStatusId: arg.monitorStatusId,
                filterCondition: FilterCondition.Any,
                alerts: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        alertSeverityId: arg.alertSeverityId,
                        autoResolveAlert: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                createAlerts: false,
                filters: [
                    {
                        checkOn: CheckOn.IsNotAValidCertificate,
                        filterType: FilterType.True,
                        value: undefined,
                    },
                ],
                incidents: [
                    {
                        title: `${arg.monitorName} is offline`,
                        description: `${arg.monitorName} is currently offline.`,
                        incidentSeverityId: arg.incidentSeverityId,
                        autoResolveIncident: true,
                        id: ObjectID.generate().toString(),
                        onCallPolicyIds: [],
                    },
                ],
                changeMonitorStatus: true,
                createIncidents: true,
                name: `Check if ${arg.monitorName} is offline`,
                description: `This criteria checks if the ${arg.monitorName} is offline`,
            };
        }
        return monitorCriteriaInstance;
    }
    static getNewMonitorCriteriaInstanceAsJSON() {
        return {
            id: ObjectID.generate().toString(),
            monitorStatusId: undefined,
            filterCondition: FilterCondition.All,
            filters: [
                {
                    checkOn: CheckOn.IsOnline,
                    filterType: FilterType.True,
                    value: undefined,
                },
            ],
            incidents: [],
            name: "",
            description: "",
            createIncidents: false,
            changeMonitorStatus: false,
        };
    }
    static getValidationError(value, monitorType) {
        var _a;
        if (!value.data) {
            return `Monitor Step is required.`;
        }
        if (value.data.filters.length === 0) {
            return `Filter is required for criteria "${value.data.name}"`;
        }
        if (!value.data.name) {
            return `Name is required for criteria "${value.data.name}"`;
        }
        if (!value.data.description) {
            return `Description is required for criteria "${value.data.name}"`;
        }
        for (const incident of value.data.incidents) {
            if (!incident) {
                continue;
            }
            if (!incident.title) {
                return `Incident title is required for criteria "${value.data.name}"`;
            }
            if (!incident.description) {
                return `Incident description is required for criteria "${value.data.name}"`;
            }
            if (!incident.incidentSeverityId) {
                return `Incident severity is required for criteria "${value.data.name}"`;
            }
        }
        for (const alert of value.data.alerts) {
            if (!alert) {
                continue;
            }
            if (!alert.title) {
                return `Alert title is required for criteria "${value.data.name}"`;
            }
            if (!alert.description) {
                return `Alert description is required for criteria "${value.data.name}"`;
            }
            if (!alert.alertSeverityId) {
                return `Alert severity is required for criteria "${value.data.name}"`;
            }
        }
        for (const filter of value.data.filters) {
            if (!filter.checkOn) {
                return `Filter Type is required for criteria "${value.data.name}"`;
            }
            if (monitorType === MonitorType.Ping &&
                filter.checkOn !== CheckOn.IsOnline &&
                filter.checkOn !== CheckOn.ResponseTime &&
                filter.checkOn !== CheckOn.IsRequestTimeout) {
                return "Ping Monitor cannot have filter type: " + filter.checkOn;
            }
            if (filter.checkOn === CheckOn.DiskUsagePercent &&
                !((_a = filter.serverMonitorOptions) === null || _a === void 0 ? void 0 : _a.diskPath)) {
                return "Disk Path is required for Disk Usage Percent";
            }
            if (CriteriaFilterUtil.hasValueField({
                checkOn: filter.checkOn,
                filterType: filter.filterType,
            })) {
                if (!filter.value && filter.value !== 0) {
                    return `Value is required for criteria "${value.data.name}" on filter type: ${filter.checkOn}`;
                }
            }
        }
        return null;
    }
    setName(name) {
        if (this.data) {
            this.data.name = name;
        }
        return this;
    }
    setDescription(description) {
        if (this.data) {
            this.data.description = description;
        }
        return this;
    }
    static clone(monitorCriteriaInstance) {
        return MonitorCriteriaInstance.fromJSON(monitorCriteriaInstance.toJSON());
    }
    setMonitorStatusId(monitorStatusId) {
        if (this.data) {
            this.data.monitorStatusId = monitorStatusId;
        }
        return this;
    }
    setFilterCondition(filterCondition) {
        if (this.data) {
            this.data.filterCondition = filterCondition;
        }
        return this;
    }
    setFilters(filters) {
        if (this.data) {
            this.data.filters = filters;
        }
        return this;
    }
    setIncidents(incidents) {
        if (this.data) {
            this.data.incidents = [...incidents];
        }
        return this;
    }
    setAlerts(alerts) {
        if (this.data) {
            this.data.alerts = [...alerts];
        }
        return this;
    }
    setChangeMonitorStatus(changeMonitorStatus) {
        if (this.data) {
            this.data.changeMonitorStatus = changeMonitorStatus;
        }
        return this;
    }
    setCreateIncidents(createIncidents) {
        if (this.data) {
            this.data.createIncidents = createIncidents;
        }
        return this;
    }
    setCreateAlerts(createAlerts) {
        if (this.data) {
            this.data.createAlerts = createAlerts;
        }
        return this;
    }
    setIsEnabled(isEnabled) {
        if (this.data) {
            this.data.isEnabled = isEnabled;
        }
        return this;
    }
    toJSON() {
        var _a;
        if (!this.data) {
            return MonitorCriteriaInstance.getNewMonitorCriteriaInstanceAsJSON();
        }
        return JSONFunctions.serialize({
            _type: ObjectType.MonitorCriteriaInstance,
            value: {
                id: this.data.id,
                monitorStatusId: (_a = this.data.monitorStatusId) === null || _a === void 0 ? void 0 : _a.toString(),
                filterCondition: this.data.filterCondition,
                filters: this.data.filters,
                incidents: this.data.incidents,
                alerts: this.data.alerts,
                createAlerts: this.data.createAlerts,
                changeMonitorStatus: this.data.changeMonitorStatus,
                createIncidents: this.data.createIncidents,
                isEnabled: this.data.isEnabled,
                name: this.data.name,
                description: this.data.description,
            },
        });
    }
    static fromJSON(json) {
        if (json instanceof MonitorCriteriaInstance) {
            return json;
        }
        if (!json) {
            throw new BadDataException("json is null");
        }
        if (!json["_type"]) {
            throw new BadDataException("json._type is null");
        }
        if (json["_type"] !== ObjectType.MonitorCriteriaInstance) {
            throw new BadDataException("json._type should be MonitorCriteriaInstance");
        }
        if (!json["value"]) {
            throw new BadDataException("json.value is null");
        }
        json = json["value"];
        if (!json["filterCondition"]) {
            throw new BadDataException("json.filterCondition is null");
        }
        if (!json["filters"]) {
            throw new BadDataException("json.filters is null");
        }
        if (!Array.isArray(json["filters"])) {
            throw new BadDataException("json.filters should be an array");
        }
        if (!json["incidents"]) {
            json["incidents"] = [];
        }
        if (!Array.isArray(json["incidents"])) {
            throw new BadDataException("json.incidents should be an array");
        }
        if (!json["alerts"]) {
            json["alerts"] = [];
        }
        if (!Array.isArray(json["alerts"])) {
            throw new BadDataException("json.alerts should be an array");
        }
        let monitorStatusId = undefined;
        if (json["monitorStatusId"] &&
            typeof json["monitorStatusId"] === Typeof.String) {
            monitorStatusId = new ObjectID(json["monitorStatusId"]);
        }
        else if (json["monitorStatusId"] &&
            json["monitorStatusId"]["value"] !== null) {
            monitorStatusId = new ObjectID(json["monitorStatusId"]["value"]);
        }
        const filterCondition = json["filterCondition"];
        const filters = [];
        const incidents = [];
        for (const filter of json["filters"]) {
            filters.push(Object.assign({}, filter));
        }
        for (const incident of json["incidents"]) {
            incidents.push(Object.assign({}, incident));
        }
        const alerts = [];
        for (const alert of json["alerts"]) {
            alerts.push(Object.assign({}, alert));
        }
        const monitorCriteriaInstance = new MonitorCriteriaInstance();
        monitorCriteriaInstance.data = JSONFunctions.deserialize({
            id: json["id"] || ObjectID.generate().toString(),
            monitorStatusId,
            filterCondition,
            changeMonitorStatus: json["changeMonitorStatus"] || false,
            createIncidents: json["createIncidents"] || false,
            createAlerts: json["createAlerts"] || false,
            isEnabled: json["isEnabled"] === undefined ? true : json["isEnabled"],
            filters: filters,
            incidents: incidents,
            alerts: alerts,
            name: json["name"] || "",
            description: json["description"] || "",
        });
        return monitorCriteriaInstance;
    }
    static getSchema() {
        return Zod.object({
            _type: Zod.literal(ObjectType.MonitorCriteriaInstance),
            value: Zod.object({
                id: Zod.string(),
                monitorStatusId: Zod.any(),
                filterCondition: Zod.any(),
                filters: Zod.array(CriteriaFilterSchema),
                incidents: Zod.array(CriteriaIncidentSchema),
                alerts: Zod.array(CriteriaAlertSchema),
                name: Zod.string(),
                description: Zod.string(),
                changeMonitorStatus: Zod.boolean().optional(),
                createIncidents: Zod.boolean().optional(),
                createAlerts: Zod.boolean().optional(),
                isEnabled: Zod.boolean().optional(),
            }).openapi({
                type: "object",
                example: {
                    id: "id",
                    monitorStatusId: "statusId",
                    filterCondition: "All",
                    filters: [],
                    incidents: [],
                    alerts: [],
                    name: "Criteria Name",
                    description: "Description",
                },
            }),
        }).openapi({
            type: "object",
            description: "MonitorCriteriaInstance object",
            example: {
                _type: ObjectType.MonitorCriteriaInstance,
                value: {
                    id: "id",
                    monitorStatusId: "statusId",
                    filterCondition: "All",
                    filters: [],
                    incidents: [],
                    alerts: [],
                    name: "Criteria Name",
                    description: "Description",
                },
            },
        });
    }
    static isValid(_json) {
        return true;
    }
    static toDatabase(value) {
        if (value && value instanceof MonitorCriteriaInstance) {
            return value.toJSON();
        }
        else if (value) {
            return JSONFunctions.serialize(value);
        }
        return null;
    }
    static fromDatabase(value) {
        if (value) {
            return MonitorCriteriaInstance.fromJSON(value);
        }
        return null;
    }
    toString() {
        return JSON.stringify(this.toJSON());
    }
}
//# sourceMappingURL=MonitorCriteriaInstance.js.map