"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathResolver = exports.DEFAULT_PATH_CONFIG = exports.CONFIG_FILES = void 0;
exports.getWorkingDirectory = getWorkingDirectory;
exports.requireConfigRoot = requireConfigRoot;
exports.setSessionProjectRoot = setSessionProjectRoot;
exports.getSessionProjectRoot = getSessionProjectRoot;
exports.findConfigRoot = findConfigRoot;
exports.findProjectRoot = findProjectRoot;
const constants_1 = require("../../constants");
const errors_1 = require("../../errors");
const fs_1 = require("fs");
const path_1 = require("path");
exports.CONFIG_FILES = constants_1.CONFIG_FILES;
/**
 * Get the working directory where the user invoked the CLI.
 *
 * When running via npm/bun scripts (e.g., `npm run cli`), the package manager
 * changes process.cwd() to the package root. INIT_CWD preserves the original
 * directory where the user ran the command.
 *
 * For globally installed CLIs, INIT_CWD is not set, so we fall back to process.cwd().
 */
function getWorkingDirectory() {
    return process.env.INIT_CWD ?? process.cwd();
}
/**
 * Find the config root, throwing NoProjectError if not found.
 * Use this when a project is required to proceed.
 */
function requireConfigRoot(startDir) {
    const configRoot = findConfigRoot(startDir);
    if (!configRoot) {
        throw new errors_1.NoProjectError();
    }
    return configRoot;
}
/**
 * Session-level project root override.
 * Set this after init creates a project so subsequent discovery finds it.
 */
let sessionProjectRoot = null;
/**
 * Set the project root for the current session.
 * Call this after init creates a new project at cwd/projectName/.
 * Subsequent calls to findConfigRoot() will check this location first.
 */
function setSessionProjectRoot(projectRoot) {
    sessionProjectRoot = projectRoot;
}
/**
 * Get the current session project root, if set.
 */
function getSessionProjectRoot() {
    return sessionProjectRoot;
}
/**
 * Check if a directory is a valid AgentCore config directory.
 * A valid config directory contains agentcore.json.
 */
function isValidConfigDir(configPath) {
    return (0, fs_1.existsSync)((0, path_1.join)(configPath, constants_1.CONFIG_FILES.AGENT_ENV));
}
/**
 * Walk up the directory tree from startDir looking for an agentcore directory.
 * If a session project was set (via setSessionProjectRoot), checks there first.
 * Returns the path to the agentcore directory if found, or null if not found.
 */
function findConfigRoot(startDir = getWorkingDirectory()) {
    // Check session project first (set after init creates a project)
    if (sessionProjectRoot) {
        const sessionConfigPath = (0, path_1.join)(sessionProjectRoot, constants_1.CONFIG_DIR);
        if ((0, fs_1.existsSync)(sessionConfigPath) && isValidConfigDir(sessionConfigPath)) {
            return sessionConfigPath;
        }
    }
    // Fall back to walking up the directory tree
    let currentDir = startDir;
    while (true) {
        const configPath = (0, path_1.join)(currentDir, constants_1.CONFIG_DIR);
        if ((0, fs_1.existsSync)(configPath) && isValidConfigDir(configPath)) {
            return configPath;
        }
        const parentDir = (0, path_1.dirname)(currentDir);
        // Reached filesystem root
        if (parentDir === currentDir) {
            return null;
        }
        currentDir = parentDir;
    }
}
/**
 * Get the project root directory (parent of agentcore/).
 * Returns null if no project is found.
 */
function findProjectRoot(startDir = process.cwd()) {
    const configRoot = findConfigRoot(startDir);
    return configRoot ? (0, path_1.dirname)(configRoot) : null;
}
/**
 * Default configuration uses agentcore/ in current working directory.
 */
exports.DEFAULT_PATH_CONFIG = {
    baseDir: (0, path_1.join)(getWorkingDirectory(), constants_1.CONFIG_DIR),
};
/**
 * Utility class for resolving AgentCore file paths
 */
class PathResolver {
    config;
    constructor(config) {
        this.config = {
            ...exports.DEFAULT_PATH_CONFIG,
            ...config,
        };
    }
    /**
     * Get the base directory path (the agentcore/ config directory)
     */
    getBaseDir() {
        return this.config.baseDir;
    }
    /**
     * Get the project root directory (parent of agentcore/)
     */
    getProjectRoot() {
        return (0, path_1.dirname)(this.config.baseDir);
    }
    /**
     * Get the path to the agent config file (agentcore.json)
     */
    getAgentConfigPath() {
        return (0, path_1.join)(this.config.baseDir, exports.CONFIG_FILES.AGENT_ENV);
    }
    /**
     * Get the path to the AWS targets config file (aws-targets.json)
     */
    getAWSTargetsConfigPath() {
        return (0, path_1.join)(this.config.baseDir, exports.CONFIG_FILES.AWS_TARGETS);
    }
    /**
     * Get the path to the CLI system directory (agentcore/.cli/)
     */
    getCliSystemDir() {
        return (0, path_1.join)(this.config.baseDir, constants_1.CLI_SYSTEM_DIR);
    }
    /**
     * Get the path to the logs directory (agentcore/.cli/logs/)
     */
    getLogsDir() {
        return (0, path_1.join)(this.config.baseDir, constants_1.CLI_SYSTEM_DIR, constants_1.CLI_LOGS_DIR);
    }
    /**
     * Get the path to the invoke logs directory (agentcore/.cli/logs/invoke/)
     */
    getInvokeLogsDir() {
        return (0, path_1.join)(this.config.baseDir, constants_1.CLI_SYSTEM_DIR, constants_1.CLI_LOGS_DIR, 'invoke');
    }
    /**
     * Get the path to the deployed state file (agentcore/.cli/deployed-state.json)
     */
    getStatePath() {
        return (0, path_1.join)(this.config.baseDir, constants_1.CLI_SYSTEM_DIR, exports.CONFIG_FILES.DEPLOYED_STATE);
    }
    /**
     * Get the path to the MCP definitions file (mcp-defs.json)
     */
    getMcpDefsPath() {
        return (0, path_1.join)(this.config.baseDir, exports.CONFIG_FILES.MCP_DEFS);
    }
    /**
     * Update the base directory
     */
    setBaseDir(baseDir) {
        this.config.baseDir = baseDir;
    }
}
exports.PathResolver = PathResolver;
//# sourceMappingURL=path-resolver.js.map