"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cdk_1 = require("@aws-cdk/cdk");
const iam_generated_1 = require("./iam.generated");
const policy_1 = require("./policy");
const util_1 = require("./util");
class Group extends cdk_1.Construct {
    constructor(parent, name, props = {}) {
        super(parent, name);
        this.attachedPolicies = new util_1.AttachedPolicies();
        this.managedPolicies = props.managedPolicyArns || [];
        const group = new iam_generated_1.cloudformation.GroupResource(this, 'Resource', {
            groupName: props.groupName,
            managedPolicyArns: util_1.undefinedIfEmpty(() => this.managedPolicies),
            path: props.path,
        });
        this.groupName = group.ref;
        this.groupArn = group.groupArn;
        this.principal = new cdk_1.ArnPrincipal(this.groupArn);
    }
    /**
     * Attaches a managed policy to this group.
     * @param arn The ARN of the managed policy to attach.
     */
    attachManagedPolicy(arn) {
        this.managedPolicies.push(arn);
    }
    /**
     * Attaches a policy to this group.
     * @param policy The policy to attach.
     */
    attachInlinePolicy(policy) {
        this.attachedPolicies.attach(policy);
        policy.attachToGroup(this);
    }
    /**
     * Adds a user to this group.
     */
    addUser(user) {
        user.addToGroup(this);
    }
    /**
     * Adds an IAM statement to the default policy.
     */
    addToPolicy(statement) {
        if (!this.defaultPolicy) {
            this.defaultPolicy = new policy_1.Policy(this, 'DefaultPolicy');
            this.defaultPolicy.attachToGroup(this);
        }
        this.defaultPolicy.addStatement(statement);
    }
}
exports.Group = Group;
class GroupName extends cdk_1.Token {
}
exports.GroupName = GroupName;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ3JvdXAuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJncm91cC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHNDQUFnRztBQUNoRyxtREFBMkQ7QUFDM0QscUNBQWlFO0FBRWpFLGlDQUE0RDtBQStCNUQsTUFBYSxLQUFNLFNBQVEsZUFBUztJQW9CaEMsWUFBWSxNQUFpQixFQUFFLElBQVksRUFBRSxRQUFvQixF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