"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StackTags = void 0;
const aws_cdk_lib_1 = require("aws-cdk-lib");
/**
 * Aspect that adds our expected stack tags to all stacks using values from context
 */
class StackTags {
    visit(node) {
        if (node instanceof aws_cdk_lib_1.Stack) {
            const projectName = this.getContext('projectName', node);
            // Not sure this makes sense to do for multistack applications, but for now it applies only
            // one description to all stacks. Perhaps this should just change to test if this tag was added
            const description = this.getContext('description', node);
            const contact = this.getContext('contact', node);
            const owner = this.getContext('owner', node);
            node.tags.setTag('ProjectName', projectName);
            node.tags.setTag('Contact', contact);
            node.tags.setTag('Owner', owner);
            node.tags.setTag('Description', description);
            if (!node.nested) {
                node.tags.setTag('Name', node.stackName);
            }
        }
    }
    /**
     *
     * @param keyName The name of the key to get from context
     * @param node The node to add errors to if the key does not exist
     */
    getContext(keyName, node) {
        const value = node.node.tryGetContext(keyName);
        if (value === undefined) {
            aws_cdk_lib_1.Annotations.of(node).addError(`Expected context key "${keyName}"`);
        }
        return value;
    }
}
exports.StackTags = StackTags;
