"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events = require("@aws-cdk/aws-events");
const cdk = require("@aws-cdk/cdk");
const codecommit_generated_1 = require("./codecommit.generated");
/**
 * Represents a reference to a CodeCommit Repository.
 *
 * If you want to create a new Repository managed alongside your CDK code,
 * use the {@link Repository} class.
 *
 * If you want to reference an already existing Repository,
 * use the {@link RepositoryRef.import} method.
 */
class RepositoryRef extends cdk.Construct {
    /**
     * Import a Repository defined either outside the CDK, or in a different Stack
     * (exported with the {@link export} method).
     *
     * @param parent the parent Construct for the Repository
     * @param name the name of the Repository Construct
     * @param props the properties used to identify the existing Repository
     * @returns a reference to the existing Repository
     */
    static import(parent, name, props) {
        return new ImportedRepositoryRef(parent, name, props);
    }
    /**
     * Exports this Repository. Allows the same Repository to be used in 2 different Stacks.
     *
     * @see import
     */
    export() {
        return {
            repositoryName: new cdk.Output(this, 'RepositoryName', { value: this.repositoryName }).makeImportValue(),
        };
    }
    /**
     * Defines a CloudWatch event rule which triggers for repository events. Use
     * `rule.addEventPattern(pattern)` to specify a filter.
     */
    onEvent(name, target, options) {
        const rule = new events.EventRule(this, name, options);
        rule.addEventPattern({
            source: ['aws.codecommit'],
            resources: [this.repositoryArn]
        });
        rule.addTarget(target);
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a "CodeCommit
     * Repository State Change" event occurs.
     */
    onStateChange(name, target, options) {
        const rule = this.onEvent(name, target, options);
        rule.addEventPattern({
            detailType: ['CodeCommit Repository State Change'],
        });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a reference is
     * created (i.e. a new brach/tag is created) to the repository.
     */
    onReferenceCreated(name, target, options) {
        const rule = this.onStateChange(name, target, options);
        rule.addEventPattern({ detail: { event: ['referenceCreated'] } });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a reference is
     * updated (i.e. a commit is pushed to an existig branch) from the repository.
     */
    onReferenceUpdated(name, target, options) {
        const rule = this.onStateChange(name, target, options);
        rule.addEventPattern({ detail: { event: ['referenceUpdated'] } });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a reference is
     * delete (i.e. a branch/tag is deleted) from the repository.
     */
    onReferenceDeleted(name, target, options) {
        const rule = this.onStateChange(name, target, options);
        rule.addEventPattern({ detail: { event: ['referenceDeleted'] } });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a pull request state is changed.
     */
    onPullRequestStateChange(name, target, options) {
        const rule = this.onEvent(name, target, options);
        rule.addEventPattern({ detailType: ['CodeCommit Pull Request State Change'] });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a comment is made on a pull request.
     */
    onCommentOnPullRequest(name, target, options) {
        const rule = this.onEvent(name, target, options);
        rule.addEventPattern({ detailType: ['CodeCommit Comment on Pull Request'] });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a comment is made on a commit.
     */
    onCommentOnCommit(name, target, options) {
        const rule = this.onEvent(name, target, options);
        rule.addEventPattern({ detailType: ['CodeCommit Comment on Commit'] });
        return rule;
    }
    /**
     * Defines a CloudWatch event rule which triggers when a commit is pushed to a branch.
     * @param target The target of the event
     * @param branch The branch to monitor. Defaults to all branches.
     */
    onCommit(name, target, branch) {
        const rule = this.onReferenceUpdated(name, target);
        if (branch) {
            rule.addEventPattern({ detail: { referenceName: [branch] } });
        }
        return rule;
    }
}
exports.RepositoryRef = RepositoryRef;
class ImportedRepositoryRef extends RepositoryRef {
    constructor(parent, name, props) {
        super(parent, name);
        this.repositoryArn = cdk.Arn.fromComponents({
            service: 'codecommit',
            resource: props.repositoryName,
        });
        this.repositoryName = props.repositoryName;
    }
}
/**
 * Provides a CodeCommit Repository
 */
class Repository extends RepositoryRef {
    constructor(parent, name, props) {
        super(parent, name);
        this.triggers = new Array();
        this.repository = new codecommit_generated_1.cloudformation.RepositoryResource(this, 'Resource', {
            repositoryName: props.repositoryName,
            repositoryDescription: props.description,
            triggers: this.triggers
        });
    }
    get repositoryArn() {
        return this.repository.repositoryArn;
    }
    get repositoryCloneUrlHttp() {
        return this.repository.repositoryCloneUrlHttp;
    }
    get repositoryCloneUrlSsh() {
        return this.repository.repositoryCloneUrlSsh;
    }
    get repositoryName() {
        return this.repository.repositoryName;
    }
    /**
     * Create a trigger to notify another service to run actions on repository events.
     * @param arn     Arn of the resource that repository events will notify
     * @param options Trigger options to run actions
     */
    notify(arn, options) {
        let evt = options && options.events;
        if (evt && evt.length > 1 && evt.indexOf(RepositoryEventTrigger.All) > -1) {
            evt = [RepositoryEventTrigger.All];
        }
        const customData = options && options.customData;
        const branches = options && options.branches;
        let name = options && options.name;
        if (!name) {
            name = this.path + '/' + arn;
        }
        if (this.triggers.find(prop => prop.name === name)) {
            throw new Error(`Unable to set repository trigger named ${name} because trigger names must be unique`);
        }
        this.triggers.push({
            destinationArn: arn,
            name,
            customData,
            branches,
            events: evt || [RepositoryEventTrigger.All],
        });
        return this;
    }
}
exports.Repository = Repository;
/**
 * Repository events that will cause the trigger to run actions in another service.
 */
var RepositoryEventTrigger;
(function (RepositoryEventTrigger) {
    RepositoryEventTrigger["All"] = "all";
    RepositoryEventTrigger["UpdateRef"] = "updateReference";
    RepositoryEventTrigger["CreateRef"] = "createReference";
    RepositoryEventTrigger["DeleteRef"] = "deleteReference";
})(RepositoryEventTrigger = exports.RepositoryEventTrigger || (exports.RepositoryEventTrigger = {}));
//# sourceMappingURL=data:application/json;base64, 