"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.inferSpringStructureAndDoTransform = exports.inferSpringStructureAndRenameTransform = exports.inferSpringStructureAndDo = exports.inferSpringStructureAndRename = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const path = require("path");
const javaProjectUtils_1 = require("../../java/javaProjectUtils");
const SpringBootProjectStructure_1 = require("./SpringBootProjectStructure");
const SpringProjectCreationParameters_1 = require("./SpringProjectCreationParameters");
/**
 * Infer the Spring Boot structure and rename the class.
 * @param {string} serviceClassName
 * @param {Project} p
 * @return {Promise<Project>}
 */
async function inferSpringStructureAndRename(serviceClassName, p) {
    return inferSpringStructureAndDo(p, async (project, structure) => {
        return javaProjectUtils_1.renameClass(project, structure.applicationClassStem, serviceClassName);
    });
}
exports.inferSpringStructureAndRename = inferSpringStructureAndRename;
/**
 * Infer the Spring Boot structure and perform an action
 * @param {string} serviceClassName
 * @param {Project} p
 * @return {Promise<Project>}
 */
async function inferSpringStructureAndDo(p, action, params) {
    const structures = await SpringBootProjectStructure_1.SpringBootProjectStructure.inferFromJavaOrKotlin(p);
    if (!structures || structures.length === 0) {
        logger_1.logger.warn("Spring Boot project structure not found");
        return p;
    }
    else if (structures.length > 1) {
        const msg = `Found more than one Spring Boot application annotation in files: ` +
            structures.map(s => path.join(s.appClassFile.path, s.appClassFile.name)).join(",");
        logger_1.logger.warn(msg);
        throw new Error(msg);
    }
    else {
        return action(p, structures[0], params);
    }
}
exports.inferSpringStructureAndDo = inferSpringStructureAndDo;
exports.inferSpringStructureAndRenameTransform = (p, c, params) => inferSpringStructureAndRename(SpringProjectCreationParameters_1.computeServiceClassName(params, p), p);
function inferSpringStructureAndDoTransform(action) {
    return (p, c, params) => inferSpringStructureAndDo(p, action, params);
}
exports.inferSpringStructureAndDoTransform = inferSpringStructureAndDoTransform;
//# sourceMappingURL=springBootUtils.js.map