"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const assets = require("@aws-cdk/assets");
class Code {
    /**
     * @returns `LambdaS3Code` associated with the specified S3 object.
     * @param bucket The S3 bucket
     * @param key The object key
     * @param objectVersion Optional S3 object version
     */
    static bucket(bucket, key, objectVersion) {
        return new S3Code(bucket, key, objectVersion);
    }
    /**
     * @returns `LambdaInlineCode` with inline code.
     * @param code The actual handler code (limited to 4KiB)
     */
    static inline(code) {
        return new InlineCode(code);
    }
    /**
     * @returns Zip archives the contents of a directory on disk and uses this
     * as the lambda handler's code.
     * @param directoryToZip The directory to zip
     */
    static directory(directoryToZip) {
        return new AssetCode(directoryToZip, assets.AssetPackaging.ZipDirectory);
    }
    /**
     * @returns Uses a file on disk as a lambda handler's code.
     * @param filePath The file path
     */
    static file(filePath) {
        return new AssetCode(filePath, assets.AssetPackaging.File);
    }
    /**
     * Called when the lambda is initialized to allow this object to
     * bind to the stack, add resources and have fun.
     */
    bind(_lambda) {
        return;
    }
}
exports.Code = Code;
/**
 * Lambda code from an S3 archive.
 */
class S3Code extends Code {
    constructor(bucket, key, objectVersion) {
        super();
        this.key = key;
        this.objectVersion = objectVersion;
        if (!bucket.bucketName) {
            throw new Error('bucketName is undefined for the provided bucket');
        }
        this.bucketName = bucket.bucketName;
    }
    toJSON() {
        return {
            s3Bucket: this.bucketName,
            s3Key: this.key,
            s3ObjectVersion: this.objectVersion
        };
    }
}
exports.S3Code = S3Code;
/**
 * Lambda code from an inline string (limited to 4KiB).
 */
class InlineCode extends Code {
    constructor(code) {
        super();
        this.code = code;
        if (code.length > 4096) {
            throw new Error("Lambda source is too large, must be <= 4096 but is " + code.length);
        }
    }
    bind(lambda) {
        if (!lambda.runtime.supportsInlineCode) {
            throw new Error(`Inline source not allowed for ${lambda.runtime.name}`);
        }
    }
    toJSON() {
        return {
            zipFile: this.code
        };
    }
}
exports.InlineCode = InlineCode;
/**
 * Lambda code from a local directory.
 */
class AssetCode extends Code {
    /**
     * @param path The path to the asset file or directory.
     * @param packaging The asset packaging format
     */
    constructor(path, packaging) {
        super();
        this.path = path;
        this.packaging = packaging;
    }
    bind(lambda) {
        this.asset = new assets.Asset(lambda, 'Code', {
            path: this.path,
            packaging: this.packaging
        });
        this.asset.grantRead(lambda.role);
    }
    toJSON() {
        return {
            s3Bucket: this.asset.s3BucketName,
            s3Key: this.asset.s3ObjectKey
        };
    }
}
exports.AssetCode = AssetCode;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29kZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbImNvZGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7QUFBQSwwQ0FBMkM7QUFLM0MsTUFBc0IsSUFBSTtJQUN0Qjs7Ozs7T0FLRztJQUNJLE1BQU0sQ0FBQyxNQUFNLENBQUMsTUFBb0IsRUFBRSxHQUFXLEVBQUUsYUFBc0I7UUFDMUUsT0FBTyxJQUFJLE1BQU0sQ0FBQyxNQUFNLEVBQUUsR0FBRyxFQUFFLGFBQWEsQ0FBQyxDQUFDO0lBQ2xELENBQUM7SUFFRDs7O09BR0c7SUFDSSxNQUFNLENBQUMsTUFBTSxDQUFDLElBQVk7UUFDN0IsT0FBTyxJQUFJLFVBQVUsQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNoQyxDQUFDO0lBRUQ7Ozs7T0FJRztJQUNJLE1BQU0sQ0FBQyxTQUFTLENBQUMsY0FBc0I7UUFDMUMsT0FBTyxJQUFJLFNBQVMsQ0FBQyxjQUFjLEVBQUUsTUFBTSxDQUFDLGNBQWMsQ0FBQyxZQUFZLENBQUMsQ0FBQztJQUM3RSxDQUFDO0lBRUQ7OztPQUdHO0lBQ0ksTUFBTSxDQUFDLElBQUksQ0FBQyxRQUFnQjtRQUMvQixPQUFPLElBQUksU0FBUyxDQUFDLFFBQVEsRUFBRSxNQUFNLENBQUMsY0FBYyxDQUFDLElBQUksQ0FBQyxDQUFDO0l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