"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.Stage=exports.MethodLoggingLevel=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),core_1=require("../../core"),access_log_1=require("./access-log"),apigateway_generated_1=require("./apigateway.generated"),restapi_1=require("./restapi"),util_1=require("./util");var MethodLoggingLevel;(function(MethodLoggingLevel2){MethodLoggingLevel2.OFF="OFF",MethodLoggingLevel2.ERROR="ERROR",MethodLoggingLevel2.INFO="INFO"})(MethodLoggingLevel=exports.MethodLoggingLevel||(exports.MethodLoggingLevel={}));class Stage extends core_1.Resource{constructor(scope,id,props){super(scope,id);jsiiDeprecationWarnings.aws_cdk_lib_aws_apigateway_StageProps(props),this.enableCacheCluster=props.cacheClusterEnabled;const methodSettings=this.renderMethodSettings(props);let accessLogSetting;const accessLogDestination=props.accessLogDestination,accessLogFormat=props.accessLogFormat;if(!accessLogDestination&&!accessLogFormat)accessLogSetting=void 0;else{if(accessLogFormat!==void 0&&!core_1.Token.isUnresolved(accessLogFormat.toString())&&!/.*\$context.requestId.*/.test(accessLogFormat.toString()))throw new Error("Access log must include at least `AccessLogFormat.contextRequestId()`");if(accessLogFormat!==void 0&&accessLogDestination===void 0)throw new Error("Access log format is specified without a destination");accessLogSetting={destinationArn:accessLogDestination==null?void 0:accessLogDestination.bind(this).destinationArn,format:(accessLogFormat==null?void 0:accessLogFormat.toString())?accessLogFormat==null?void 0:accessLogFormat.toString():access_log_1.AccessLogFormat.clf().toString()}}if(props.cacheClusterSize!==void 0){if(this.enableCacheCluster===void 0)this.enableCacheCluster=!0;else if(this.enableCacheCluster===!1)throw new Error(`Cannot set "cacheClusterSize" to ${props.cacheClusterSize} and "cacheClusterEnabled" to "false"`)}const cacheClusterSize=this.enableCacheCluster?props.cacheClusterSize||"0.5":void 0,resource=new apigateway_generated_1.CfnStage(this,"Resource",{stageName:props.stageName||"prod",accessLogSetting,cacheClusterEnabled:this.enableCacheCluster,cacheClusterSize,clientCertificateId:props.clientCertificateId,deploymentId:props.deployment.deploymentId,restApiId:props.deployment.api.restApiId,description:props.description,documentationVersion:props.documentationVersion,variables:props.variables,tracingEnabled:props.tracingEnabled,methodSettings});this.stageName=resource.ref,this.restApi=props.deployment.api,restapi_1.RestApiBase._isRestApiBase(this.restApi)&&this.restApi._attachStage(this)}urlForPath(path="/"){if(!path.startsWith("/"))throw new Error(`Path must begin with "/": ${path}`);return`https://${this.restApi.restApiId}.execute-api.${core_1.Stack.of(this).region}.${core_1.Stack.of(this).urlSuffix}/${this.stageName}${path}`}get stageArn(){return core_1.Stack.of(this).formatArn({arnFormat:core_1.ArnFormat.SLASH_RESOURCE_SLASH_RESOURCE_NAME,service:"apigateway",account:"",resource:"restapis",resourceName:`${this.restApi.restApiId}/stages/${this.stageName}`})}renderMethodSettings(props){const settings=new Array,self=this,commonMethodOptions={metricsEnabled:props.metricsEnabled,loggingLevel:props.loggingLevel,dataTraceEnabled:props.dataTraceEnabled,throttlingBurstLimit:props.throttlingBurstLimit,throttlingRateLimit:props.throttlingRateLimit,cachingEnabled:props.cachingEnabled,cacheTtl:props.cacheTtl,cacheDataEncrypted:props.cacheDataEncrypted};if(Object.keys(commonMethodOptions).map(v=>commonMethodOptions[v]).filter(x=>x).length>0&&settings.push(renderEntry("/*/*",commonMethodOptions)),props.methodOptions)for(const path of Object.keys(props.methodOptions))settings.push(renderEntry(path,props.methodOptions[path]));return settings.length===0?void 0:settings;function renderEntry(path,options){var _b;if(options.cachingEnabled){if(self.enableCacheCluster===void 0)self.enableCacheCluster=!0;else if(self.enableCacheCluster===!1)throw new Error(`Cannot enable caching for method ${path} since cache cluster is disabled on stage`)}const{httpMethod,resourcePath}=util_1.parseMethodOptionsPath(path);return{httpMethod,resourcePath,cacheDataEncrypted:options.cacheDataEncrypted,cacheTtlInSeconds:options.cacheTtl&&options.cacheTtl.toSeconds(),cachingEnabled:options.cachingEnabled,dataTraceEnabled:(_b=options.dataTraceEnabled)!==null&&_b!==void 0?_b:!1,loggingLevel:options.loggingLevel,metricsEnabled:options.metricsEnabled,throttlingBurstLimit:options.throttlingBurstLimit,throttlingRateLimit:options.throttlingRateLimit}}}}exports.Stage=Stage,_a=JSII_RTTI_SYMBOL_1,Stage[_a]={fqn:"aws-cdk-lib.aws_apigateway.Stage",version:"2.13.0"};
//# sourceMappingURL=stage.js.map
