"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.KubernetesFulfillmentGoalScheduler = exports.defaultKubernetesFulfillmentOptions = void 0;
const _ = require("lodash");
const minimalClone_1 = require("../../../api-helper/goal/minimalClone");
const sdmGoal_1 = require("../../../api-helper/goal/sdmGoal");
const storeGoals_1 = require("../../../api-helper/goal/storeGoals");
const SdmGoalMessage_1 = require("../../../api/goal/SdmGoalMessage");
const container_1 = require("../../../core/goal/container/container");
const types_1 = require("../../../typings/types");
function defaultKubernetesFulfillmentOptions() {
    return {
        registration: "@atomist/k8s-sdm-skill",
        name: require("../container").K8sContainerFulfillerName,
    };
}
exports.defaultKubernetesFulfillmentOptions = defaultKubernetesFulfillmentOptions;
/**
 * GoalScheduler implementation that redirects goals to a registered k8s-sdm for
 * fulfillment.
 *
 * This is useful in runtimes that don't support launching container goals such as
 * FaaS environments like Google Cloud Functions.
 */
class KubernetesFulfillmentGoalScheduler {
    constructor(options = defaultKubernetesFulfillmentOptions()) {
        this.options = options;
    }
    async schedule(gi) {
        const { goalEvent, goal, configuration } = gi;
        const containerGoal = goal;
        let registration = _.cloneDeep(containerGoal.registrations[0]);
        const fulfillment = _.get(registration, "fulfillment");
        if (!!fulfillment) {
            goalEvent.fulfillment.registration = fulfillment.registration;
            goalEvent.fulfillment.name = fulfillment.name;
        }
        else {
            goalEvent.fulfillment.registration =
                typeof this.options.registration === "string" ? this.options.registration : await this.options.registration(gi);
            goalEvent.fulfillment.name =
                typeof this.options.name === "string" ? this.options.name : await this.options.name(gi);
        }
        goalEvent.fulfillment.method = SdmGoalMessage_1.SdmGoalFulfillmentMethod.Sdm;
        if (registration.callback) {
            registration = await configuration.sdm.projectLoader.doWithProject(Object.assign(Object.assign({}, gi), { readOnly: true, cloneOptions: minimalClone_1.minimalClone(goalEvent.push, { detachHead: true }) }), async (p) => {
                return Object.assign(Object.assign({}, registration), (await registration.callback(_.cloneDeep(registration), p, containerGoal, goalEvent, gi)) || {});
            });
        }
        const data = sdmGoal_1.goalData(goalEvent);
        const newData = {};
        delete registration.callback;
        _.set(newData, container_1.ContainerRegistrationGoalDataKey, registration);
        goalEvent.data = JSON.stringify(_.merge(data, newData));
        return {
            state: types_1.SdmGoalState.requested,
            description: storeGoals_1.descriptionFromState(goal, types_1.SdmGoalState.requested),
            phase: "scheduled",
        };
    }
    async supports(gi) {
        const { goal } = gi;
        return goal instanceof container_1.Container;
    }
}
exports.KubernetesFulfillmentGoalScheduler = KubernetesFulfillmentGoalScheduler;
//# sourceMappingURL=KubernetesFulfillmentGoalScheduler.js.map