"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.prepareHandlerContext = exports.prepareCommandInvocation = void 0;
const RequestProcessor_1 = require("@atomist/automation-client/lib/internal/transport/RequestProcessor");
const MessageClient_1 = require("@atomist/automation-client/lib/spi/message/MessageClient");
/**
 * Prepare the CommandInvocation instance to be sent for execution
 *
 * This pieces apart provided values form the parameters into the command's parameter, mapped parameter
 * and secret structures.
 */
function prepareCommandInvocation(md, parameters = {}) {
    // Flatten the provided parameters before creating the CommandInvocation
    const params = MessageClient_1.mergeParameters(parameters, {});
    const ci = {
        name: md.name,
        args: md.parameters.filter(p => params[p.name] !== undefined).map(p => ({
            name: p.name,
            value: params[p.name],
        })),
        mappedParameters: md.mapped_parameters.filter(p => params[p.name] !== undefined).map(p => ({
            name: p.name,
            value: params[p.name],
        })),
        secrets: md.secrets.map(p => ({
            uri: p.uri,
            value: params[p.name] || "null",
        })),
    };
    return ci;
}
exports.prepareCommandInvocation = prepareCommandInvocation;
/**
 * Decorate the HandlerContext to support response messages for this event handler invocation.
 *
 * Task execution happens is rooted in an event handler executing; this would prevent response
 * messages to work out of the box which is why this function adds the respond function to the
 * MessageClient if possible.
 */
function prepareHandlerContext(ctx, trigger) {
    if (RequestProcessor_1.isCommandIncoming(trigger)) {
        const source = trigger.source;
        if (!!source) {
            ctx.messageClient.respond = (msg, options) => {
                return ctx.messageClient.send(msg, new MessageClient_1.SourceDestination(source, source.user_agent), options);
            };
        }
    }
    else if (RequestProcessor_1.isEventIncoming(trigger)) {
        ctx.messageClient.respond = async () => {
            return;
        };
    }
    if (!!trigger) {
        ctx.trigger = trigger;
    }
    return ctx;
}
exports.prepareHandlerContext = prepareHandlerContext;
//# sourceMappingURL=helpers.js.map