import DatabaseProperty from "../Database/DatabaseProperty";
import BadDataException from "../Exception/BadDataException";
import { ObjectType } from "../JSON";
import JSONFunctions from "../JSONFunctions";
import MonitorCriteriaInstance from "./MonitorCriteriaInstance";
import Zod from "../../Utils/Schema/Zod";
export default class MonitorCriteria extends DatabaseProperty {
    constructor() {
        super();
        this.data = undefined;
        this.data = {
            monitorCriteriaInstanceArray: [new MonitorCriteriaInstance()],
        };
    }
    static getDefaultMonitorCriteria(arg) {
        const monitorCriteria = new MonitorCriteria();
        const offlineCriteria = MonitorCriteriaInstance.getDefaultOfflineMonitorCriteriaInstance({
            monitorType: arg.monitorType,
            monitorStatusId: arg.offlineMonitorStatusId,
            incidentSeverityId: arg.defaultIncidentSeverityId,
            alertSeverityId: arg.defaultAlertSeverityId,
            monitorName: arg.monitorName,
        });
        const onlineCriteria = MonitorCriteriaInstance.getDefaultOnlineMonitorCriteriaInstance({
            monitorType: arg.monitorType,
            monitorStatusId: arg.onlineMonitorStatusId,
            monitorName: arg.monitorName,
        });
        monitorCriteria.data = {
            monitorCriteriaInstanceArray: [],
        };
        if (offlineCriteria) {
            monitorCriteria.data.monitorCriteriaInstanceArray.push(offlineCriteria);
        }
        if (onlineCriteria) {
            monitorCriteria.data.monitorCriteriaInstanceArray.push(onlineCriteria);
        }
        return monitorCriteria;
    }
    static getValidationError(value, monitorType) {
        if (!value.data) {
            return "Monitor Criteria is required";
        }
        if (value.data.monitorCriteriaInstanceArray.length === 0) {
            return "Monitor Criteria is required";
        }
        for (const criteria of value.data.monitorCriteriaInstanceArray) {
            if (MonitorCriteriaInstance.getValidationError(criteria, monitorType)) {
                return MonitorCriteriaInstance.getValidationError(criteria, monitorType);
            }
        }
        return null;
    }
    static getNewMonitorCriteriaAsJSON() {
        return {
            _type: "MonitorCriteria",
            value: {
                monitorCriteriaInstanceArray: [new MonitorCriteriaInstance().toJSON()],
            },
        };
    }
    toJSON() {
        if (!this.data) {
            return MonitorCriteria.getNewMonitorCriteriaAsJSON();
        }
        return JSONFunctions.serialize({
            _type: ObjectType.MonitorCriteria,
            value: {
                monitorCriteriaInstanceArray: this.data.monitorCriteriaInstanceArray.map((criteria) => {
                    return criteria.toJSON();
                }),
            },
        });
    }
    static fromJSON(json) {
        if (json instanceof MonitorCriteria) {
            return json;
        }
        if (!json || json["_type"] !== ObjectType.MonitorCriteria) {
            throw new BadDataException("Invalid monitor criteria");
        }
        if (!json) {
            throw new BadDataException("Invalid monitor criteria");
        }
        if (!json["value"]) {
            throw new BadDataException("Invalid monitor criteria");
        }
        if (!json["value"]["monitorCriteriaInstanceArray"]) {
            throw new BadDataException("Invalid monitor criteria");
        }
        const monitorCriteriaInstanceArray = json["value"]["monitorCriteriaInstanceArray"];
        const monitorCriteria = new MonitorCriteria();
        monitorCriteria.data = {
            monitorCriteriaInstanceArray: monitorCriteriaInstanceArray.map((json) => {
                return MonitorCriteriaInstance.fromJSON(json);
            }),
        };
        return monitorCriteria;
    }
    static isValid(_json) {
        return true;
    }
    static toDatabase(value) {
        if (value && value instanceof MonitorCriteria) {
            return value.toJSON();
        }
        else if (value) {
            return JSONFunctions.serialize(value);
        }
        return null;
    }
    static fromDatabase(value) {
        if (value) {
            return MonitorCriteria.fromJSON(value);
        }
        return null;
    }
    toString() {
        return JSON.stringify(this.toJSON());
    }
    static getSchema() {
        return Zod.object({
            _type: Zod.literal(ObjectType.MonitorCriteria),
            value: Zod.object({
                monitorCriteriaInstanceArray: Zod.array(MonitorCriteriaInstance.getSchema()),
            }).openapi({
                type: "object",
                example: {
                    monitorCriteriaInstanceArray: [],
                },
            }),
        }).openapi({
            type: "object",
            description: "MonitorCriteria object",
            example: {
                _type: ObjectType.MonitorCriteria,
                value: { monitorCriteriaInstanceArray: [] },
            },
        });
    }
}
//# sourceMappingURL=MonitorCriteria.js.map