"use strict";var _a,_b;Object.defineProperty(exports,"__esModule",{value:!0}),exports.CompositeDependable=exports.Grant=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),cdk=require("../../core"),constructs_1=require("constructs"),policy_statement_1=require("./policy-statement");class Grant{constructor(props){this.options=props.options,this.principalStatement=props.principalStatement,this.resourceStatement=props.resourceStatement,constructs_1.Dependable.implement(this,{get dependencyRoots(){return props.policyDependable?constructs_1.Dependable.of(props.policyDependable).dependencyRoots:[]}})}static addToPrincipalOrResource(options){var _c;jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_GrantWithResourceOptions(options);const result=Grant.addToPrincipal({...options,scope:options.resource}),resourceAndPrincipalAccountComparison=options.grantee.grantPrincipal.principalAccount?cdk.Token.compareStrings(options.resource.env.account,options.grantee.grantPrincipal.principalAccount):void 0,equalOrBothUnresolved=resourceAndPrincipalAccountComparison===cdk.TokenComparison.SAME||resourceAndPrincipalAccountComparison==cdk.TokenComparison.BOTH_UNRESOLVED,sameAccount=resourceAndPrincipalAccountComparison?equalOrBothUnresolved:!1;if(result.success&&sameAccount)return result;const statement=new policy_statement_1.PolicyStatement({actions:options.actions,resources:options.resourceSelfArns||options.resourceArns,principals:[options.grantee.grantPrincipal]}),resourceResult=options.resource.addToResourcePolicy(statement);return new Grant({resourceStatement:statement,options,policyDependable:resourceResult.statementAdded?(_c=resourceResult.policyDependable)!==null&&_c!==void 0?_c:options.resource:void 0})}static addToPrincipal(options){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_GrantOnPrincipalOptions(options);const statement=new policy_statement_1.PolicyStatement({actions:options.actions,resources:options.resourceArns}),addedToPrincipal=options.grantee.grantPrincipal.addToPrincipalPolicy(statement);if(!addedToPrincipal.statementAdded)return new Grant({principalStatement:void 0,options});if(!addedToPrincipal.policyDependable)throw new Error("Contract violation: when Principal returns statementAdded=true, it should return a dependable");return new Grant({principalStatement:statement,options,policyDependable:addedToPrincipal.policyDependable})}static addToPrincipalAndResource(options){var _c;jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_GrantOnPrincipalAndResourceOptions(options);const result=Grant.addToPrincipal({...options,scope:options.resource}),statement=new policy_statement_1.PolicyStatement({actions:options.actions,resources:options.resourceSelfArns||options.resourceArns,principals:[options.resourcePolicyPrincipal||options.grantee.grantPrincipal]}),resourceResult=options.resource.addToResourcePolicy(statement),resourceDependable=resourceResult.statementAdded?(_c=resourceResult.policyDependable)!==null&&_c!==void 0?_c:options.resource:void 0;return new Grant({principalStatement:statement,resourceStatement:result.resourceStatement,options,policyDependable:resourceDependable?new CompositeDependable(result,resourceDependable):result})}static drop(grantee,_intent){return jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_IGrantable(grantee),new Grant({options:{grantee,actions:[],resourceArns:[]}})}get success(){return this.principalStatement!==void 0||this.resourceStatement!==void 0}assertSuccess(){if(!this.success)throw new Error(`${describeGrant(this.options)} could not be added on either identity or resource policy.`)}applyBefore(...constructs){for(const construct of constructs)construct.node.addDependency(this)}}exports.Grant=Grant,_a=JSII_RTTI_SYMBOL_1,Grant[_a]={fqn:"aws-cdk-lib.aws_iam.Grant",version:"2.13.0"};function describeGrant(options){return`Permissions for '${options.grantee}' to call '${options.actions}' on '${options.resourceArns}'`}class CompositeDependable{constructor(...dependables){constructs_1.Dependable.implement(this,{get dependencyRoots(){return Array.prototype.concat.apply([],dependables.map(d=>constructs_1.Dependable.of(d).dependencyRoots))}})}}exports.CompositeDependable=CompositeDependable,_b=JSII_RTTI_SYMBOL_1,CompositeDependable[_b]={fqn:"aws-cdk-lib.aws_iam.CompositeDependable",version:"2.13.0"};
//# sourceMappingURL=grant.js.map
