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 MonitorStatusTimeline from "../../../Models/DatabaseModels/MonitorStatusTimeline";
import SortOrder from "../../../Types/BaseDatabase/SortOrder";
import BadDataException from "../../../Types/Exception/BadDataException";
import MonitorStatusTimelineService from "../../Services/MonitorStatusTimelineService";
import logger from "../Logger";
import CaptureSpan from "../Telemetry/CaptureSpan";
export default class MonitorStatusTimelineUtil {
    static async updateMonitorStatusTimeline(input) {
        // criteria filters are met, now process the actions.
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
        const lastMonitorStatusTimeline = await MonitorStatusTimelineService.findOneBy({
            query: {
                monitorId: input.monitor.id,
                projectId: input.monitor.projectId,
            },
            select: {
                _id: true,
                monitorStatusId: true,
            },
            sort: {
                startsAt: SortOrder.Descending,
            },
            props: {
                isRoot: true,
            },
        });
        let shouldUpdateStatus = false;
        if (!lastMonitorStatusTimeline) {
            // if monitor does not have any status timeline, then create one.
            shouldUpdateStatus = true;
        }
        if (((_a = input.criteriaInstance.data) === null || _a === void 0 ? void 0 : _a.changeMonitorStatus) &&
            ((_b = input.criteriaInstance.data) === null || _b === void 0 ? void 0 : _b.monitorStatusId) &&
            ((_c = input.criteriaInstance.data) === null || _c === void 0 ? void 0 : _c.monitorStatusId.toString()) !==
                ((_d = lastMonitorStatusTimeline === null || lastMonitorStatusTimeline === void 0 ? void 0 : lastMonitorStatusTimeline.id) === null || _d === void 0 ? void 0 : _d.toString())) {
            // if monitor status is changed, then create a new status timeline.
            shouldUpdateStatus = true;
        }
        // check if the current status is same as the last status.
        if (((_e = input.criteriaInstance.data) === null || _e === void 0 ? void 0 : _e.changeMonitorStatus) &&
            ((_f = input.criteriaInstance.data) === null || _f === void 0 ? void 0 : _f.monitorStatusId) &&
            ((_g = input.criteriaInstance.data) === null || _g === void 0 ? void 0 : _g.monitorStatusId.toString()) !==
                ((_h = input.monitor.currentMonitorStatusId) === null || _h === void 0 ? void 0 : _h.toString())) {
            // if monitor status is changed, then create a new status timeline.
            shouldUpdateStatus = true;
        }
        if (shouldUpdateStatus) {
            const monitorLogAttributes = {
                projectId: (_j = input.monitor.projectId) === null || _j === void 0 ? void 0 : _j.toString(),
            };
            logger.debug(`${(_k = input.monitor.id) === null || _k === void 0 ? void 0 : _k.toString()} - Change monitor status to ${(_m = (_l = input.criteriaInstance.data) === null || _l === void 0 ? void 0 : _l.monitorStatusId) === null || _m === void 0 ? void 0 : _m.toString()}`, monitorLogAttributes);
            // change monitor status
            const monitorStatusId = (_o = input.criteriaInstance.data) === null || _o === void 0 ? void 0 : _o.monitorStatusId;
            if (!monitorStatusId) {
                throw new BadDataException("Monitor status is not defined.");
            }
            //change monitor status.
            // get last status of this monitor.
            // get last monitor status timeline.
            if (lastMonitorStatusTimeline &&
                lastMonitorStatusTimeline.monitorStatusId &&
                lastMonitorStatusTimeline.monitorStatusId.toString() ===
                    monitorStatusId.toString()) {
                // status is same as last status. do not create new status timeline.
                return null;
            }
            const monitorStatusTimeline = new MonitorStatusTimeline();
            monitorStatusTimeline.monitorId = input.monitor.id;
            monitorStatusTimeline.monitorStatusId = monitorStatusId;
            monitorStatusTimeline.projectId = input.monitor.projectId;
            monitorStatusTimeline.statusChangeLog = JSON.parse(JSON.stringify(input.dataToProcess));
            monitorStatusTimeline.rootCause = input.rootCause;
            return await MonitorStatusTimelineService.create({
                data: monitorStatusTimeline,
                props: {
                    isRoot: true,
                },
            });
        }
        return null;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], MonitorStatusTimelineUtil, "updateMonitorStatusTimeline", null);
//# sourceMappingURL=MonitorStatusTimeline.js.map