"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.Alias=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),appscaling=require("../../aws-applicationautoscaling"),iam=require("../../aws-iam"),core_1=require("../../core"),function_base_1=require("./function-base"),lambda_version_1=require("./lambda-version"),lambda_generated_1=require("./lambda.generated"),scalable_function_attribute_1=require("./private/scalable-function-attribute");class Alias extends function_base_1.QualifiedFunctionBase{constructor(scope,id,props){super(scope,id,{physicalName:props.aliasName});this.canCreatePermissions=!0,jsiiDeprecationWarnings.aws_cdk_lib_aws_lambda_AliasProps(props),this.lambda=props.version.lambda,this.aliasName=this.physicalName,this.version=props.version,this.architecture=this.lambda.architecture;const alias=new lambda_generated_1.CfnAlias(this,"Resource",{name:this.aliasName,description:props.description,functionName:this.version.lambda.functionName,functionVersion:props.version.version,routingConfig:this.determineRoutingConfig(props),provisionedConcurrencyConfig:this.determineProvisionedConcurrency(props)});this.scalingRole=iam.Role.fromRoleArn(this,"ScalingRole",this.stack.formatArn({service:"iam",region:"",resource:"role/aws-service-role/lambda.application-autoscaling.amazonaws.com",resourceName:"AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency"})),this.functionArn=this.getResourceArnAttribute(alias.ref,{service:"lambda",resource:"function",resourceName:`${this.lambda.functionName}:${this.physicalName}`,arnFormat:core_1.ArnFormat.COLON_RESOURCE_NAME}),this.qualifier=lambda_version_1.extractQualifierFromArn(alias.ref),(props.onFailure||props.onSuccess||props.maxEventAge||props.retryAttempts!==void 0)&&this.configureAsyncInvoke({onFailure:props.onFailure,onSuccess:props.onSuccess,maxEventAge:props.maxEventAge,retryAttempts:props.retryAttempts}),this.functionName=`${this.stack.splitArn(this.functionArn,core_1.ArnFormat.COLON_RESOURCE_NAME).resourceName}:${this.aliasName}`}static fromAliasAttributes(scope,id,attrs){jsiiDeprecationWarnings.aws_cdk_lib_aws_lambda_AliasAttributes(attrs);class Imported extends function_base_1.QualifiedFunctionBase{constructor(){super(...arguments);this.aliasName=attrs.aliasName,this.version=attrs.aliasVersion,this.lambda=attrs.aliasVersion.lambda,this.functionArn=`${attrs.aliasVersion.lambda.functionArn}:${attrs.aliasName}`,this.functionName=`${attrs.aliasVersion.lambda.functionName}:${attrs.aliasName}`,this.grantPrincipal=attrs.aliasVersion.grantPrincipal,this.role=attrs.aliasVersion.role,this.architecture=attrs.aliasVersion.lambda.architecture,this.canCreatePermissions=this._isStackAccount(),this.qualifier=attrs.aliasName}}return new Imported(scope,id)}get grantPrincipal(){return this.version.grantPrincipal}get role(){return this.version.role}metric(metricName,props={}){return jsiiDeprecationWarnings.aws_cdk_lib_aws_cloudwatch_MetricOptions(props),super.metric(metricName,{dimensionsMap:{FunctionName:this.lambda.functionName,Resource:`${this.lambda.functionName}:${this.aliasName}`},...props})}addAutoScaling(options){var _b;if(jsiiDeprecationWarnings.aws_cdk_lib_aws_lambda_AutoScalingOptions(options),this.scalableAlias)throw new Error("AutoScaling already enabled for this alias");return this.scalableAlias=new scalable_function_attribute_1.ScalableFunctionAttribute(this,"AliasScaling",{minCapacity:(_b=options.minCapacity)!==null&&_b!==void 0?_b:1,maxCapacity:options.maxCapacity,resourceId:`function:${this.functionName}`,dimension:"lambda:function:ProvisionedConcurrency",serviceNamespace:appscaling.ServiceNamespace.LAMBDA,role:this.scalingRole})}determineRoutingConfig(props){if(!(!props.additionalVersions||props.additionalVersions.length===0))return this.validateAdditionalWeights(props.additionalVersions),{additionalVersionWeights:props.additionalVersions.map(vw=>({functionVersion:vw.version.version,functionWeight:vw.weight}))}}validateAdditionalWeights(weights){const total=weights.map(w=>{if(w.weight<0||w.weight>1)throw new Error(`Additional version weight must be between 0 and 1, got: ${w.weight}`);return w.weight}).reduce((a,x)=>a+x);if(total>1)throw new Error(`Sum of additional version weights must not exceed 1, got: ${total}`)}determineProvisionedConcurrency(props){if(!!props.provisionedConcurrentExecutions){if(props.provisionedConcurrentExecutions<=0)throw new Error("provisionedConcurrentExecutions must have value greater than or equal to 1");return{provisionedConcurrentExecutions:props.provisionedConcurrentExecutions}}}}exports.Alias=Alias,_a=JSII_RTTI_SYMBOL_1,Alias[_a]={fqn:"aws-cdk-lib.aws_lambda.Alias",version:"2.13.0"};
//# sourceMappingURL=alias.js.map
