"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.clearWorkspaceCache = clearWorkspaceCache;
exports.getWorkspaceDefinition = getWorkspaceDefinition;
exports.getProjectDefinition = getProjectDefinition;
exports.getProjectBuildTargetName = getProjectBuildTargetName;
exports.getProjectBuildTarget = getProjectBuildTarget;
exports.updateProjectDefinition = updateProjectDefinition;
exports.updateWorkspaceDefinition = updateWorkspaceDefinition;
const schematics_1 = require("@angular-devkit/schematics");
const prompt_1 = require("@angular/cli/src/utilities/prompt");
const utility_1 = require("@schematics/angular/utility");
const workspace_1 = require("@schematics/angular/utility/workspace");
let _workspace;
const buildTargets = {};
/**
 * Clears the cached workspace.
 */
function clearWorkspaceCache() {
    _workspace = undefined;
}
/**
 * Gets the cached workspace definition.
 * @param tree
 */
function getWorkspaceDefinition(tree) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!_workspace) {
            _workspace = yield (0, workspace_1.getWorkspace)(tree);
        }
        return _workspace;
    });
}
/**
 * Gets the project definition for the given project name.
 * @param tree
 * @param projectName
 */
function getProjectDefinition(tree, projectName) {
    return __awaiter(this, void 0, void 0, function* () {
        const workspace = yield getWorkspaceDefinition(tree);
        const project = workspace.projects.get(projectName);
        if (!project) {
            throw new schematics_1.SchematicsException(`Project ${projectName} not found`);
        }
        return project;
    });
}
/** Gets the build target name for the given project. */
function getProjectBuildTargetName(tree, projectName) {
    return __awaiter(this, void 0, void 0, function* () {
        const projectDefinition = yield getProjectDefinition(tree, projectName);
        if (!buildTargets[projectName]) {
            buildTargets[projectName] = 'build';
            const buildTarget = projectDefinition.targets.get(buildTargets[projectName]);
            if (!buildTarget) {
                buildTargets[projectName] = (yield (0, prompt_1.askQuestion)("Please select your project's build target", Array.from(projectDefinition.targets.keys()).map((name) => ({
                    name,
                    value: name
                })), 0, null));
            }
        }
        return buildTargets[projectName];
    });
}
/**
 * Gets the build target for the given project name.
 * @param tree
 * @param projectName
 */
function getProjectBuildTarget(tree, projectName) {
    return __awaiter(this, void 0, void 0, function* () {
        const projectDefinition = yield getProjectDefinition(tree, projectName);
        return projectDefinition.targets.get(yield getProjectBuildTargetName(tree, projectName));
    });
}
/**
 * Updates the project definition for the given project name.
 * @param tree
 * @param projectName
 * @param updatedProject
 */
function updateProjectDefinition(tree, projectName, updatedProject) {
    return __awaiter(this, void 0, void 0, function* () {
        const workspace = yield getWorkspaceDefinition(tree);
        workspace.projects.set(projectName, updatedProject);
        yield (0, utility_1.writeWorkspace)(tree, workspace);
    });
}
/**
 * Updates the workspace definition.
 * @param tree
 * @param updatedWorkspace
 */
function updateWorkspaceDefinition(tree, updatedWorkspace) {
    return __awaiter(this, void 0, void 0, function* () {
        yield (0, utility_1.writeWorkspace)(tree, _workspace);
        _workspace = updatedWorkspace;
    });
}
//# sourceMappingURL=workspace.js.map