"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.TagManager=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),cfn_resource_1=require("./cfn-resource"),lazy_1=require("./lazy");class StandardFormatter{parseTags(cfnPropertyTags,priority){if(!Array.isArray(cfnPropertyTags))throw new Error(`Invalid tag input expected array of {key, value} have ${JSON.stringify(cfnPropertyTags)}`);const tags=[],dynamicTags=[];for(const tag of cfnPropertyTags)tag.key===void 0||tag.value===void 0?dynamicTags.push(tag):tags.push({key:`${tag.key}`,value:`${tag.value}`,priority});return{tags,dynamicTags}}formatTags(tags){const cfnTags=[];for(const tag of tags)cfnTags.push({key:tag.key,value:tag.value});return cfnTags}}class AsgFormatter{parseTags(cfnPropertyTags,priority){if(!Array.isArray(cfnPropertyTags))throw new Error(`Invalid tag input expected array of {key, value, propagateAtLaunch} have ${JSON.stringify(cfnPropertyTags)}`);const tags=[],dynamicTags=[];for(const tag of cfnPropertyTags)tag.key===void 0||tag.value===void 0||tag.propagateAtLaunch===void 0?dynamicTags.push(tag):tags.push({key:`${tag.key}`,value:`${tag.value}`,priority,applyToLaunchedInstances:!!tag.propagateAtLaunch});return{tags,dynamicTags}}formatTags(tags){const cfnTags=[];for(const tag of tags)cfnTags.push({key:tag.key,value:tag.value,propagateAtLaunch:tag.applyToLaunchedInstances!==!1});return cfnTags}}class MapFormatter{parseTags(cfnPropertyTags,priority){if(Array.isArray(cfnPropertyTags)||typeof cfnPropertyTags!="object")throw new Error(`Invalid tag input expected map of {key: value} have ${JSON.stringify(cfnPropertyTags)}`);const tags=[];for(const[key,value]of Object.entries(cfnPropertyTags))tags.push({key,value:`${value}`,priority});return{tags,dynamicTags:void 0}}formatTags(tags){const cfnTags={};for(const tag of tags)cfnTags[`${tag.key}`]=`${tag.value}`;return cfnTags}}class KeyValueFormatter{parseTags(keyValueTags,priority){const tags=[];for(const key in keyValueTags)if(keyValueTags.hasOwnProperty(key)){const value=keyValueTags[key];tags.push({key,value,priority})}return{tags,dynamicTags:void 0}}formatTags(unformattedTags){const tags=[];return unformattedTags.forEach(tag=>{tags.push({Key:tag.key,Value:tag.value})}),tags}}class NoFormat{parseTags(_cfnPropertyTags){return{tags:[],dynamicTags:void 0}}formatTags(_tags){}}let _tagFormattersCache;function TAG_FORMATTERS(){return _tagFormattersCache??(_tagFormattersCache={[cfn_resource_1.TagType.AUTOSCALING_GROUP]:new AsgFormatter,[cfn_resource_1.TagType.STANDARD]:new StandardFormatter,[cfn_resource_1.TagType.MAP]:new MapFormatter,[cfn_resource_1.TagType.KEY_VALUE]:new KeyValueFormatter,[cfn_resource_1.TagType.NOT_TAGGABLE]:new NoFormat})}class TagManager{constructor(tagType,resourceTypeName,tagStructure,options={}){this.tags=new Map,this.priorities=new Map,this.initialTagPriority=50;try{jsiiDeprecationWarnings.aws_cdk_lib_TagType(tagType),jsiiDeprecationWarnings.aws_cdk_lib_TagManagerOptions(options)}catch(error){throw process.env.JSII_DEBUG!=="1"&&error.name==="DeprecationError"&&Error.captureStackTrace(error,TagManager),error}if(this.resourceTypeName=resourceTypeName,this.tagFormatter=TAG_FORMATTERS()[tagType],tagStructure!==void 0){const parseTagsResult=this.tagFormatter.parseTags(tagStructure,this.initialTagPriority);this.dynamicTags=parseTagsResult.dynamicTags,this._setTag(...parseTagsResult.tags)}this.tagPropertyName=options.tagPropertyName||"tags",this.renderedTags=lazy_1.Lazy.any({produce:()=>this.renderTags()})}static isTaggable(construct){return construct.tags!==void 0}setTag(key,value,priority=0,applyToLaunchedInstances=!0){this._setTag({key,value,priority,applyToLaunchedInstances})}removeTag(key,priority){priority>=(this.priorities.get(key)||0)&&(this.tags.delete(key),this.priorities.set(key,priority))}renderTags(){const formattedTags=this.tagFormatter.formatTags(this.sortedTags);if(Array.isArray(formattedTags)||Array.isArray(this.dynamicTags)){const ret=[...formattedTags??[],...this.dynamicTags??[]];return ret.length>0?ret:void 0}else{const ret={...formattedTags??{},...this.dynamicTags??{}};return Object.keys(ret).length>0?ret:void 0}}tagValues(){const ret={};for(const tag of this.sortedTags)ret[tag.key]=tag.value;return ret}applyTagAspectHere(include,exclude){return!(exclude&&exclude.length>0&&exclude.indexOf(this.resourceTypeName)!==-1||include&&include.length>0&&include.indexOf(this.resourceTypeName)===-1)}hasTags(){return this.tags.size>0}_setTag(...tags){for(const tag of tags)tag.priority>=(this.priorities.get(tag.key)||0)&&(this.tags.set(tag.key,tag),this.priorities.set(tag.key,tag.priority))}get sortedTags(){return Array.from(this.tags.values()).sort((a,b)=>a.key.localeCompare(b.key))}}exports.TagManager=TagManager,_a=JSII_RTTI_SYMBOL_1,TagManager[_a]={fqn:"aws-cdk-lib.TagManager",version:"2.46.0"};
//# sourceMappingURL=tag-manager.js.map
