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 { Blue500 } from "../../Types/BrandColors";
import OneUptimeDate from "../../Types/Date";
import BadDataException from "../../Types/Exception/BadDataException";
import { IsBillingEnabled } from "../EnvironmentConfig";
import logger from "../Utils/Logger";
import DatabaseService from "./DatabaseService";
import MonitorFeed from "../../Models/DatabaseModels/MonitorFeed";
import WorkspaceNotificationRuleService from "./WorkspaceNotificationRuleService";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export class Service extends DatabaseService {
    constructor() {
        super(MonitorFeed);
        if (IsBillingEnabled) {
            this.hardDeleteItemsOlderThanInDays("createdAt", 3 * 365); // 3 years
        }
    }
    async createMonitorFeedItem(data) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
        try {
            logger.debug("MonitorFeedService.createMonitorFeedItem", {
                projectId: (_a = data.projectId) === null || _a === void 0 ? void 0 : _a.toString(),
                monitorId: (_b = data.monitorId) === null || _b === void 0 ? void 0 : _b.toString(),
            });
            logger.debug(data, {
                projectId: (_c = data.projectId) === null || _c === void 0 ? void 0 : _c.toString(),
                monitorId: (_d = data.monitorId) === null || _d === void 0 ? void 0 : _d.toString(),
            });
            const monitorFeed = new MonitorFeed();
            if (!data.monitorId) {
                throw new BadDataException("Monitor ID is required");
            }
            if (!data.feedInfoInMarkdown) {
                throw new BadDataException("Log in markdown is required");
            }
            if (!data.monitorFeedEventType) {
                throw new BadDataException("Monitor log event is required");
            }
            if (!data.projectId) {
                throw new BadDataException("Project ID is required");
            }
            if (!data.displayColor) {
                data.displayColor = Blue500;
            }
            monitorFeed.displayColor = data.displayColor;
            monitorFeed.monitorId = data.monitorId;
            monitorFeed.feedInfoInMarkdown = data.feedInfoInMarkdown;
            monitorFeed.monitorFeedEventType = data.monitorFeedEventType;
            monitorFeed.projectId = data.projectId;
            if (!data.postedAt) {
                monitorFeed.postedAt = OneUptimeDate.getCurrentDate();
            }
            if (data.userId) {
                monitorFeed.userId = data.userId;
            }
            if (data.moreInformationInMarkdown) {
                monitorFeed.moreInformationInMarkdown = data.moreInformationInMarkdown;
            }
            const createdMonitorFeed = await this.create({
                data: monitorFeed,
                props: {
                    isRoot: true,
                },
            });
            logger.debug("Monitor Feed created", {
                projectId: (_e = data.projectId) === null || _e === void 0 ? void 0 : _e.toString(),
                monitorId: (_f = data.monitorId) === null || _f === void 0 ? void 0 : _f.toString(),
            });
            logger.debug(createdMonitorFeed, {
                projectId: (_g = data.projectId) === null || _g === void 0 ? void 0 : _g.toString(),
                monitorId: (_h = data.monitorId) === null || _h === void 0 ? void 0 : _h.toString(),
            });
            try {
                // send notification to slack and teams
                if (data.workspaceNotification &&
                    ((_j = data.workspaceNotification) === null || _j === void 0 ? void 0 : _j.sendWorkspaceNotification)) {
                    await this.sendWorkspaceNotification({
                        projectId: data.projectId,
                        monitorId: data.monitorId,
                        feedInfoInMarkdown: data.feedInfoInMarkdown,
                        workspaceNotification: data.workspaceNotification,
                    });
                }
            }
            catch (e) {
                logger.error("Error in sending notification to slack and teams", {
                    projectId: (_k = data.projectId) === null || _k === void 0 ? void 0 : _k.toString(),
                    monitorId: (_l = data.monitorId) === null || _l === void 0 ? void 0 : _l.toString(),
                });
                logger.error(e, {
                    projectId: (_m = data.projectId) === null || _m === void 0 ? void 0 : _m.toString(),
                    monitorId: (_o = data.monitorId) === null || _o === void 0 ? void 0 : _o.toString(),
                });
                // we dont throw this error as it is not a critical error
            }
        }
        catch (e) {
            logger.error("Error in creating monitor feed", {
                projectId: (_p = data.projectId) === null || _p === void 0 ? void 0 : _p.toString(),
                monitorId: (_q = data.monitorId) === null || _q === void 0 ? void 0 : _q.toString(),
            });
            logger.error(e, {
                projectId: (_r = data.projectId) === null || _r === void 0 ? void 0 : _r.toString(),
                monitorId: (_s = data.monitorId) === null || _s === void 0 ? void 0 : _s.toString(),
            });
            // we dont throw this error as it is not a critical error
        }
    }
    async sendWorkspaceNotification(data) {
        return await WorkspaceNotificationRuleService.sendWorkspaceMarkdownNotification({
            projectId: data.projectId,
            notificationFor: {
                monitorId: data.monitorId,
            },
            feedInfoInMarkdown: data.feedInfoInMarkdown,
            workspaceNotification: data.workspaceNotification,
        });
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "createMonitorFeedItem", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], Service.prototype, "sendWorkspaceNotification", null);
export default new Service();
//# sourceMappingURL=MonitorFeedService.js.map