"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SlackPipelineStatusNotifications = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const aws_sns_1 = require("aws-cdk-lib/aws-sns");
const constructs_1 = require("constructs");
class SlackPipelineStatusNotifications extends constructs_1.Construct {
    constructor(scope, id, props) {
        super(scope, id);
        const eventInfo = aws_cdk_lib_1.aws_events.EventField.fromPath('$');
        const pipelineName = aws_cdk_lib_1.aws_events.EventField.fromPath('$.detail.pipeline');
        const pipelineState = aws_cdk_lib_1.aws_events.EventField.fromPath('$.detail.state');
        const pipelineBaseUrl = aws_cdk_lib_1.Fn.sub('https://${AWS::Region}.console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/');
        const testStackApprovalTopic = aws_sns_1.Topic.fromTopicArn(this, 'SlackTopicFromArn', props.slackNotifyTopicArn);
        const SlackPipelineStatusNotificationsData = {
            eventInfo: eventInfo,
            messageText: props.messageText,
            pipelineName: pipelineName,
            pipelineState: pipelineState,
            pipelineUrl: pipelineBaseUrl + pipelineName,
            slackChannelId: props.slackChannelId,
            slackChannelName: props.slackChannelName,
        };
        const message = aws_cdk_lib_1.aws_events.RuleTargetInput.fromObject(SlackPipelineStatusNotificationsData);
        const target = new aws_cdk_lib_1.aws_events_targets.SnsTopic(testStackApprovalTopic, { message });
        props.pipeline.onStateChange('OnPipelineStateChange', { target });
    }
}
exports.SlackPipelineStatusNotifications = SlackPipelineStatusNotifications;
