"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.CustomResourceProvider=exports.CustomResourceProviderRuntime=void 0;const jsiiDeprecationWarnings=require("../../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),fs=require("fs"),path=require("path"),cxapi=require("../../../cx-api"),constructs_1=require("constructs"),asset_staging_1=require("../asset-staging"),assets_1=require("../assets"),cfn_resource_1=require("../cfn-resource"),duration_1=require("../duration"),size_1=require("../size"),stack_1=require("../stack"),token_1=require("../token"),ENTRYPOINT_FILENAME="__entrypoint__",ENTRYPOINT_NODEJS_SOURCE=path.join(__dirname,"nodejs-entrypoint.js");var CustomResourceProviderRuntime;(function(CustomResourceProviderRuntime2){CustomResourceProviderRuntime2.NODEJS_12_X="nodejs12.x",CustomResourceProviderRuntime2.NODEJS_12="nodejs12.x",CustomResourceProviderRuntime2.NODEJS_14_X="nodejs14.x"})(CustomResourceProviderRuntime=exports.CustomResourceProviderRuntime||(exports.CustomResourceProviderRuntime={}));class CustomResourceProvider extends constructs_1.Construct{constructor(scope,id,props){var _b,_c,_d;super(scope,id);jsiiDeprecationWarnings.aws_cdk_lib_CustomResourceProviderProps(props);const stack=stack_1.Stack.of(scope);if(fs.copyFileSync(ENTRYPOINT_NODEJS_SOURCE,path.join(props.codeDirectory,`${ENTRYPOINT_FILENAME}.js`)),!fs.existsSync(path.join(props.codeDirectory,"index.js")))throw new Error(`cannot find ${props.codeDirectory}/index.js`);const staging=new asset_staging_1.AssetStaging(this,"Staging",{sourcePath:props.codeDirectory}),assetFileName=staging.relativeStagedPath(stack),asset=stack.synthesizer.addFileAsset({fileName:assetFileName,sourceHash:staging.assetHash,packaging:assets_1.FileAssetPackaging.ZIP_DIRECTORY}),policies=props.policyStatements?[{PolicyName:"Inline",PolicyDocument:{Version:"2012-10-17",Statement:props.policyStatements}}]:void 0,role=new cfn_resource_1.CfnResource(this,"Role",{type:"AWS::IAM::Role",properties:{AssumeRolePolicyDocument:{Version:"2012-10-17",Statement:[{Action:"sts:AssumeRole",Effect:"Allow",Principal:{Service:"lambda.amazonaws.com"}}]},ManagedPolicyArns:[{"Fn::Sub":"arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"}],Policies:policies}});this.roleArn=token_1.Token.asString(role.getAtt("Arn"));const timeout=(_b=props.timeout)!==null&&_b!==void 0?_b:duration_1.Duration.minutes(15),memory=(_c=props.memorySize)!==null&&_c!==void 0?_c:size_1.Size.mebibytes(128),handler=new cfn_resource_1.CfnResource(this,"Handler",{type:"AWS::Lambda::Function",properties:{Code:{S3Bucket:asset.bucketName,S3Key:asset.objectKey},Timeout:timeout.toSeconds(),MemorySize:memory.toMebibytes(),Handler:`${ENTRYPOINT_FILENAME}.handler`,Role:role.getAtt("Arn"),Runtime:props.runtime,Environment:this.renderEnvironmentVariables(props.environment),Description:(_d=props.description)!==null&&_d!==void 0?_d:void 0}});handler.addDependsOn(role),this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)&&(handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PATH_KEY,assetFileName),handler.addMetadata(cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY,"Code")),this.serviceToken=token_1.Token.asString(handler.getAtt("Arn"))}static getOrCreate(scope,uniqueid,props){return jsiiDeprecationWarnings.aws_cdk_lib_CustomResourceProviderProps(props),this.getOrCreateProvider(scope,uniqueid,props).serviceToken}static getOrCreateProvider(scope,uniqueid,props){var _b;jsiiDeprecationWarnings.aws_cdk_lib_CustomResourceProviderProps(props);const id=`${uniqueid}CustomResourceProvider`,stack=stack_1.Stack.of(scope);return(_b=stack.node.tryFindChild(id))!==null&&_b!==void 0?_b:new CustomResourceProvider(stack,id,props)}renderEnvironmentVariables(env){if(!env||Object.keys(env).length===0)return;const variables={},keys=Object.keys(env).sort();for(const key of keys)variables[key]=env[key];return{Variables:variables}}}exports.CustomResourceProvider=CustomResourceProvider,_a=JSII_RTTI_SYMBOL_1,CustomResourceProvider[_a]={fqn:"aws-cdk-lib.CustomResourceProvider",version:"2.13.0"};
//# sourceMappingURL=custom-resource-provider.js.map
