"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.LogRetention=void 0;var jsiiDeprecationWarnings=()=>{var tmp=require("../../.warnings.jsii.js");return jsiiDeprecationWarnings=()=>tmp,tmp};const JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti");var path=()=>{var tmp=require("path");return path=()=>tmp,tmp},constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},log_group_1=()=>{var tmp=require("./log-group");return log_group_1=()=>tmp,tmp},iam=()=>{var tmp=require("../../aws-iam");return iam=()=>tmp,tmp},s3_assets=()=>{var tmp=require("../../aws-s3-assets");return s3_assets=()=>tmp,tmp},cdk=()=>{var tmp=require("../../core");return cdk=()=>tmp,tmp},core_1=()=>{var tmp=require("../../core");return core_1=()=>tmp,tmp};class LogRetention extends constructs_1().Construct{constructor(scope,id,props){super(scope,id);try{jsiiDeprecationWarnings().aws_cdk_lib_aws_logs_LogRetentionProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,LogRetention),error}const provider=this.ensureSingletonLogRetentionFunction(props);props.removalPolicy===cdk().RemovalPolicy.DESTROY&&provider.grantDeleteLogGroup(props.logGroupName);const retryOptions=props.logRetentionRetryOptions,logGroupName=new(cdk()).CfnResource(this,"Resource",{type:"Custom::LogRetention",properties:{ServiceToken:provider.functionArn,LogGroupName:props.logGroupName,LogGroupRegion:props.logGroupRegion,SdkRetry:retryOptions?{maxRetries:retryOptions.maxRetries}:void 0,RetentionInDays:props.retention===log_group_1().RetentionDays.INFINITE?void 0:props.retention,RemovalPolicy:props.removalPolicy}}).getAtt("LogGroupName").toString();this.logGroupArn=cdk().Stack.of(this).formatArn({region:props.logGroupRegion,service:"logs",resource:"log-group",resourceName:`${logGroupName}:*`,arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME})}ensureSingletonLogRetentionFunction(props){const functionLogicalId="LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8a",existing=cdk().Stack.of(this).node.tryFindChild(functionLogicalId);return existing||new LogRetentionFunction(cdk().Stack.of(this),functionLogicalId,props)}}exports.LogRetention=LogRetention,_a=JSII_RTTI_SYMBOL_1,LogRetention[_a]={fqn:"aws-cdk-lib.aws_logs.LogRetention",version:"2.159.0"};class LogRetentionFunction extends constructs_1().Construct{constructor(scope,id,props){super(scope,id),this.tags=new(cdk()).TagManager(cdk().TagType.KEY_VALUE,"AWS::Lambda::Function");const asset=new(s3_assets()).Asset(this,"Code",{path:path().join(__dirname,"..","..","custom-resource-handlers","dist","aws-logs","log-retention-handler")}),role=props.role||new(iam()).Role(this,"ServiceRole",{assumedBy:new(iam()).ServicePrincipal("lambda.amazonaws.com"),managedPolicies:[iam().ManagedPolicy.fromAwsManagedPolicyName("service-role/AWSLambdaBasicExecutionRole")]});role.addToPrincipalPolicy(new(iam()).PolicyStatement({actions:["logs:PutRetentionPolicy","logs:DeleteRetentionPolicy"],resources:["*"]})),this.role=role;const resource=new(cdk()).CfnResource(this,"Resource",{type:"AWS::Lambda::Function",properties:{Handler:"index.handler",Runtime:cdk().determineLatestNodeRuntimeName(this),Timeout:cdk().Duration.minutes(15).toSeconds(),Code:{S3Bucket:asset.s3BucketName,S3Key:asset.s3ObjectKey},Role:role.roleArn,Tags:this.tags.renderedTags}});this.functionArn=resource.getAtt("Arn"),asset.addResourceMetadata(resource,"Code"),role.node.children.forEach(child=>{cdk().CfnResource.isCfnResource(child)&&resource.addDependency(child),constructs_1().Construct.isConstruct(child)&&child.node.defaultChild&&cdk().CfnResource.isCfnResource(child.node.defaultChild)&&resource.addDependency(child.node.defaultChild)})}grantDeleteLogGroup(logGroupName){this.role.addToPrincipalPolicy(new(iam()).PolicyStatement({actions:["logs:DeleteLogGroup"],resources:[cdk().Stack.of(this).formatArn({service:"logs",resource:"log-group",resourceName:`${logGroupName}:*`,arnFormat:core_1().ArnFormat.COLON_RESOURCE_NAME})]}))}}
