import DatabaseProperty from "../Database/DatabaseProperty";
import BadDataException from "../Exception/BadDataException";
import { ObjectType } from "../JSON";
import JSONFunctions from "../JSONFunctions";
import ObjectID from "../ObjectID";
import MonitorStep from "./MonitorStep";
import Zod from "../../Utils/Schema/Zod";
export default class MonitorSteps extends DatabaseProperty {
    constructor() {
        super();
        this.data = undefined;
        this.data = {
            monitorStepsInstanceArray: [new MonitorStep()],
            defaultMonitorStatusId: undefined,
        };
    }
    static getNewMonitorStepsAsJSON() {
        return {
            _type: ObjectType.MonitorSteps,
            value: {
                monitorStepsInstanceArray: [new MonitorStep().toJSON()],
                defaultMonitorStatusId: undefined,
            },
        };
    }
    static getDefaultMonitorSteps(arg) {
        const monitorSteps = new MonitorSteps();
        monitorSteps.data = {
            monitorStepsInstanceArray: [MonitorStep.getDefaultMonitorStep(arg)],
            defaultMonitorStatusId: arg.defaultMonitorStatusId,
        };
        return monitorSteps;
    }
    setMonitorStepsInstanceArray(monitorSteps) {
        if (this.data) {
            this.data.monitorStepsInstanceArray = monitorSteps;
        }
    }
    static clone(monitorSteps) {
        return MonitorSteps.fromJSON(monitorSteps.toJSON());
    }
    setDefaultMonitorStatusId(monitorStatusId) {
        if (this.data) {
            this.data.defaultMonitorStatusId = monitorStatusId;
        }
        return this;
    }
    toJSON() {
        var _a;
        if (!this.data) {
            return MonitorSteps.getNewMonitorStepsAsJSON();
        }
        return JSONFunctions.serialize({
            _type: ObjectType.MonitorSteps,
            value: {
                monitorStepsInstanceArray: this.data.monitorStepsInstanceArray.map((step) => {
                    return step.toJSON();
                }),
                defaultMonitorStatusId: ((_a = this.data.defaultMonitorStatusId) === null || _a === void 0 ? void 0 : _a.toString()) || undefined,
            },
        });
    }
    static fromJSON(json) {
        if (!json) {
            return new MonitorSteps();
        }
        if (json instanceof MonitorSteps) {
            return json;
        }
        if (!json) {
            throw new BadDataException("Invalid monitor steps");
        }
        if (json["_type"] !== "MonitorSteps") {
            throw new BadDataException("Invalid monitor steps");
        }
        if (!json["value"]) {
            throw new BadDataException("Invalid monitor steps");
        }
        if (!json["value"]["monitorStepsInstanceArray"]) {
            throw new BadDataException("Invalid monitor steps");
        }
        const monitorStepsInstanceArray = json["value"]["monitorStepsInstanceArray"];
        const monitorSteps = new MonitorSteps();
        monitorSteps.data = {
            monitorStepsInstanceArray: monitorStepsInstanceArray.map((json) => {
                return MonitorStep.fromJSON(json);
            }),
            defaultMonitorStatusId: json["value"]["defaultMonitorStatusId"]
                ? new ObjectID(json["value"]["defaultMonitorStatusId"])
                : undefined,
        };
        return monitorSteps;
    }
    static getValidationError(value, monitorType) {
        if (!value.data) {
            return "Monitor Steps is required";
        }
        if (value.data.monitorStepsInstanceArray.length === 0) {
            return "Monitor Steps is required";
        }
        if (!value.data.defaultMonitorStatusId) {
            return "Default Monitor Status is required";
        }
        for (const step of value.data.monitorStepsInstanceArray) {
            if (MonitorStep.getValidationError(step, monitorType)) {
                return MonitorStep.getValidationError(step, monitorType);
            }
        }
        return null;
    }
    static toDatabase(value) {
        if (value && value instanceof MonitorSteps) {
            return value.toJSON();
        }
        else if (value) {
            return JSONFunctions.serialize(value);
        }
        return null;
    }
    static fromDatabase(value) {
        if (!value) {
            return null;
        }
        /*
         * Be lenient when reading from the database. Some rows can be created
         * with malformed monitorSteps (e.g. via Terraform or older migrations),
         * and throwing here makes those rows un-deletable and un-readable —
         * `_deleteBy` selects every column for the audit-log snapshot, so a
         * single bad row blocks DELETE entirely. Strict validation still runs
         * via fromJSON / getValidationError on writes from the API layer, which
         * is the right place to reject bad input.
         */
        try {
            return MonitorSteps.fromJSON(value);
        }
        catch (_a) {
            return new MonitorSteps();
        }
    }
    toString() {
        return JSON.stringify(this.toJSON());
    }
    static getSchema() {
        return Zod.object({
            _type: Zod.literal(ObjectType.MonitorSteps),
            value: Zod.object({
                monitorStepsInstanceArray: Zod.array(MonitorStep.getSchema()),
                defaultMonitorStatusId: Zod.string().optional(),
            }).openapi({
                type: "object",
                example: {
                    monitorStepsInstanceArray: [],
                    defaultMonitorStatusId: undefined,
                },
            }),
        }).openapi({
            type: "object",
            description: "MonitorSteps object",
            example: {
                _type: ObjectType.MonitorSteps,
                value: {
                    monitorStepsInstanceArray: [],
                    defaultMonitorStatusId: undefined,
                },
            },
        });
    }
}
//# sourceMappingURL=MonitorSteps.js.map