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 Response from "../../../Response";
import { MicrosoftTeamsOnCallDutyActionType } from "./ActionTypes";
import logger from "../../../Logger";
import CaptureSpan from "../../../Telemetry/CaptureSpan";
import { TurnContext } from "botbuilder";
import OnCallDutyPolicyService from "../../../../Services/OnCallDutyPolicyService";
import UserNotificationEventType from "../../../../../Types/UserNotification/UserNotificationEventType";
export default class MicrosoftTeamsOnCallDutyActions {
    static isOnCallDutyAction(data) {
        return (data.actionType === MicrosoftTeamsOnCallDutyActionType.ViewOnCallDuty ||
            data.actionType === MicrosoftTeamsOnCallDutyActionType.EscalateOnCall);
    }
    static async handleOnCallDutyAction(data) {
        const { action } = data;
        logger.debug("Handling Microsoft Teams on-call duty action:", {
            projectId: data.teamsRequest.projectId.toString(),
            actionType: action.actionType,
        });
        logger.debug(action);
        try {
            switch (action.actionType) {
                case MicrosoftTeamsOnCallDutyActionType.ViewOnCallDuty:
                    // This is handled by opening the URL directly
                    break;
                default:
                    logger.debug("Unhandled on-call duty action: " + action.actionType, {
                        projectId: data.teamsRequest.projectId.toString(),
                        actionType: action.actionType,
                    });
                    break;
            }
        }
        catch (error) {
            logger.error("Error handling Microsoft Teams on-call duty action:", {
                projectId: data.teamsRequest.projectId.toString(),
                actionType: action.actionType,
            });
            logger.error(error);
        }
        Response.sendTextResponse(data.req, data.res, "");
    }
    static async handleBotOnCallDutyAction(actionType, turnContext, actionPayload) {
        try {
            const onCallDutyPolicyId = actionPayload["onCallDutyPolicyId"];
            if (!onCallDutyPolicyId) {
                logger.error("OnCallDutyPolicy ID is required", {
                    actionType: actionType,
                });
                await turnContext.sendActivity("OnCallDutyPolicy ID is required");
                return;
            }
            const onCallDutyPolicy = await OnCallDutyPolicyService.findOneById({
                id: onCallDutyPolicyId,
                select: {
                    _id: true,
                    name: true,
                    description: true,
                },
                props: {
                    isRoot: true,
                },
            });
            if (!onCallDutyPolicy) {
                logger.error("OnCallDutyPolicy not found", {
                    onCallDutyPolicyId: onCallDutyPolicyId.toString(),
                });
                await turnContext.sendActivity("OnCallDutyPolicy not found");
                return;
            }
            switch (actionType) {
                case MicrosoftTeamsOnCallDutyActionType.ViewOnCallDuty:
                    await turnContext.sendActivity(`**${onCallDutyPolicy.name}**\n\n${onCallDutyPolicy.description || "No description"}`);
                    break;
                case MicrosoftTeamsOnCallDutyActionType.EscalateOnCall:
                    // TODO: Implement escalation logic
                    await OnCallDutyPolicyService.executePolicy(onCallDutyPolicyId, {
                        userNotificationEventType: UserNotificationEventType.IncidentCreated, // TODO: Get the correct event type
                    });
                    await turnContext.sendActivity("On-call policy escalated successfully");
                    break;
                default:
                    logger.error(`Unknown action type: ${actionType}`, {
                        onCallDutyPolicyId: onCallDutyPolicyId.toString(),
                        actionType: actionType,
                    });
                    await turnContext.sendActivity("Unknown action type");
                    break;
            }
        }
        catch (error) {
            logger.error(`Error handling on-call duty action: ${error}`, {
                actionType: actionType,
            });
            await turnContext.sendActivity("An error occurred while processing the action");
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Boolean)
], MicrosoftTeamsOnCallDutyActions, "isOnCallDutyAction", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], MicrosoftTeamsOnCallDutyActions, "handleOnCallDutyAction", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, TurnContext, Object]),
    __metadata("design:returntype", Promise)
], MicrosoftTeamsOnCallDutyActions, "handleBotOnCallDutyAction", null);
//# sourceMappingURL=OnCallDutyPolicy.js.map