import { spawnSync } from "child_process";
import { accessSync, constants, existsSync, readFileSync, realpathSync } from "fs";
import { homedir } from "os";
import { basename, dirname, join, resolve, sep, win32 } from "path";
import { fileURLToPath } from "url";
import { shouldUseWindowsShell } from "./utils/child-process.js";
// =============================================================================
// Package Detection
// =============================================================================
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/**
 * Detect if we're running as a Bun compiled binary.
 * Bun binaries have import.meta.url containing "$bunfs", "~BUN", or "%7EBUN" (Bun's virtual filesystem path)
 */
export const isBunBinary = import.meta.url.includes("$bunfs") || import.meta.url.includes("~BUN") || import.meta.url.includes("%7EBUN");
/** Detect if Bun is the runtime (compiled binary or bun run) */
export const isBunRuntime = !!process.versions.bun;
/** Returns the path used for install method detection. Exported for testing. */
export function getInstallPath() {
    // AERY_PACKAGE_DIR overrides __dirname for testing and non-standard installs
    const dir = process.env.AERY_PACKAGE_DIR ?? __dirname;
    return `${dir}\0${process.execPath || ""}`;
}
export function detectInstallMethod() {
    if (isBunBinary) {
        return "bun-binary";
    }
    const resolvedPath = getInstallPath().toLowerCase().replace(/\\/g, "/");
    if (resolvedPath.includes("/pnpm/") || resolvedPath.includes("/.pnpm/")) {
        return "pnpm";
    }
    if (resolvedPath.includes("/yarn/") || resolvedPath.includes("/.yarn/")) {
        return "yarn";
    }
    if (isBunRuntime || resolvedPath.includes("/install/global/node_modules/")) {
        return "bun";
    }
    if (resolvedPath.includes("/npm/") || resolvedPath.includes("/node_modules/")) {
        return "npm";
    }
    return "unknown";
}
function getInferredNpmInstall(packageName) {
    const packageDir = getPackageDir();
    const path = process.platform === "win32" || packageDir.includes("\\") ? win32 : { basename, dirname };
    const [scope, name] = packageName.split("/");
    let root;
    if (name &&
        scope?.startsWith("@") &&
        path.basename(path.dirname(packageDir)) === scope &&
        path.basename(packageDir) === name) {
        root = path.dirname(path.dirname(packageDir));
    }
    else if (!name && path.basename(packageDir) === packageName) {
        root = path.dirname(packageDir);
    }
    if (!root || path.basename(root) !== "node_modules")
        return undefined;
    const parent = path.dirname(root);
    if (path.basename(parent) === "lib")
        return { root, prefix: path.dirname(parent) };
    // Windows global npm prefixes use `<prefix>\\node_modules`, which is
    // indistinguishable from local project installs by path shape alone. Do not
    // infer unsupported Windows custom prefixes without `npm root -g` evidence.
    return undefined;
}
function getSelfUpdateCommandForMethod(method, packageName, npmCommand) {
    switch (method) {
        case "bun-binary":
            return undefined;
        case "pnpm":
            return { command: "pnpm", args: ["install", "-g", packageName], display: `pnpm install -g ${packageName}` };
        case "yarn":
            return { command: "yarn", args: ["global", "add", packageName], display: `yarn global add ${packageName}` };
        case "bun":
            return { command: "bun", args: ["install", "-g", packageName], display: `bun install -g ${packageName}` };
        case "npm": {
            const [command = "npm", ...npmArgs] = npmCommand ?? [];
            const inferred = npmCommand?.length ? undefined : getInferredNpmInstall(packageName);
            const args = [...npmArgs, ...(inferred ? ["--prefix", inferred.prefix] : []), "install", "-g", packageName];
            const display = [command, ...args].map((arg) => (/\s/.test(arg) ? `"${arg}"` : arg)).join(" ");
            return { command, args, display };
        }
        case "unknown":
            return undefined;
    }
}
function readCommandOutput(command, args, options = {}) {
    const result = spawnSync(command, args, {
        encoding: "utf-8",
        stdio: ["ignore", "pipe", "pipe"],
        shell: shouldUseWindowsShell(command),
    });
    if (result.status === 0)
        return result.stdout.trim() || undefined;
    if (options.requireSuccess) {
        const reason = result.error?.message || result.stderr.trim() || `exit code ${result.status ?? "unknown"}`;
        throw new Error(`Failed to run ${[command, ...args].join(" ")}: ${reason}`);
    }
    return undefined;
}
function getGlobalPackageRoots(method, packageName, npmCommand) {
    switch (method) {
        case "npm": {
            const configured = !!npmCommand?.length;
            const [command = "npm", ...npmArgs] = npmCommand ?? [];
            if (configured && command === "bun") {
                const bunBin = readCommandOutput(command, [...npmArgs, "pm", "bin", "-g"], {
                    requireSuccess: true,
                });
                const roots = [join(homedir(), ".bun", "install", "global", "node_modules")];
                if (bunBin) {
                    roots.push(join(dirname(bunBin), "install", "global", "node_modules"));
                }
                return roots;
            }
            const root = readCommandOutput(command, [...npmArgs, "root", "-g"], {
                requireSuccess: configured,
            });
            const inferred = configured ? undefined : getInferredNpmInstall(packageName);
            return [root, inferred?.root].filter((x) => !!x);
        }
        case "pnpm": {
            const root = readCommandOutput("pnpm", ["root", "-g"]);
            return root ? [root, dirname(root)] : [];
        }
        case "yarn": {
            const dir = readCommandOutput("yarn", ["global", "dir"]);
            return dir ? [dir, join(dir, "node_modules")] : [];
        }
        case "bun": {
            const bunBin = readCommandOutput("bun", ["pm", "bin", "-g"]);
            const roots = [join(homedir(), ".bun", "install", "global", "node_modules")];
            if (bunBin) {
                roots.push(join(dirname(bunBin), "install", "global", "node_modules"));
            }
            return roots;
        }
        case "bun-binary":
        case "unknown":
            return [];
    }
}
function normalizeExistingPathForComparison(path) {
    const resolvedPath = resolve(path);
    if (!existsSync(resolvedPath)) {
        return undefined;
    }
    let normalizedPath;
    try {
        normalizedPath = realpathSync(resolvedPath);
    }
    catch {
        return undefined;
    }
    if (process.platform === "win32") {
        normalizedPath = normalizedPath.toLowerCase();
    }
    return normalizedPath;
}
function isSelfUpdatePathWritable() {
    const packageDir = getPackageDir();
    try {
        accessSync(packageDir, constants.W_OK);
        accessSync(dirname(packageDir), constants.W_OK);
        return true;
    }
    catch {
        return false;
    }
}
function isManagedByGlobalPackageManager(method, packageName, npmCommand) {
    const packageDir = normalizeExistingPathForComparison(getPackageDir());
    return (!!packageDir &&
        getGlobalPackageRoots(method, packageName, npmCommand).some((root) => {
            const normalizedRoot = normalizeExistingPathForComparison(root);
            return (!!normalizedRoot &&
                packageDir.startsWith(normalizedRoot.endsWith(sep) ? normalizedRoot : `${normalizedRoot}${sep}`));
        }));
}
export function getSelfUpdateCommand(packageName, npmCommand) {
    const method = detectInstallMethod();
    const command = getSelfUpdateCommandForMethod(method, packageName, npmCommand);
    if (!command || !isManagedByGlobalPackageManager(method, packageName, npmCommand) || !isSelfUpdatePathWritable()) {
        return undefined;
    }
    return command;
}
export function getSelfUpdateUnavailableInstruction(packageName, npmCommand) {
    const method = detectInstallMethod();
    if (method === "bun-binary") {
        return `Download from: https://github.com/eminent337/aery/releases/latest`;
    }
    const command = getSelfUpdateCommandForMethod(method, packageName, npmCommand);
    if (command) {
        if (isManagedByGlobalPackageManager(method, packageName, npmCommand) && !isSelfUpdatePathWritable()) {
            return `This installation is managed by a global ${method} install, but the install path is not writable. Update it yourself with: ${command.display}`;
        }
        return `This installation is not managed by a global ${method} install. Update it with the package manager, wrapper, or source checkout that provides it.`;
    }
    return `Update ${packageName} using the package manager, wrapper, or source checkout that provides this installation.`;
}
export function getUpdateInstruction(packageName) {
    const method = detectInstallMethod();
    const command = getSelfUpdateCommandForMethod(method, packageName);
    if (command) {
        return `Run: aery update`;
    }
    return getSelfUpdateUnavailableInstruction(packageName);
}
// =============================================================================
// Package Asset Paths (shipped with executable)
// =============================================================================
/**
 * Get the base directory for resolving package assets (themes, package.json, README.md, CHANGELOG.md).
 * - For Bun binary: returns the directory containing the executable
 * - For Node.js (dist/): returns __dirname (the dist/ directory)
 * - For tsx (src/): returns parent directory (the package root)
 */
export function getPackageDir() {
    // Allow override via environment variable (useful for Nix/Guix where store paths tokenize poorly)
    const envDir = process.env.AERY_PACKAGE_DIR;
    if (envDir) {
        if (envDir === "~")
            return homedir();
        if (envDir.startsWith("~/"))
            return homedir() + envDir.slice(1);
        return envDir;
    }
    if (isBunBinary) {
        // Bun binary: process.execPath points to the compiled executable
        return dirname(process.execPath);
    }
    // Node.js: walk up from __dirname until we find package.json
    let dir = __dirname;
    while (dir !== dirname(dir)) {
        if (existsSync(join(dir, "package.json"))) {
            return dir;
        }
        dir = dirname(dir);
    }
    // Fallback (shouldn't happen)
    return __dirname;
}
/**
 * Get path to built-in themes directory (shipped with package)
 * - For Bun binary: theme/ next to executable
 * - For Node.js (dist/): dist/modes/interactive/theme/
 * - For tsx (src/): src/modes/interactive/theme/
 */
export function getThemesDir() {
    if (isBunBinary) {
        return join(getPackageDir(), "theme");
    }
    // Theme is in modes/interactive/theme/ relative to src/ or dist/
    const packageDir = getPackageDir();
    const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
    return join(packageDir, srcOrDist, "modes", "interactive", "theme");
}
/**
 * Get path to HTML export template directory (shipped with package)
 * - For Bun binary: export-html/ next to executable
 * - For Node.js (dist/): dist/core/export-html/
 * - For tsx (src/): src/core/export-html/
 */
export function getExportTemplateDir() {
    if (isBunBinary) {
        return join(getPackageDir(), "export-html");
    }
    const packageDir = getPackageDir();
    const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
    return join(packageDir, srcOrDist, "core", "export-html");
}
/** Get path to package.json */
export function getPackageJsonPath() {
    return join(getPackageDir(), "package.json");
}
/** Get path to README.md */
export function getReadmePath() {
    return resolve(join(getPackageDir(), "README.md"));
}
/** Get path to docs directory */
export function getDocsPath() {
    return resolve(join(getPackageDir(), "docs"));
}
/** Get path to examples directory */
export function getExamplesPath() {
    return resolve(join(getPackageDir(), "examples"));
}
/** Get path to CHANGELOG.md */
export function getChangelogPath() {
    return resolve(join(getPackageDir(), "CHANGELOG.md"));
}
/**
 * Get path to built-in interactive assets directory.
 * - For Bun binary: assets/ next to executable
 * - For Node.js (dist/): dist/modes/interactive/assets/
 * - For tsx (src/): src/modes/interactive/assets/
 */
export function getInteractiveAssetsDir() {
    if (isBunBinary) {
        return join(getPackageDir(), "assets");
    }
    const packageDir = getPackageDir();
    const srcOrDist = existsSync(join(packageDir, "src")) ? "src" : "dist";
    return join(packageDir, srcOrDist, "modes", "interactive", "assets");
}
/** Get path to a bundled interactive asset */
export function getBundledInteractiveAssetPath(name) {
    return join(getInteractiveAssetsDir(), name);
}
const pkg = JSON.parse(readFileSync(getPackageJsonPath(), "utf-8"));
export const APP_NAME = pkg.aeryConfig?.name || "aery";
export const CONFIG_DIR_NAME = pkg.aeryConfig?.configDir || ".aery";
export const VERSION = pkg.version ?? "0.0.0";
// e.g., AERY_CODING_AGENT_DIR or TAU_CODING_AGENT_DIR
export const ENV_AGENT_DIR = `${APP_NAME.toUpperCase()}_CODING_AGENT_DIR`;
const DEFAULT_SHARE_VIEWER_URL = "https://aery.dev/session/";
/** Get the share viewer URL for a gist ID */
export function getShareViewerUrl(gistId) {
    const baseUrl = process.env.AERY_SHARE_VIEWER_URL || DEFAULT_SHARE_VIEWER_URL;
    return `${baseUrl}#${gistId}`;
}
// =============================================================================
// User Config Paths (~/.aery/agent/*)
// =============================================================================
/** Get the agent config directory (e.g., ~/.aery/agent/) */
export function getAgentDir() {
    const envDir = process.env[ENV_AGENT_DIR];
    if (envDir) {
        // Expand tilde to home directory
        if (envDir === "~")
            return homedir();
        if (envDir.startsWith("~/"))
            return homedir() + envDir.slice(1);
        return envDir;
    }
    return join(homedir(), CONFIG_DIR_NAME, "agent");
}
/** Get path to user's custom themes directory */
export function getCustomThemesDir() {
    return join(getAgentDir(), "themes");
}
/** Get path to models.json */
export function getModelsPath() {
    return join(getAgentDir(), "models.json");
}
/** Get path to auth.json */
export function getAuthPath() {
    return join(getAgentDir(), "auth.json");
}
/** Get path to settings.json */
export function getSettingsPath() {
    return join(getAgentDir(), "settings.json");
}
/** Get path to tools directory */
export function getToolsDir() {
    return join(getAgentDir(), "tools");
}
/** Get path to managed binaries directory (fd, rg) */
export function getBinDir() {
    return join(getAgentDir(), "bin");
}
/** Get path to prompt templates directory */
export function getPromptsDir() {
    return join(getAgentDir(), "prompts");
}
/** Get path to sessions directory */
export function getSessionsDir() {
    return join(getAgentDir(), "sessions");
}
/** Get path to debug log file */
export function getDebugLogPath() {
    return join(getAgentDir(), `${APP_NAME}-debug.log`);
}
export const PACKAGE_NAME = pkg.name ?? "@eminent337/aery";
export const APP_TITLE = APP_NAME;
//# sourceMappingURL=config.js.map