"use strict";
/*
 * Copyright © 2018 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.DefaultGoalNameGenerator = exports.SourceLocationGoalNameGenerator = void 0;
const path = require("path");
const trace = require("stack-trace");
/**
 * Generates goal names based on source code location.
 * This is stable enough to survive SDM restarts and also supports the cluster and goal forking mode.
 *
 * This implementation has to used directly inside the body of the goal you want to name.
 * Otherwise the source code location might not get captured correctly.
 */
class SourceLocationGoalNameGenerator {
    generateName(prefix) {
        let stack;
        try {
            // Just throw an error so that we can capture the stack
            throw new Error();
        }
        catch (err) {
            stack = trace.parse(err);
        }
        // 0 = this, 1 = the caller of generateName, 2 = the creator of the goal
        const goal = stack[1];
        const goalName = path.basename(goal.getFileName()).split(".")[0];
        const creator = stack[2];
        const creatorFileName = path.basename(creator.getFileName());
        const creatorLineNumber = creator.getLineNumber();
        const name = `${creatorFileName}:${creatorLineNumber}`;
        return `${prefix ? prefix : goalName}#${name}`;
    }
}
exports.SourceLocationGoalNameGenerator = SourceLocationGoalNameGenerator;
// Default GoalNameGenerator
exports.DefaultGoalNameGenerator = new SourceLocationGoalNameGenerator();
//# sourceMappingURL=GoalNameGenerator.js.map