"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cdk_1 = require("@aws-cdk/cdk");
function validateBucketName(bucketName) {
    if (!bucketName) {
        return;
    }
    if (bucketName.search(/[^a-z0-9.-]/g) !== -1) {
        throw new Error('Bucket name contains invalid characters: ' + bucketName);
    }
}
exports.validateBucketName = validateBucketName;
function parseBucketArn(props) {
    // if we have an explicit bucket ARN, use it.
    if (props.bucketArn) {
        return props.bucketArn;
    }
    if (props.bucketName) {
        return cdk_1.Arn.fromComponents({
            // S3 Bucket names are globally unique in a partition,
            // and so their ARNs have empty region and account components
            region: '',
            account: '',
            service: 's3',
            resource: props.bucketName
        });
    }
    throw new Error('Cannot determine bucket ARN. At least `bucketArn` or `bucketName` is needed');
}
exports.parseBucketArn = parseBucketArn;
function parseBucketName(props) {
    // if we have an explicit bucket name, use it.
    if (props.bucketName) {
        return props.bucketName;
    }
    // if we have a string arn, we can extract the bucket name from it.
    if (props.bucketArn) {
        const resolved = props.bucketArn.resolve();
        if (typeof (resolved) === 'string') {
            const components = cdk_1.Arn.parse(resolved);
            if (components.service !== 's3') {
                throw new Error('Invalid ARN. Expecting "s3" service:' + resolved);
            }
            if (components.resourceName) {
                throw new Error(`Bucket ARN must not contain a path`);
            }
            return components.resource;
        }
    }
    // no bucket name is okay since it's optional.
    return undefined;
}
exports.parseBucketName = parseBucketName;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInV0aWwudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSxzQ0FBbUM7QUFHbkMsU0FBZ0Isa0JBQWtCLENBQUMsVUFBbUI7SUFDbEQsSUFBSSxDQUFDLFVBQVUsRUFBRTtRQUNiLE9BQU87S0FDVjtJQUVELElBQUksVUFBVSxDQUFDLE1BQU0sQ0FBQyxjQUFjLENBQUMsS0FBSyxDQUFDLENBQUMsRUFBRTtRQ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