"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PipelineNotifications = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
const constructs_1 = require("constructs");
class PipelineNotifications extends constructs_1.Construct {
    constructor(scope, id, props) {
        super(scope, id);
        const topic = new aws_cdk_lib_1.aws_sns.Topic(this, 'PipelineNotifications');
        new aws_cdk_lib_1.aws_sns.Subscription(this, 'PipelineSubscription', {
            endpoint: props.receivers,
            protocol: aws_cdk_lib_1.aws_sns.SubscriptionProtocol.EMAIL,
            topic,
        });
        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 message = props.messageText !== undefined
            ? aws_cdk_lib_1.aws_events.RuleTargetInput.fromText(props.messageText)
            : aws_cdk_lib_1.aws_events.RuleTargetInput.fromText(`The pipeline ${pipelineName} has changed state to ${pipelineState}. ` +
                `To view the pipeline, go to ${pipelineBaseUrl + pipelineName}.`);
        const target = new aws_cdk_lib_1.aws_events_targets.SnsTopic(topic, { message });
        props.pipeline.onStateChange('OnStateChange', { target });
    }
}
exports.PipelineNotifications = PipelineNotifications;
