"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.TopicBase=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),policy_1=require("./policy"),subscription_1=require("./subscription"),iam=require("../../aws-iam"),core_1=require("../../core");class TopicBase extends core_1.Resource{constructor(scope,id,props={}){super(scope,id,props);try{jsiiDeprecationWarnings.aws_cdk_lib_ResourceProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,TopicBase),error}this.node.addValidation({validate:()=>this.policy?.document.validateForResourcePolicy()??[]})}addSubscription(topicSubscription){try{jsiiDeprecationWarnings.aws_cdk_lib_aws_sns_ITopicSubscription(topicSubscription)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addSubscription),error}const subscriptionConfig=topicSubscription.bind(this),scope=subscriptionConfig.subscriberScope||this;let id=subscriptionConfig.subscriberId;if(core_1.Token.isUnresolved(subscriptionConfig.subscriberId)&&(id=this.nextTokenId(scope)),scope.node.tryFindChild(id))throw new Error(`A subscription with id "${id}" already exists under the scope ${scope.node.path}`);const subscription=new subscription_1.Subscription(scope,id,{topic:this,...subscriptionConfig});return subscriptionConfig.subscriptionDependency&&subscription.node.addDependency(subscriptionConfig.subscriptionDependency),subscription}addToResourcePolicy(statement){try{jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_PolicyStatement(statement)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.addToResourcePolicy),error}return!this.policy&&this.autoCreatePolicy&&(this.policy=new policy_1.TopicPolicy(this,"Policy",{topics:[this]})),this.policy?(this.policy.document.addStatements(statement),{statementAdded:!0,policyDependable:this.policy}):{statementAdded:!1}}grantPublish(grantee){try{jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_IGrantable(grantee)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,this.grantPublish),error}return iam.Grant.addToPrincipalOrResource({grantee,actions:["sns:Publish"],resourceArns:[this.topicArn],resource:this})}bindAsNotificationRuleTarget(_scope){return this.grantPublish(new iam.ServicePrincipal("codestar-notifications.amazonaws.com")),{targetType:"SNS",targetAddress:this.topicArn}}nextTokenId(scope){let nextSuffix=1;const re=/TokenSubscription:([\d]*)/gm;for(const source of scope.node.findAll()){const m=re.exec(source.node.id);if(m!==null){const matchSuffix=parseInt(m[1],10);matchSuffix>=nextSuffix&&(nextSuffix=matchSuffix+1)}}return`TokenSubscription:${nextSuffix}`}}exports.TopicBase=TopicBase,_a=JSII_RTTI_SYMBOL_1,TopicBase[_a]={fqn:"aws-cdk-lib.aws_sns.TopicBase",version:"2.96.1"};
