"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cdk = require("@aws-cdk/cdk");
const logs_generated_1 = require("./logs.generated");
/**
 * A new CloudWatch Logs Destination for use in cross-account scenarios
 *
 * Log destinations can be used to subscribe a Kinesis stream in a different
 * account to a CloudWatch Subscription. A Kinesis stream in the same account
 * can be subscribed directly.
 *
 * The @aws-cdk/aws-kinesis library takes care of this automatically; you shouldn't
 * need to bother with this class.
 */
class CrossAccountDestination extends cdk.Construct {
    constructor(parent, id, props) {
        super(parent, id);
        /**
         * Policy object of this CrossAccountDestination object
         */
        this.policyDocument = new cdk.PolicyDocument();
        // In the underlying model, the name is not optional, but we make it so anyway.
        const destinationName = props.destinationName || new cdk.Token(() => this.generateUniqueName());
        this.resource = new logs_generated_1.cloudformation.DestinationResource(this, 'Resource', {
            destinationName,
            // Must be stringified policy
            destinationPolicy: new cdk.Token(() => this.stringifiedPolicyDocument()),
            roleArn: props.role.roleArn,
            targetArn: props.targetArn
        });
        this.destinationArn = this.resource.destinationArn;
        this.destinationName = this.resource.ref;
    }
    addToPolicy(statement) {
        this.policyDocument.addStatement(statement);
    }
    logSubscriptionDestination(_sourceLogGroup) {
        return { arn: this.destinationArn };
    }
    /**
     * Generate a unique Destination name in case the user didn't supply one
     */
    generateUniqueName() {
        // Combination of stack name and LogicalID, which are guaranteed to be unique.
        const stack = cdk.Stack.find(this);
        return stack.name + '-' + this.resource.logicalId;
    }
    /**
     * Return a stringified JSON version of the PolicyDocument
     */
    stringifiedPolicyDocument() {
        return this.policyDocument.isEmpty ? '' : cdk.CloudFormationJSON.stringify(cdk.resolve(this.policyDocument));
    }
}
exports.CrossAccountDestination = CrossAccountDestination;
/**
 * Name of a CloudWatch Destination
 */
class DestinationName extends cdk.Token {
}
exports.DestinationName = DestinationName;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3Jvc3MtYWNjb3VudC1kZXN0aW5hdGlvbi5qcyIsInNvdXJjZVJvb3QiOiI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