import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
/**
 * Resolve project directories relative to CWD.
 */
export function resolveConfig() {
    const projectRoot = process.cwd();
    return {
        projectRoot,
        squadsDir: path.join(projectRoot, 'squads'),
        skillsDir: path.join(projectRoot, 'skills'),
        coreDir: path.join(projectRoot, 'core'),
        serverDir: path.join(projectRoot, 'server'),
    };
}
/**
 * Check if current directory is an initialized ExpxAgents project.
 */
export function isInitialized() {
    const cwd = process.cwd();
    return fs.existsSync(path.join(cwd, 'squads'))
        && fs.existsSync(path.join(cwd, '_expxagents', '_memory'));
}
/**
 * Find the package root by walking up from the compiled file's location
 * until we find a package.json. Works regardless of dist/ nesting depth.
 */
export function findPackageRoot() {
    let dir = path.dirname(fileURLToPath(import.meta.url));
    while (dir !== path.dirname(dir)) {
        if (fs.existsSync(path.join(dir, 'package.json')))
            return dir;
        dir = path.dirname(dir);
    }
    throw new Error('Could not find expxagents package root');
}
/**
 * Get the directory where package assets are stored.
 * Resolves to <package-root>/assets/ regardless of compiled file nesting.
 * Works both in development (monorepo) and production (npx/global install).
 */
export function getAssetsDir() {
    return path.join(findPackageRoot(), 'assets');
}
/**
 * Resolve a core asset path (solution-architect, runner, best-practices, etc).
 * Falls back to monorepo path for development.
 */
export function getCoreAsset(relativePath) {
    const assetsPath = path.join(getAssetsDir(), 'core', relativePath);
    if (fs.existsSync(assetsPath))
        return assetsPath;
    // Fallback: monorepo development path
    const monoPath = path.join(process.cwd(), '_expxagents', 'core', relativePath);
    if (fs.existsSync(monoPath))
        return monoPath;
    return assetsPath;
}
/**
 * Resolve the agent catalog directory.
 */
export function getAgentCatalogDir() {
    const assetsPath = path.join(getAssetsDir(), 'agents');
    if (fs.existsSync(assetsPath))
        return assetsPath;
    const monoPath = path.join(process.cwd(), 'agents');
    return fs.existsSync(monoPath) ? monoPath : assetsPath;
}
/**
 * Resolve the templates directory.
 */
export function getTemplateDir() {
    const assetsPath = path.join(getAssetsDir(), 'templates');
    if (fs.existsSync(assetsPath))
        return assetsPath;
    const monoPath = path.join(process.cwd(), 'templates');
    return fs.existsSync(monoPath) ? monoPath : assetsPath;
}
