"use strict";var _a;Object.defineProperty(exports,"__esModule",{value:!0}),exports.PolicyDocument=void 0;const jsiiDeprecationWarnings=require("../../.warnings.jsii.js"),JSII_RTTI_SYMBOL_1=Symbol.for("jsii.rtti"),cdk=require("../../core"),policy_statement_1=require("./policy-statement");class PolicyDocument{constructor(props={}){this.statements=new Array,jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_PolicyDocumentProps(props),this.creationStack=cdk.captureStackTrace(),this.autoAssignSids=!!props.assignSids,this.addStatements(...props.statements||[])}static fromJson(obj){var _b;const newPolicyDocument=new PolicyDocument,statement=(_b=obj.Statement)!==null&&_b!==void 0?_b:[];if(statement&&!Array.isArray(statement))throw new Error("Statement must be an array");return newPolicyDocument.addStatements(...obj.Statement.map(s=>policy_statement_1.PolicyStatement.fromJson(s))),newPolicyDocument}resolve(context){return jsiiDeprecationWarnings.aws_cdk_lib_IResolveContext(context),context.registerPostProcessor(new RemoveDuplicateStatements(this.autoAssignSids)),this.render()}get isEmpty(){return this.statements.length===0}get statementCount(){return this.statements.length}addStatements(...statement){jsiiDeprecationWarnings.aws_cdk_lib_aws_iam_PolicyStatement(statement),this.statements.push(...statement)}toString(){return cdk.Token.asString(this,{displayHint:"PolicyDocument"})}toJSON(){return this.render()}validateForAnyPolicy(){const errors=new Array;for(const statement of this.statements)errors.push(...statement.validateForAnyPolicy());return errors}validateForResourcePolicy(){const errors=new Array;for(const statement of this.statements)errors.push(...statement.validateForResourcePolicy());return errors}validateForIdentityPolicy(){const errors=new Array;for(const statement of this.statements)errors.push(...statement.validateForIdentityPolicy());return errors}render(){return this.isEmpty?void 0:{Statement:this.statements.map(s=>s.toStatementJson()),Version:"2012-10-17"}}}exports.PolicyDocument=PolicyDocument,_a=JSII_RTTI_SYMBOL_1,PolicyDocument[_a]={fqn:"aws-cdk-lib.aws_iam.PolicyDocument",version:"2.13.0"};class RemoveDuplicateStatements{constructor(autoAssignSids){this.autoAssignSids=autoAssignSids}postProcess(input,_context){if(!input||!input.Statement)return input;const jsonStatements=new Set,uniqueStatements=[];for(const statement of input.Statement){const jsonStatement=JSON.stringify(statement);jsonStatements.has(jsonStatement)||(uniqueStatements.push(statement),jsonStatements.add(jsonStatement))}const statements=uniqueStatements.map((s,i)=>(this.autoAssignSids&&!s.Sid&&(s.Sid=i.toString()),s));return{...input,Statement:statements}}}
//# sourceMappingURL=policy-document.js.map
