import DropdownUtil from "../../../UI/Utils/Dropdown";
import MonitorType from "../../Monitor/MonitorType";
import NotificationRuleEventType from "./EventType";
export var NotificationRuleConditionCheckOn;
(function (NotificationRuleConditionCheckOn) {
    NotificationRuleConditionCheckOn["MonitorName"] = "Monitor Name";
    NotificationRuleConditionCheckOn["IncidentTitle"] = "Incident Title";
    NotificationRuleConditionCheckOn["IncidentDescription"] = "Incident Description";
    NotificationRuleConditionCheckOn["IncidentSeverity"] = "Incident Severity";
    NotificationRuleConditionCheckOn["IncidentState"] = "Incident State";
    NotificationRuleConditionCheckOn["MonitorType"] = "Monitor Type";
    NotificationRuleConditionCheckOn["MonitorStatus"] = "Monitor Status";
    NotificationRuleConditionCheckOn["AlertTitle"] = "Alert Title";
    NotificationRuleConditionCheckOn["AlertDescription"] = "Alert Description";
    NotificationRuleConditionCheckOn["AlertSeverity"] = "Alert Severity";
    NotificationRuleConditionCheckOn["AlertState"] = "Alert State";
    NotificationRuleConditionCheckOn["AlertEpisodeTitle"] = "Alert Episode Title";
    NotificationRuleConditionCheckOn["AlertEpisodeDescription"] = "Alert Episode Description";
    NotificationRuleConditionCheckOn["AlertEpisodeSeverity"] = "Alert Episode Severity";
    NotificationRuleConditionCheckOn["AlertEpisodeState"] = "Alert Episode State";
    NotificationRuleConditionCheckOn["AlertEpisodeLabels"] = "Alert Episode Labels";
    NotificationRuleConditionCheckOn["IncidentEpisodeTitle"] = "Incident Episode Title";
    NotificationRuleConditionCheckOn["IncidentEpisodeDescription"] = "Incident Episode Description";
    NotificationRuleConditionCheckOn["IncidentEpisodeSeverity"] = "Incident Episode Severity";
    NotificationRuleConditionCheckOn["IncidentEpisodeState"] = "Incident Episode State";
    NotificationRuleConditionCheckOn["IncidentEpisodeLabels"] = "Incident Episode Labels";
    NotificationRuleConditionCheckOn["ScheduledMaintenanceTitle"] = "Scheduled Maintenance Title";
    NotificationRuleConditionCheckOn["ScheduledMaintenanceDescription"] = "Scheduled Maintenance Description";
    NotificationRuleConditionCheckOn["ScheduledMaintenanceState"] = "Scheduled Maintenance State";
    NotificationRuleConditionCheckOn["IncidentLabels"] = "Incident Labels";
    NotificationRuleConditionCheckOn["AlertLabels"] = "Alert Labels";
    NotificationRuleConditionCheckOn["MonitorLabels"] = "Monitor Labels";
    NotificationRuleConditionCheckOn["ScheduledMaintenanceLabels"] = "Scheduled Maintenance Labels";
    NotificationRuleConditionCheckOn["Monitors"] = "Monitors";
    // On Call Duty Policy
    NotificationRuleConditionCheckOn["OnCallDutyPolicyName"] = "On Call Duty Policy Name";
    NotificationRuleConditionCheckOn["OnCallDutyPolicyDescription"] = "On Call Duty Policy Description";
    NotificationRuleConditionCheckOn["OnCallDutyPolicyLabels"] = "On Call Duty Policy Labels";
})(NotificationRuleConditionCheckOn || (NotificationRuleConditionCheckOn = {}));
export var ConditionType;
(function (ConditionType) {
    ConditionType["EqualTo"] = "Equal To";
    ConditionType["NotEqualTo"] = "Not Equal To";
    ConditionType["GreaterThan"] = "Greater Than";
    ConditionType["LessThan"] = "Less Than";
    ConditionType["GreaterThanOrEqualTo"] = "Greater Than Or Equal To";
    ConditionType["LessThanOrEqualTo"] = "Less Than Or Equal To";
    ConditionType["Contains"] = "Contains";
    ConditionType["ContainsAny"] = "Contains Any";
    ConditionType["NotContains"] = "Not Contains";
    ConditionType["StartsWith"] = "Starts With";
    ConditionType["EndsWith"] = "Ends With";
    ConditionType["IsEmpty"] = "Is Empty";
    ConditionType["IsNotEmpty"] = "Is Not Empty";
    ConditionType["True"] = "True";
    ConditionType["False"] = "False";
    ConditionType["ContainsAll"] = "Contains All";
})(ConditionType || (ConditionType = {}));
export class NotificationRuleConditionUtil {
    static getValidationError(data) {
        var _a, _b;
        const { notificationRule, eventType, workspaceType } = data;
        for (const condition of notificationRule.filters) {
            if (!condition.checkOn) {
                return "Check On is required";
            }
            if (!condition.conditionType) {
                return `Filter Condition is required for ${condition.checkOn}`;
            }
            if (!condition.value) {
                return `Value is required for ${condition.checkOn}`;
            }
            if (Array.isArray(condition.value) && condition.value.length === 0) {
                return `Value is required for ${condition.checkOn}`;
            }
        }
        if (eventType === NotificationRuleEventType.Incident ||
            eventType === NotificationRuleEventType.Alert ||
            eventType === NotificationRuleEventType.AlertEpisode ||
            eventType === NotificationRuleEventType.IncidentEpisode ||
            eventType === NotificationRuleEventType.ScheduledMaintenance) {
            // either create slack channel or select existing one should be active.
            if (!notificationRule.shouldCreateNewChannel &&
                !notificationRule.shouldPostToExistingChannel) {
                return ("Please select either create slack channel or post to existing " +
                    workspaceType +
                    " channel");
            }
            if (notificationRule.shouldPostToExistingChannel) {
                if (!((_a = notificationRule.existingChannelNames) === null || _a === void 0 ? void 0 : _a.trim())) {
                    return "Existing " + workspaceType + " channel name is required";
                }
            }
            if (notificationRule.shouldCreateNewChannel) {
                if (!((_b = notificationRule.newChannelTemplateName) === null || _b === void 0 ? void 0 : _b.trim())) {
                    return "New " + workspaceType + " channel name is required";
                }
            }
        }
        return null;
    }
    static hasValueField(data) {
        switch (data.conditionType) {
            case ConditionType.IsEmpty:
            case ConditionType.IsNotEmpty:
            case ConditionType.True:
            case ConditionType.False:
                return false;
            default:
                return true;
        }
    }
    static isDropdownValueField(data) {
        // incident state, alert state, monitor status, scheduled maintenance state, severit, labels, monitors are all dropdowns.
        if (!data.checkOn || !data.conditionType) {
            return false;
        }
        switch (data.checkOn) {
            case NotificationRuleConditionCheckOn.MonitorType:
            case NotificationRuleConditionCheckOn.IncidentState:
            case NotificationRuleConditionCheckOn.AlertState:
            case NotificationRuleConditionCheckOn.AlertEpisodeState:
            case NotificationRuleConditionCheckOn.IncidentEpisodeState:
            case NotificationRuleConditionCheckOn.MonitorStatus:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceState:
            case NotificationRuleConditionCheckOn.IncidentSeverity:
            case NotificationRuleConditionCheckOn.AlertSeverity:
            case NotificationRuleConditionCheckOn.AlertEpisodeSeverity:
            case NotificationRuleConditionCheckOn.IncidentEpisodeSeverity:
            case NotificationRuleConditionCheckOn.MonitorLabels:
            case NotificationRuleConditionCheckOn.IncidentLabels:
            case NotificationRuleConditionCheckOn.AlertLabels:
            case NotificationRuleConditionCheckOn.AlertEpisodeLabels:
            case NotificationRuleConditionCheckOn.IncidentEpisodeLabels:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels:
            case NotificationRuleConditionCheckOn.Monitors:
                return true;
            default:
                return false;
        }
    }
    static getDropdownOptionsByCheckOn(data) {
        if (data.checkOn === NotificationRuleConditionCheckOn.IncidentEpisodeSeverity) {
            return data.incidentSeverities.map((severity) => {
                return {
                    value: severity.id.toString(),
                    label: severity.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.AlertSeverity ||
            data.checkOn === NotificationRuleConditionCheckOn.AlertEpisodeSeverity) {
            return data.alertSeverities.map((severity) => {
                return {
                    value: severity.id.toString(),
                    label: severity.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.IncidentSeverity) {
            return data.incidentSeverities.map((severity) => {
                return {
                    value: severity.id.toString(),
                    label: severity.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.MonitorStatus) {
            return data.monitorStatus.map((status) => {
                return {
                    value: status.id.toString(),
                    label: status.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.IncidentState ||
            data.checkOn === NotificationRuleConditionCheckOn.IncidentEpisodeState) {
            return data.incidentStates.map((state) => {
                return {
                    value: state.id.toString(),
                    label: state.name || "",
                };
            });
        }
        if (data.checkOn ===
            NotificationRuleConditionCheckOn.ScheduledMaintenanceState) {
            return data.scheduledMaintenanceStates.map((state) => {
                return {
                    value: state.id.toString(),
                    label: state.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.MonitorLabels ||
            data.checkOn === NotificationRuleConditionCheckOn.IncidentLabels ||
            data.checkOn === NotificationRuleConditionCheckOn.AlertLabels ||
            data.checkOn === NotificationRuleConditionCheckOn.AlertEpisodeLabels ||
            data.checkOn === NotificationRuleConditionCheckOn.IncidentEpisodeLabels ||
            data.checkOn ===
                NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels) {
            return data.labels.map((label) => {
                return {
                    value: label.id.toString(),
                    label: label.name || "",
                };
            });
        }
        if (data.checkOn === NotificationRuleConditionCheckOn.Monitors) {
            return data.monitors.map((monitor) => {
                return {
                    value: monitor.id.toString(),
                    label: monitor.name || "",
                };
            });
        }
        // alert states (also used for alert episodes)
        if (data.checkOn === NotificationRuleConditionCheckOn.AlertState ||
            data.checkOn === NotificationRuleConditionCheckOn.AlertEpisodeState) {
            return data.alertStates.map((state) => {
                return {
                    value: state.id.toString(),
                    label: state.name || "",
                };
            });
        }
        // if monitor type
        if (data.checkOn === NotificationRuleConditionCheckOn.MonitorType) {
            return DropdownUtil.getDropdownOptionsFromEnum(MonitorType);
        }
        return [];
    }
    static getCheckOnByEventType(eventType) {
        switch (eventType) {
            case NotificationRuleEventType.Incident:
                return [
                    NotificationRuleConditionCheckOn.IncidentTitle,
                    NotificationRuleConditionCheckOn.IncidentDescription,
                    NotificationRuleConditionCheckOn.IncidentSeverity,
                    NotificationRuleConditionCheckOn.IncidentState,
                    NotificationRuleConditionCheckOn.IncidentLabels,
                    NotificationRuleConditionCheckOn.MonitorLabels,
                    NotificationRuleConditionCheckOn.Monitors,
                ];
            case NotificationRuleEventType.Alert:
                return [
                    NotificationRuleConditionCheckOn.AlertTitle,
                    NotificationRuleConditionCheckOn.AlertDescription,
                    NotificationRuleConditionCheckOn.AlertSeverity,
                    NotificationRuleConditionCheckOn.AlertState,
                    NotificationRuleConditionCheckOn.AlertLabels,
                    NotificationRuleConditionCheckOn.MonitorLabels,
                    NotificationRuleConditionCheckOn.Monitors,
                ];
            case NotificationRuleEventType.AlertEpisode:
                return [
                    NotificationRuleConditionCheckOn.AlertEpisodeTitle,
                    NotificationRuleConditionCheckOn.AlertEpisodeDescription,
                    NotificationRuleConditionCheckOn.AlertEpisodeSeverity,
                    NotificationRuleConditionCheckOn.AlertEpisodeState,
                    NotificationRuleConditionCheckOn.AlertEpisodeLabels,
                ];
            case NotificationRuleEventType.IncidentEpisode:
                return [
                    NotificationRuleConditionCheckOn.IncidentEpisodeTitle,
                    NotificationRuleConditionCheckOn.IncidentEpisodeDescription,
                    NotificationRuleConditionCheckOn.IncidentEpisodeSeverity,
                    NotificationRuleConditionCheckOn.IncidentEpisodeState,
                    NotificationRuleConditionCheckOn.IncidentEpisodeLabels,
                ];
            case NotificationRuleEventType.Monitor:
                return [
                    NotificationRuleConditionCheckOn.MonitorName,
                    NotificationRuleConditionCheckOn.MonitorStatus,
                    NotificationRuleConditionCheckOn.MonitorType,
                    NotificationRuleConditionCheckOn.Monitors,
                    NotificationRuleConditionCheckOn.MonitorLabels,
                ];
            case NotificationRuleEventType.ScheduledMaintenance:
                return [
                    NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle,
                    NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription,
                    NotificationRuleConditionCheckOn.ScheduledMaintenanceState,
                    NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels,
                    NotificationRuleConditionCheckOn.MonitorLabels,
                    NotificationRuleConditionCheckOn.Monitors,
                ];
            default:
                return [];
        }
    }
    static getConditionTypeByCheckOn(checkOn) {
        switch (checkOn) {
            case NotificationRuleConditionCheckOn.MonitorName:
            case NotificationRuleConditionCheckOn.IncidentTitle:
            case NotificationRuleConditionCheckOn.IncidentDescription:
            case NotificationRuleConditionCheckOn.AlertTitle:
            case NotificationRuleConditionCheckOn.AlertDescription:
            case NotificationRuleConditionCheckOn.AlertEpisodeTitle:
            case NotificationRuleConditionCheckOn.AlertEpisodeDescription:
            case NotificationRuleConditionCheckOn.IncidentEpisodeTitle:
            case NotificationRuleConditionCheckOn.IncidentEpisodeDescription:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceTitle:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceDescription:
                return [
                    ConditionType.EqualTo,
                    ConditionType.NotEqualTo,
                    ConditionType.Contains,
                    ConditionType.NotContains,
                    ConditionType.StartsWith,
                    ConditionType.EndsWith,
                ];
            case NotificationRuleConditionCheckOn.IncidentSeverity:
            case NotificationRuleConditionCheckOn.AlertSeverity:
            case NotificationRuleConditionCheckOn.AlertEpisodeSeverity:
            case NotificationRuleConditionCheckOn.IncidentEpisodeSeverity:
                return [ConditionType.ContainsAny, ConditionType.NotContains];
            case NotificationRuleConditionCheckOn.IncidentState:
            case NotificationRuleConditionCheckOn.AlertState:
            case NotificationRuleConditionCheckOn.AlertEpisodeState:
            case NotificationRuleConditionCheckOn.IncidentEpisodeState:
            case NotificationRuleConditionCheckOn.MonitorStatus:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceState:
                return [ConditionType.ContainsAny, ConditionType.NotContains];
            case NotificationRuleConditionCheckOn.MonitorType:
                return [ConditionType.ContainsAny, ConditionType.NotContains];
            case NotificationRuleConditionCheckOn.AlertLabels:
            case NotificationRuleConditionCheckOn.AlertEpisodeLabels:
            case NotificationRuleConditionCheckOn.IncidentEpisodeLabels:
            case NotificationRuleConditionCheckOn.IncidentLabels:
            case NotificationRuleConditionCheckOn.MonitorLabels:
            case NotificationRuleConditionCheckOn.ScheduledMaintenanceLabels:
                return [
                    ConditionType.ContainsAny,
                    ConditionType.NotContains,
                    ConditionType.ContainsAll,
                ];
            case NotificationRuleConditionCheckOn.Monitors:
                return [
                    ConditionType.ContainsAny,
                    ConditionType.NotContains,
                    ConditionType.ContainsAll,
                ];
            default:
                return [];
        }
    }
}
//# sourceMappingURL=NotificationRuleCondition.js.map