// packages/plugin-awf/src/commands/install-helpers.ts
// Helper functions extracted from install.ts for better modularity
import * as fs from "node:fs";
import * as path from "node:path";
// ══════════════════════════════════════════════════════════════
// Source Directory Resolution
// ══════════════════════════════════════════════════════════════
/**
 * Find source directories for AWF components.
 * Resolves PKG_ROOT from dist/commands/ → plugin-awf/ root.
 */
export function findSourceDirs() {
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    // From dist/commands/install.js -> go up 2 levels to reach plugin-awf/
    const PKG_ROOT = path.resolve(import.meta.dirname || __dirname, "..", "..");
    // Source directory for skills/workflows/rules (from .agent/)
    const agentDir = path.join(PKG_ROOT, ".agent");
    let sourceDir;
    if (fs.existsSync(agentDir)) {
        sourceDir = PKG_ROOT;
    }
    else {
        // Monorepo fallback: .agent/ may live in sibling domyh-awf/ directory
        const monorepoRoot = path.resolve(PKG_ROOT, "..", "..", "..");
        const domyhAwfAgent = path.join(monorepoRoot, "domyh-awf", ".agent");
        if (fs.existsSync(domyhAwfAgent)) {
            sourceDir = path.join(monorepoRoot, "domyh-awf");
        }
    }
    // Configs directory for IDE-specific root configs
    let configsDirExists;
    const configsDir = path.join(PKG_ROOT, "configs");
    if (fs.existsSync(configsDir)) {
        configsDirExists = configsDir;
    }
    else if (sourceDir) {
        // Fallback: configs may also live alongside .agent/
        const sourceConfigs = path.join(sourceDir, "configs");
        if (fs.existsSync(sourceConfigs)) {
            configsDirExists = sourceConfigs;
        }
    }
    return { configsDir: configsDirExists, sourceDir };
}
// ══════════════════════════════════════════════════════════════
// Language Resolution (i18n)
// ══════════════════════════════════════════════════════════════
/**
 * Resolve language preference for i18n workflow descriptions.
 * Priority: CLI --language flag → CWD state.json → rootDir state.json → default "en"
 *
 * CWD is checked first because `nock awf init` saves language to CWD/.agent/memory/state.json
 * rootDir may be a different location (e.g., global install path like ~/.gemini/antigravity/)
 */
export function resolveLanguage(langFlag, rootDir) {
    // 0. CLI flag takes highest priority
    if (langFlag && ["en", "vi"].includes(langFlag))
        return langFlag;
    // 1. Try CWD state.json FIRST (project-level, where init wizard saves language)
    const cwdState = path.join(process.cwd(), ".agent", "memory", "state.json");
    if (fs.existsSync(cwdState)) {
        try {
            const state = JSON.parse(fs.readFileSync(cwdState, "utf-8"));
            const lang = state?.preferences?.language;
            if (lang && ["en", "vi"].includes(lang))
                return lang;
        }
        catch {
            // Ignore parse errors, use fallback
        }
    }
    // 2. Try rootDir state.json (target directory, may differ from CWD for global installs)
    const stateFile = path.join(rootDir, ".agent", "memory", "state.json");
    if (stateFile !== cwdState && fs.existsSync(stateFile)) {
        try {
            const state = JSON.parse(fs.readFileSync(stateFile, "utf-8"));
            const lang = state?.preferences?.language;
            if (lang && ["en", "vi"].includes(lang))
                return lang;
        }
        catch {
            // Ignore parse errors, use fallback
        }
    }
    // 3. Default fallback — AWF convention: Vietnamese
    return "vi";
}
// ══════════════════════════════════════════════════════════════
// Global Target Resolution
// ══════════════════════════════════════════════════════════════
/**
 * Resolve globalTarget overrides for global scope installation.
 * IDEs like Antigravity need different target paths for global vs project:
 * - Project: .agent/workflows/ (workspace-level)
 * - Global: global_workflows/ (relative to ~/.gemini/antigravity/)
 */
export function resolveGlobalTargets(spec) {
    return {
        ...spec,
        skills: {
            ...spec.skills,
            target: spec.skills.globalTarget || spec.skills.target,
        },
        workflows: {
            ...spec.workflows,
            target: spec.workflows.globalTarget || spec.workflows.target,
        },
        rules: {
            ...spec.rules,
            target: spec.rules.globalTarget || spec.rules.target,
        },
        personas: {
            ...spec.personas,
            target: spec.personas.globalTarget || spec.personas.target,
        },
        templates: {
            ...spec.templates,
            target: spec.templates.globalTarget || spec.templates.target,
        },
        rootConfig: {
            ...spec.rootConfig,
            target: spec.rootConfig.globalTarget || spec.rootConfig.target,
        },
    };
}
// ══════════════════════════════════════════════════════════════
// Result Helpers
// ══════════════════════════════════════════════════════════════
export function hasInstallContent(result) {
    return (result.skills.count > 0 ||
        result.workflows.count > 0 ||
        result.rules.count > 0 ||
        result.personas.count > 0 ||
        result.templates.count > 0 ||
        result.hsa.count > 0 ||
        result.rootConfig);
}
export function hasErrors(result) {
    return (result.skills.error !== null ||
        result.workflows.error !== null ||
        result.rules.error !== null ||
        result.core.error !== null ||
        result.memory.error !== null ||
        result.personas.error !== null ||
        result.templates.error !== null ||
        result.hsa.error !== null);
}
