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 EvaluateOverTime from "./EvaluateOverTime";
import { CheckOn, FilterType, } from "../../../../Types/Monitor/CriteriaFilter";
import Typeof from "../../../../Types/Typeof";
import CaptureSpan from "../../Telemetry/CaptureSpan";
import logger from "../../Logger";
export default class APIRequestCriteria {
    static async isMonitorInstanceCriteriaFilterMet(input) {
        // Server Monitoring Checks
        let threshold = input.criteriaFilter.value;
        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,
                    miscData: input.criteriaFilter.serverMonitorOptions,
                });
                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,
            });
        }
        if (input.criteriaFilter.checkOn === CheckOn.IsRequestTimeout) {
            const currentIsTimeout = overTimeValue ||
                input.dataToProcess.isTimeout;
            return CompareCriteria.compareCriteriaBoolean({
                value: currentIsTimeout,
                criteriaFilter: input.criteriaFilter,
            });
        }
        // check response time filter
        if (input.criteriaFilter.checkOn === CheckOn.ResponseTime) {
            threshold = CompareCriteria.convertToNumber(threshold);
            const value = overTimeValue ||
                input.dataToProcess.responseTimeInMs;
            return CompareCriteria.compareCriteriaNumbers({
                value: value,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        //check response code
        if (input.criteriaFilter.checkOn === CheckOn.ResponseStatusCode &&
            input.dataToProcess.responseCode) {
            threshold = CompareCriteria.convertToNumber(threshold);
            const value = overTimeValue ||
                input.dataToProcess.responseCode;
            return CompareCriteria.compareCriteriaNumbers({
                value: value,
                threshold: threshold,
                criteriaFilter: input.criteriaFilter,
            });
        }
        if (input.criteriaFilter.checkOn === CheckOn.ResponseBody) {
            let responseBody = input.dataToProcess.responseBody;
            if (responseBody && typeof responseBody === Typeof.Object) {
                responseBody = JSON.stringify(responseBody);
            }
            if (!responseBody) {
                return null;
            }
            // contains
            if (input.criteriaFilter.filterType === FilterType.Contains) {
                if (threshold &&
                    responseBody &&
                    responseBody.includes(threshold)) {
                    return `Response body contains ${threshold}.`;
                }
                return null;
            }
            if (input.criteriaFilter.filterType === FilterType.NotContains) {
                if (threshold &&
                    responseBody &&
                    !responseBody.includes(threshold)) {
                    return `Response body does not contain ${threshold}.`;
                }
                return null;
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.ResponseHeader) {
            const headerKeys = Object.keys(input.dataToProcess.responseHeaders || {}).map((key) => {
                return key.toLowerCase();
            });
            // contains
            if (input.criteriaFilter.filterType === FilterType.Contains) {
                if (threshold &&
                    headerKeys &&
                    headerKeys.includes(threshold)) {
                    return `Response header contains ${threshold}.`;
                }
                return null;
            }
            if (input.criteriaFilter.filterType === FilterType.NotContains) {
                if (threshold &&
                    headerKeys &&
                    !headerKeys.includes(threshold)) {
                    return `Response header does not contain ${threshold}.`;
                }
                return null;
            }
        }
        if (input.criteriaFilter.checkOn === CheckOn.ResponseHeaderValue) {
            const headerValues = Object.values(input.dataToProcess.responseHeaders || {}).map((key) => {
                return key.toLowerCase();
            });
            // contains
            if (input.criteriaFilter.filterType === FilterType.Contains) {
                if (threshold &&
                    headerValues &&
                    headerValues.includes(threshold)) {
                    return `Response header threshold contains ${threshold}.`;
                }
                return null;
            }
            if (input.criteriaFilter.filterType === FilterType.NotContains) {
                if (threshold &&
                    headerValues &&
                    !headerValues.includes(threshold)) {
                    return `Response header threshold does not contain ${threshold}.`;
                }
                return null;
            }
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], APIRequestCriteria, "isMonitorInstanceCriteriaFilterMet", null);
//# sourceMappingURL=APIRequestCriteria.js.map