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 OneUptimeDate from "../../../../Types/Date";
import { CheckOn, FilterType, } from "../../../../Types/Monitor/CriteriaFilter";
import EvaluateOverTime from "./EvaluateOverTime";
import CaptureSpan from "../../Telemetry/CaptureSpan";
import logger from "../../Logger";
export default class ServerMonitorCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        let threshold = input.criteriaFilter.value;
        const dataToProcess = input.dataToProcess;
        const sslResponse = dataToProcess.sslResponse;
        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;
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsOnline) {
            const currentIsOnline = overTimeValue ||
                input.dataToProcess.isOnline;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsOnline,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // timeout.
        if (input.criteriaFilter.checkOn === CheckOn.IsRequestTimeout) {
            const currentIsTimeout = overTimeValue ||
                input.dataToProcess.isTimeout;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsTimeout,
                criteriaFilter: input.criteriaFilter,
            });
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsValidCertificate) {
            const isValidCertificate = Boolean(sslResponse &&
                dataToProcess.isOnline &&
                sslResponse.expiresAt &&
                !sslResponse.isSelfSigned &&
                OneUptimeDate.isAfter(sslResponse.expiresAt, OneUptimeDate.getCurrentDate()));
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (isValidCertificate && isTrue) {
                return "SSL certificate is valid.";
            }
            if (!isValidCertificate && isFalse) {
                return "SSL certificate is not valid.";
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsSelfSignedCertificate) {
            const isSelfSigned = Boolean(sslResponse && sslResponse.isSelfSigned);
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (isSelfSigned && isTrue) {
                return "SSL Certificate is self signed.";
            }
            if (!isSelfSigned && isFalse) {
                return "SSL Certificate is not self signed.";
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsExpiredCertificate) {
            const isExpired = Boolean(sslResponse &&
                sslResponse.expiresAt &&
                OneUptimeDate.isBefore(sslResponse.expiresAt, OneUptimeDate.getCurrentDate()));
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (isExpired && isTrue) {
                return "SSL certificate is expired.";
            }
            if (!isExpired && isFalse) {
                return "SSL certificate is not expired.";
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsNotAValidCertificate) {
            const isNotValid = !sslResponse ||
                !dataToProcess.isOnline ||
                Boolean(sslResponse &&
                    sslResponse.expiresAt &&
                    (sslResponse.isSelfSigned ||
                        OneUptimeDate.isBefore(sslResponse.expiresAt, OneUptimeDate.getCurrentDate())));
            const isTrue = input.criteriaFilter.filterType === FilterType.True;
            const isFalse = input.criteriaFilter.filterType === FilterType.False;
            if (isNotValid && isTrue) {
                return "SSL certificate is not valid.";
            }
            if (!isNotValid && isFalse) {
                return "SSL certificate is valid.";
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.ExpiresInHours) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (!threshold) {
                return null;
            }
            const expiresAt = sslResponse && sslResponse.expiresAt;
            const hours = expiresAt &&
                OneUptimeDate.getHoursBetweenTwoDates(OneUptimeDate.getCurrentDate(), expiresAt);
            if (hours === null || hours === undefined) {
                return null;
            }
            return CompareCriteria.compareCriteriaNumbers({
                value: hours,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        if (input.criteriaFilter.checkOn === CheckOn.ExpiresInDays) {
            threshold = CompareCriteria.convertToNumber(threshold);
            if (!threshold) {
                return null;
            }
            const expiresAt = sslResponse && sslResponse.expiresAt;
            const days = expiresAt &&
                OneUptimeDate.getDaysBetweenTwoDates(OneUptimeDate.getCurrentDate(), expiresAt);
            if (days === null || days === undefined) {
                return null;
            }
            return CompareCriteria.compareCriteriaNumbers({
                value: days,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], ServerMonitorCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=SSLMonitorCriteria.js.map