"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringifyCustomData = exports.SlackIntegratedManualApproval = void 0;
const aws_codepipeline_actions_1 = require("aws-cdk-lib/aws-codepipeline-actions");
class SlackIntegratedManualApproval extends aws_codepipeline_actions_1.ManualApprovalAction {
    /**
     * Constructs an approval that contains links to the target hosts, the repos that were sourced, and a summary of the changes that are being pushed
     * @param props
     */
    constructor(props) {
        var _a, _b;
        const customDataStringified = (0, exports.stringifyCustomData)(props);
        const defaults = {
            actionName: (_a = props.actionName) !== null && _a !== void 0 ? _a : 'Approval',
            additionalInformation: customDataStringified,
            runOrder: (_b = props.runOrder) !== null && _b !== void 0 ? _b : 99, // This should always be the last action in the stage
        };
        super({ ...defaults, ...props });
    }
}
exports.SlackIntegratedManualApproval = SlackIntegratedManualApproval;
// This should not be exported, but I have to export it for unit tests to work
const stringifyCustomData = (props) => {
    /**
     *
     * This should not be called directly.  I just exported it in order to run unit tests.
     */
    let customDataStringified = '';
    const enhancedGithubSources = [];
    if (props.customData.githubSources) {
        for (const gitSource of props.customData.githubSources) {
            const githubSourceData = {
                authorDate: gitSource.sourceAction.variables.authorDate,
                branchName: gitSource.sourceAction.variables.branchName,
                commitId: gitSource.sourceAction.variables.commitId,
                owner: gitSource.owner,
                repositoryName: (gitSource.sourceAction.variables.repositoryName || gitSource.sourceAction.variables.fullRepositoryName || ''),
            };
            enhancedGithubSources.push(githubSourceData);
        }
        const customData = {
            additionalMessage: props.customData.additionalMessage,
            approveButtonText: props.customData.approveButtonText,
            attemptTarget: props.customData.attemptTarget,
            rejectButtonText: props.customData.rejectButtonText,
            slackChannelId: props.customData.slackChannelId,
            slackChannelName: props.customData.slackChannelName,
            successfulTarget: props.customData.successfulTarget,
            enhancedGithubSources,
        };
        customDataStringified = JSON.stringify(customData);
    }
    return customDataStringified;
};
exports.stringifyCustomData = stringifyCustomData;
