"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const events = require("@aws-cdk/aws-events");
const cdk = require("@aws-cdk/cdk");
const artifact_1 = require("./artifact");
const validation = require("./validation");
var ActionCategory;
(function (ActionCategory) {
    ActionCategory["Source"] = "Source";
    ActionCategory["Build"] = "Build";
    ActionCategory["Test"] = "Test";
    ActionCategory["Approval"] = "Approval";
    ActionCategory["Deploy"] = "Deploy";
    ActionCategory["Invoke"] = "Invoke";
})(ActionCategory = exports.ActionCategory || (exports.ActionCategory = {}));
function defaultBounds() {
    return {
        minInputs: 0,
        maxInputs: 5,
        minOutputs: 0,
        maxOutputs: 5
    };
}
exports.defaultBounds = defaultBounds;
/**
 * Low-level class for generic CodePipeline Actions.
 * It is recommended that concrete types are used instead, such as {@link codecommit.PipelineSource} or
 * {@link codebuild.PipelineBuildAction}.
 */
class Action extends cdk.Construct {
    constructor(parent, id, props) {
        super(parent, id);
        this._inputArtifacts = new Array();
        this._outputArtifacts = new Array();
        validation.validateName('Action', id);
        this.owner = props.owner || 'AWS';
        this.version = props.version || '1';
        this.category = props.category;
        this.provider = props.provider;
        this.configuration = props.configuration;
        this.artifactBounds = props.artifactBounds;
        this.runOrder = 1;
        this.stage = props.stage;
        this.stage._addAction(this);
    }
    validate() {
        return validation.validateArtifactBounds('input', this._inputArtifacts, this.artifactBounds.minInputs, this.artifactBounds.maxInputs, this.category, this.provider)
            .concat(validation.validateArtifactBounds('output', this._outputArtifacts, this.artifactBounds.minOutputs, this.artifactBounds.maxOutputs, this.category, this.provider));
    }
    onStateChange(name, target, options) {
        const rule = new events.EventRule(this.parent, name, options);
        rule.addTarget(target);
        rule.addEventPattern({
            detailType: ['CodePipeline Stage Execution State Change'],
            source: ['aws.codepipeline'],
            resources: [this.stage.pipelineArn],
            detail: {
                stage: [this.stage.name],
                action: [this.id],
            },
        });
        return rule;
    }
    get inputArtifacts() {
        return this._inputArtifacts.slice();
    }
    get outputArtifacts() {
        return this._outputArtifacts.slice();
    }
    addChild(child, name) {
        super.addChild(child, name);
        if (child instanceof artifact_1.Artifact) {
            this._outputArtifacts.push(child);
        }
    }
    addOutputArtifact(name) {
        const artifact = new artifact_1.Artifact(this, name);
        return artifact;
    }
    addInputArtifact(artifact) {
        this._inputArtifacts.push(artifact);
        return this;
    }
}
exports.Action = Action;
// export class TestAction extends Action {
//     constructor(parent: Stage, name: string, provider: string, artifactBounds: ActionArtifactBounds, configuration?: any) {
//         super(parent, name, {
//           category: ActionCategory.Test,
//           provider,
//           artifactBounds,
//           configuration
//         });
//     }
// }
// export class CodeBuildTest extends TestAction {
//     constructor(parent: Stage, name: string, project: codebuild.ProjectArnAttribute) {
//         super(parent, name, 'CodeBuild', { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 1 }, {
//             ProjectName: project
//         });
//     }
// }
// export class CodeDeploy extends DeployAction {
//     constructor(parent: Stage, name: string, applicationName: string, deploymentGroupName: string) {
//         super(parent, name, 'CodeDeploy', { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 0 }, {
//             ApplicationName: applicationName,
//             DeploymentGroupName: deploymentGroupName
//         });
//     }
// }
// export class ElasticBeanstalkDeploy extends DeployAction {
//     constructor(parent: Stage, name: string, applicationName: string, environmentName: string) {
//         super(parent, name, 'ElasticBeanstalk', { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 0 }, {
//             ApplicationName: applicationName,
//             EnvironmentName: environmentName
//         });
//     }
// }
// export class OpsWorksDeploy extends DeployAction {
//     constructor(parent: Stage, name: string, app: string, stack: string, layer?: string) {
//         super(parent, name, 'OpsWorks', { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 0 }, {
//             Stack: stack,
//             App: app,
//             Layer: layer,
//         });
//     }
// }
// export class ECSDeploy extends DeployAction {
//     constructor(parent: Stage, name: string, clusterName: string, serviceName: string, fileName?: string) {
//         super(parent, name, 'ECS', { minInputs: 1, maxInputs: 1, minOutputs: 0, maxOutputs: 0 }, {
//             ClusterName: clusterName,
//             ServiceName: serviceName,
//             FileName: fileName,
//         });
//     }
// }
/*
  TODO: A Jenkins build needs a corresponding custom action for each "Jenkins provider".
        This should be created automatically.

  Example custom action created to execute Jenkins:
  {
    "id": {
        "category": "Test",
        "provider": "<provider name>",
        "owner": "Custom",
        "version": "1"
    },
    "outputArtifactDetails": {
        "minimumCount": 0,
        "maximumCount": 5
    },
    "settings": {
        "executionUrlTemplate": "https://www.google.com/job/{Config:ProjectName}/{ExternalExecutionId}",
        "entityUrlTemplate": "https://www.google.com/job/{Config:ProjectName}"
    },
    "actionConfigurationProperties": [
        {
            "queryable": true,
            "key": true,
            "name": "ProjectName",
            "required": true,
            "secret": false
        }
    ],
    "inputArtifactDetails": {
        "minimumCount": 0,
        "maximumCount": 5
    }
  }
*/
// export class JenkinsBuild extends BuildAction {
//     constructor(parent: Stage, name: string, jenkinsProvider: string, project: string) {
//         super(parent, name, jenkinsProvider, DefaultBounds(), {
//             ProjectName: project
//         });
//     }
// }
// export class JenkinsTest extends TestAction {
//     constructor(parent: Stage, name: string, jenkinsProvider: string, project: string) {
//         super(parent, name, jenkinsProvider, DefaultBounds(), {
//             ProjectName: project
//         });
//     }
// }
//# sourceMappingURL=data:application/json;base64, 