"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.initGenerator = initGenerator;
exports.updateNxJsonConfiguration = updateNxJsonConfiguration;
const devkit_1 = require("@nx/devkit");
const versions_1 = require("../../utils/versions");
const has_gradle_plugin_1 = require("../../utils/has-gradle-plugin");
const gradle_project_graph_plugin_utils_1 = require("./gradle-project-graph-plugin-utils");
async function initGenerator(tree, options) {
    const tasks = [];
    if (!options.skipPackageJson && tree.exists('package.json')) {
        tasks.push((0, devkit_1.addDependenciesToPackageJson)(tree, {}, {
            '@nx/gradle': versions_1.nxVersion,
        }, undefined, options.keepExistingVersions));
    }
    await (0, gradle_project_graph_plugin_utils_1.addNxProjectGraphPlugin)(tree);
    addPlugin(tree);
    updateNxJsonConfiguration(tree);
    if (!options.skipFormat) {
        await (0, devkit_1.formatFiles)(tree);
    }
    return (0, devkit_1.runTasksInSerial)(...tasks);
}
function addPlugin(tree) {
    const nxJson = (0, devkit_1.readNxJson)(tree);
    if (!(0, has_gradle_plugin_1.hasGradlePlugin)(tree)) {
        nxJson.plugins ??= [];
        nxJson.plugins.push({
            plugin: '@nx/gradle',
            options: {
                testTargetName: 'test',
                classesTargetName: 'classes',
                buildTargetName: 'build',
            },
        });
        (0, devkit_1.updateNxJson)(tree, nxJson);
    }
}
function updateNxJsonConfiguration(tree) {
    const nxJson = (0, devkit_1.readNxJson)(tree);
    if (!nxJson.namedInputs) {
        nxJson.namedInputs = {};
    }
    const defaultFilesSet = nxJson.namedInputs.default ?? [];
    nxJson.namedInputs.default = Array.from(new Set([...defaultFilesSet, '{projectRoot}/**/*']));
    const productionFileSet = nxJson.namedInputs.production ?? [];
    nxJson.namedInputs.production = Array.from(new Set([...productionFileSet, 'default', '!{projectRoot}/src/test/**/*']));
    (0, devkit_1.updateNxJson)(tree, nxJson);
}
exports.default = initGenerator;
