var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import CompareCriteria from "./CompareCriteria";
import { CheckOn, } from "../../../../Types/Monitor/CriteriaFilter";
import EvaluateOverTime from "./EvaluateOverTime";
import CaptureSpan from "../../Telemetry/CaptureSpan";
import logger from "../../Logger";
export default class ExternalStatusPageMonitorCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        let threshold = input.criteriaFilter.value;
        const dataToProcess = input.dataToProcess;
        const externalStatusPageResponse = dataToProcess.externalStatusPageResponse;
        let overTimeValue = undefined;
        if (input.criteriaFilter.evaluateOverTime &&
            input.criteriaFilter.evaluateOverTimeOptions) {
            try {
                overTimeValue = await EvaluateOverTime.getValueOverTime({
                    projectId: input.dataToProcess.projectId,
                    monitorId: input.dataToProcess.monitorId,
                    evaluateOverTimeOptions: input.criteriaFilter.evaluateOverTimeOptions,
                    metricType: input.criteriaFilter.checkOn,
                });
                if (Array.isArray(overTimeValue) && overTimeValue.length === 0) {
                    overTimeValue = undefined;
                }
            }
            catch (err) {
                logger.error(`Error in getting over time value for ${input.criteriaFilter.checkOn}`);
                logger.error(err);
                overTimeValue = undefined;
            }
        }
        // Check if external status page is online
        if (input.criteriaFilter.checkOn === CheckOn.ExternalStatusPageIsOnline) {
            const currentIsOnline = overTimeValue ||
                input.dataToProcess.isOnline;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsOnline,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check external status page response time
        if (input.criteriaFilter.checkOn === CheckOn.ExternalStatusPageResponseTime) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (threshold === null || threshold === undefined) {
                return null;
            }
            const currentResponseTime = overTimeValue ||
                (externalStatusPageResponse === null || externalStatusPageResponse === void 0 ? void 0 : externalStatusPageResponse.responseTimeInMs) ||
                input.dataToProcess.responseTimeInMs;
            if (currentResponseTime === null || currentResponseTime === undefined) {
                return null;
            }
            return CompareCriteria.compareCriteriaNumbers({
                value: currentResponseTime,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check overall status
        if (input.criteriaFilter.checkOn === CheckOn.ExternalStatusPageOverallStatus) {
            if (!(externalStatusPageResponse === null || externalStatusPageResponse === void 0 ? void 0 : externalStatusPageResponse.overallStatus)) {
                return null;
            }
            return CompareCriteria.compareCriteriaStrings({
                value: externalStatusPageResponse.overallStatus,
                threshold: String(threshold),
                criteriaFilter: input.criteriaFilter,
            });
        }
        // Check component status
        if (input.criteriaFilter.checkOn === CheckOn.ExternalStatusPageComponentStatus) {
            if (!(externalStatusPageResponse === null || externalStatusPageResponse === void 0 ? void 0 : externalStatusPageResponse.componentStatuses) ||
                externalStatusPageResponse.componentStatuses.length === 0) {
                return null;
            }
            // Check if any component status matches the criteria
            for (const component of externalStatusPageResponse.componentStatuses) {
                const result = CompareCriteria.compareCriteriaStrings({
                    value: component.status,
                    threshold: String(threshold),
                    criteriaFilter: input.criteriaFilter,
                });
                if (result) {
                    return `Component "${component.name}": ${result}`;
                }
            }
            return null;
        }
        // Check active incidents count
        if (input.criteriaFilter.checkOn === CheckOn.ExternalStatusPageActiveIncidents) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (threshold === null || threshold === undefined) {
                return null;
            }
            const activeIncidents = (externalStatusPageResponse === null || externalStatusPageResponse === void 0 ? void 0 : externalStatusPageResponse.activeIncidentCount) || 0;
            return CompareCriteria.compareCriteriaNumbers({
                value: activeIncidents,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ExternalStatusPageMonitorCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=ExternalStatusPageMonitorCriteria.js.map