"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.CfnMapping=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),annotations_1=require("./annotations"),cfn_element_1=require("./cfn-element"),cfn_fn_1=require("./cfn-fn"),stack_1=require("./stack"),token_1=require("./token");class CfnMapping extends cfn_element_1.CfnRefElement{constructor(scope,id,props={}){super(scope,id),this.lazyRender=!1,this.lazyInformed=!1;try{jsiiDeprecationWarnings.aws_cdk_lib_CfnMappingProps(props)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,CfnMapping),error}this.mapping=props.mapping?this.validateMapping(props.mapping):{},this.lazy=props.lazy}setValue(key1,key2,value){this.validateAlphanumeric(key2),key1 in this.mapping||(this.mapping[key1]={}),this.mapping[key1][key2]=value}findInMap(key1,key2,defaultValue){let fullyResolved=!1,notInMap=!1;if(!token_1.Token.isUnresolved(key1))if(key1 in this.mapping){if(!token_1.Token.isUnresolved(key2)){if(!(key2 in this.mapping[key1])){if(defaultValue===void 0)throw new Error(`Mapping doesn't contain second-level key '${key2}'`);notInMap=!0}fullyResolved=!0}}else{if(defaultValue===void 0)throw new Error(`Mapping doesn't contain top-level key '${key1}'`);notInMap=!0}if(this.lazy){if(notInMap&&defaultValue!==void 0)return defaultValue;if(fullyResolved)return this.mapping[key1][key2]}return this.lazyRender=!fullyResolved,new CfnMappingEmbedder(this,this.mapping,key1,key2,defaultValue).toString()}_toCloudFormation(){return this.lazy===void 0&&!this.lazyRender&&this.informLazyUse(),!this.lazy||this.lazy&&this.lazyRender?{Mappings:{[this.logicalId]:this.mapping}}:{}}informLazyUse(){this.lazyInformed||annotations_1.Annotations.of(this).addInfo("Consider making this CfnMapping a lazy mapping by providing `lazy: true`: either no findInMap was called or every findInMap could be immediately resolved without using Fn::FindInMap"),this.lazyInformed=!0}validateMapping(mapping){return Object.keys(mapping).forEach(m=>Object.keys(mapping[m]).forEach(this.validateAlphanumeric)),mapping}validateAlphanumeric(value){if(value.match(/[^a-zA-Z0-9]/g))throw new Error(`Attribute name '${value}' must contain only alphanumeric characters.`)}}exports.CfnMapping=CfnMapping,_a=JSII_RTTI_SYMBOL_1,CfnMapping[_a]={fqn:"aws-cdk-lib.CfnMapping",version:"2.96.1"};class CfnMappingEmbedder{constructor(cfnMapping,mapping,key1,key2,defaultValue){this.cfnMapping=cfnMapping,this.mapping=mapping,this.key1=key1,this.key2=key2,this.defaultValue=defaultValue,this.creationStack=[]}resolve(context){const consumingStack=stack_1.Stack.of(context.scope);if(consumingStack===stack_1.Stack.of(this.cfnMapping))return cfn_fn_1.Fn.findInMap(this.cfnMapping.logicalId,this.key1,this.key2,this.defaultValue);const constructScope=consumingStack,constructId=`MappingCopy-${this.cfnMapping.node.id}-${this.cfnMapping.node.addr}`;let mappingCopy=constructScope.node.tryFindChild(constructId);return mappingCopy||(mappingCopy=new CfnMapping(constructScope,constructId,{mapping:this.mapping})),cfn_fn_1.Fn.findInMap(mappingCopy.logicalId,this.key1,this.key2,this.defaultValue)}toString(){return token_1.Token.asString(this)}}
