"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.hasPreconditions = exports.isGoalDefinition = exports.GoalWithPrecondition = exports.Goal = void 0;
const GitHubContext_1 = require("./GitHubContext");
const environment_1 = require("./support/environment");
/**
 * Represents a delivery action, such as Build or Deploy.
 */
class Goal {
    constructor(definition) {
        this.definition = validateGoalDefinition(definition);
        // Default environment if hasn't been provided
        if (!this.definition.environment) {
            this.definition.environment = environment_1.IndependentOfEnvironment;
        }
        this.context = GitHubContext_1.BaseContext + this.definition.environment + this.definition.uniqueName;
    }
    get environment() {
        return this.definition.environment;
    }
    get successDescription() {
        return this.definition.completedDescription || `Complete: ${this.name}`;
    }
    get inProcessDescription() {
        return this.definition.workingDescription || `Working: ${this.name}`;
    }
    get failureDescription() {
        return this.definition.failedDescription || `Failed: ${this.name}`;
    }
    get plannedDescription() {
        return this.definition.plannedDescription || `Planned: ${this.name}`;
    }
    get requestedDescription() {
        return this.definition.requestedDescription || `Ready: ${this.name}`;
    }
    get waitingForApprovalDescription() {
        return this.definition.waitingForApprovalDescription || `Approval required: ${this.name}`;
    }
    get waitingForPreApprovalDescription() {
        return this.definition.waitingForPreApprovalDescription || `Start required: ${this.name}`;
    }
    get canceledDescription() {
        return this.definition.canceledDescription || `Canceled: ${this.name}`;
    }
    get stoppedDescription() {
        return this.definition.stoppedDescription || `Stopped: ${this.name}`;
    }
    get skippedDescription() {
        return this.definition.skippedDescription || `Skipped: ${this.name}`;
    }
    get name() {
        return this.definition.displayName || this.definition.uniqueName;
    }
    get uniqueName() {
        return this.definition.uniqueName;
    }
}
exports.Goal = Goal;
class GoalWithPrecondition extends Goal {
    constructor(definition, ...dependsOn) {
        super(definition);
        this.dependsOn = dependsOn;
    }
}
exports.GoalWithPrecondition = GoalWithPrecondition;
function isGoalDefinition(f) {
    return !!f.uniqueName;
}
exports.isGoalDefinition = isGoalDefinition;
function hasPreconditions(goal) {
    return !!goal.dependsOn;
}
exports.hasPreconditions = hasPreconditions;
const UniqueNameRegExp = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/i;
function validateGoalDefinition(gd) {
    // First replace all spaces and _ with -
    const uniqueName = gd.uniqueName.replace(/ /g, "-").replace(/_/g, "-");
    // Now validate the part in front of # against regexp
    if (!UniqueNameRegExp.test(uniqueName.split("#")[0])) {
        throw new Error(`Goal uniqueName '${gd.uniqueName}' must consist of lower case alphanumeric characters or '-', and must start and ` +
            `end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '${UniqueNameRegExp}')`);
    }
    gd.uniqueName = uniqueName;
    return gd;
}
//# sourceMappingURL=Goal.js.map