"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNodes = void 0;
const devkit_1 = require("@nx/devkit");
const internal_1 = require("@nx/devkit/internal");
const path_1 = require("path");
const types_1 = require("./types");
const maven_analyzer_1 = require("./maven-analyzer");
const maven_data_cache_1 = require("./maven-data-cache");
const devkit_internals_1 = require("nx/src/devkit-internals");
/**
 * Maven plugin that analyzes Maven projects and returns configurations
 */
exports.createNodes = [
    '**/pom.xml',
    async (configFiles, options, context) => {
        const opts = {
            ...types_1.DEFAULT_OPTIONS,
            ...options,
        };
        // Check for verbose logging from multiple sources
        const isVerbose = opts.verbose || process.env.NX_VERBOSE_LOGGING === 'true';
        if (isVerbose) {
            console.error(`Maven plugin running in verbose mode (NX_VERBOSE_LOGGING=${process.env.NX_VERBOSE_LOGGING})`);
        }
        // Only process if we have the root pom.xml in the workspace root
        const rootPomExists = configFiles.some((file) => file === 'pom.xml');
        if (!rootPomExists) {
            return [];
        }
        // Get cache path based on options
        const optionsHash = (0, devkit_internals_1.hashObject)(opts);
        const cachePath = (0, maven_data_cache_1.getCachePath)(context.workspaceRoot, optionsHash);
        const mavenCache = (0, maven_data_cache_1.readMavenCache)(cachePath);
        // Calculate hashes for all pom.xml directories
        const projectRoots = configFiles.map((file) => (0, path_1.dirname)(file));
        const hashes = await (0, internal_1.calculateHashesForCreateNodes)(projectRoots, opts, context);
        // Combine all hashes into a single hash for the cache key
        const hash = (0, devkit_1.hashArray)(hashes);
        try {
            // Try to get cached data first (skip cache if in verbose mode)
            let mavenData = isVerbose ? null : mavenCache.get(hash);
            // If no cached data or cache is stale, run fresh Maven analysis
            if (!mavenData) {
                try {
                    mavenData = await (0, maven_analyzer_1.runMavenAnalysis)(context.workspaceRoot, {
                        ...opts,
                        verbose: isVerbose,
                    });
                }
                catch (e) {
                    if (e instanceof Error &&
                        e.message === 'Maven analysis was cancelled') {
                        // Cancelled by a newer createNodes call — silently return empty
                        return [];
                    }
                    throw e;
                }
                // Cache the results with the hash
                mavenCache.set(hash, mavenData);
            }
            // Store in module-level variable for createDependencies to use
            (0, maven_data_cache_1.setCurrentMavenData)(mavenData);
            // Return createNodesResults (atomization now handled in Kotlin)
            return mavenData.createNodesResults.map(([configFile, createNodesResult]) => {
                return [
                    (0, path_1.relative)(context.workspaceRoot, configFile),
                    createNodesResult,
                ];
            });
        }
        finally {
            (0, maven_data_cache_1.writeMavenCache)(cachePath, mavenCache);
        }
    },
];
