"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.Tag = void 0;
const GoalNameGenerator_1 = require("../../../api/goal/GoalNameGenerator");
const GoalWithFulfillment_1 = require("../../../api/goal/GoalWithFulfillment");
const executeTag_1 = require("../../delivery/build/executeTag");
/**
 * Goal that performs project tagging using Git.  If no fulfillment is
 * added to the goal, one is added during registration that tags using
 * the goal set pre-release version as created by the [[Version]]
 * goal.
 */
class Tag extends GoalWithFulfillment_1.FulfillableGoal {
    constructor(goalDetailsOrUniqueName = GoalNameGenerator_1.DefaultGoalNameGenerator.generateName("tag"), ...dependsOn) {
        super(Object.assign(Object.assign({ workingDescription: "Tagging", completedDescription: "Tagged", failedDescription: "Failed to create Tag" }, GoalWithFulfillment_1.getGoalDefinitionFrom(goalDetailsOrUniqueName, GoalNameGenerator_1.DefaultGoalNameGenerator.generateName("tag"))), { displayName: "tag" }), ...dependsOn);
    }
    /**
     * Called by the SDM on initialization.  This function calls
     * `super.register` and adds a startup listener to the SDM.
     *
     * The startup listener registers a default goal fulfillment that
     * calles [[executeTag]] with no arguments.
     */
    register(sdm) {
        super.register(sdm);
        sdm.addStartupListener(async () => {
            if (this.fulfillments.length === 0 && this.callbacks.length === 0) {
                this.with();
            }
        });
    }
    /**
     * Add fulfillment to this goal.
     */
    with(registration = {}) {
        const name = registration.name || GoalNameGenerator_1.DefaultGoalNameGenerator.generateName((registration.goalExecutor) ? "custom-tag" : "prerelease-tag");
        const goalExecutor = registration.goalExecutor || executeTag_1.executeTag();
        super.addFulfillment({ name, goalExecutor });
        return this;
    }
}
exports.Tag = Tag;
//# sourceMappingURL=Tag.js.map