"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const kms = require("@aws-cdk/aws-kms");
const queue_ref_1 = require("./queue-ref");
const sqs_generated_1 = require("./sqs.generated");
const validate_props_1 = require("./validate-props");
/**
 * What kind of encryption to apply to this queue
 */
var QueueEncryption;
(function (QueueEncryption) {
    /**
     * Messages in the queue are not encrypted
     */
    QueueEncryption["Unencrypted"] = "NONE";
    /**
     * Server-side KMS encryption with a master key managed by SQS.
     */
    QueueEncryption["KmsManaged"] = "MANAGED";
    /**
     * Server-side encryption with a KMS key managed by the user.
     *
     * If `encryptionKey` is specified, this key will be used, otherwise, one will be defined.
     */
    QueueEncryption["Kms"] = "KMS";
})(QueueEncryption = exports.QueueEncryption || (exports.QueueEncryption = {}));
/**
 * A new Amazon SQS queue
 */
class Queue extends queue_ref_1.QueueRef {
    constructor(parent, name, props = {}) {
        super(parent, name);
        this.autoCreatePolicy = true;
        validate_props_1.validateProps(props);
        const redrivePolicy = props.deadLetterQueue
            ? {
                deadLetterTargetArn: props.deadLetterQueue.queue.queueArn,
                maxReceiveCount: props.deadLetterQueue.maxReceiveCount
            }
            : undefined;
        const queue = new sqs_generated_1.cloudformation.QueueResource(this, 'Resource', Object.assign({ queueName: props.queueName }, this.determineFifoProps(props), this.determineEncryptionProps(props), { redrivePolicy, delaySeconds: props.deliveryDelaySec, maximumMessageSize: props.maxMessageSizeBytes, messageRetentionPeriod: props.retentionPeriodSec, receiveMessageWaitTimeSeconds: props.receiveMessageWaitTimeSec, visibilityTimeout: props.visibilityTimeoutSec }));
        this.queueArn = queue.queueArn;
        this.queueName = queue.queueName;
        this.queueUrl = queue.ref;
    }
    /**
     * Look at the props, see if the FIFO props agree, and return the correct subset of props
     */
    determineFifoProps(props) {
        // Check if any of the signals that we have say that this is a FIFO queue.
        let fifoQueue = props.fifo;
        if (typeof fifoQueue === 'undefined' && typeof props.queueName === 'string' && props.queueName.endsWith('.fifo')) {
            fifoQueue = true;
        }
        if (typeof fifoQueue === 'undefined' && props.contentBasedDeduplication) {
            fifoQueue = true;
        }
        // If we have a name, see that it agrees with the FIFO setting
        if (typeof props.queueName === 'string') {
            if (fifoQueue && !props.queueName.endsWith('.fifo')) {
                throw new Error("FIFO queue names must end in '.fifo'");
            }
            if (!fifoQueue && props.queueName.endsWith('.fifo')) {
                throw new Error("Non-FIFO queue name may not end in '.fifo'");
            }
        }
        if (props.contentBasedDeduplication && !fifoQueue) {
            throw new Error('Content-based deduplication can only be defined for FIFO queues');
        }
        return {
            contentBasedDeduplication: props.contentBasedDeduplication,
            fifoQueue,
        };
    }
    determineEncryptionProps(props) {
        let encryption = props.encryption || QueueEncryption.Unencrypted;
        if (encryption !== QueueEncryption.Kms && props.encryptionMasterKey) {
            encryption = QueueEncryption.Kms; // KMS is implied by specifying an encryption key
        }
        if (encryption === QueueEncryption.Unencrypted) {
            return {};
        }
        if (encryption === QueueEncryption.KmsManaged) {
            this.encryptionMasterKey = kms.EncryptionKey.import(this, 'Key', {
                keyArn: new kms.KeyArn('alias/aws/sqs')
            });
            return {
                kmsMasterKeyId: 'alias/aws/sqs',
                kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
            };
        }
        if (encryption === QueueEncryption.Kms) {
            this.encryptionMasterKey = props.encryptionMasterKey || new kms.EncryptionKey(this, 'Key', {
                description: `Created by ${this.path}`
            });
            return {
                kmsMasterKeyId: this.encryptionMasterKey.keyArn,
                kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
            };
        }
        throw new Error(`Unexpected 'encryptionType': ${encryption}`);
    }
}
exports.Queue = Queue;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicXVldWUuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJxdWV1ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHdDQUF5QztBQUV6QywyQ0FBaUQ7QUFDakQsbURBQXNFO0FBQ3RFLHFEQUFpRDtBQXdKakQ7O0dBRUc7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