/**
 * Resolve the executable path that `@anthropic-ai/claude-agent-sdk` should
 * spawn as its "Claude Code" subprocess.
 *
 * Why we need this:
 *   - In 0.2.108–0.2.112 the SDK shipped a `cli.js` alongside `sdk.mjs`, and
 *     the intended way to pin it from a bundled app was to pass
 *     `pathToClaudeCodeExecutable` pointing at that file.
 *   - Starting with 0.2.113 (published 2026-04-14) the SDK tarball dropped
 *     `cli.js` entirely and switched to a platform-specific native binary
 *     shipped via an optional peer package
 *     `@anthropic-ai/claude-agent-sdk-<platform>-<arch>`.
 *   - Our previous implementation hard-coded `cli.js`, so a stock
 *     `pnpm install` that pulled the newer SDK caused the child process to
 *     fail with `MODULE_NOT_FOUND`, which in turn surfaced as an async EPIPE
 *     in the parent.
 *
 * Resolution order:
 *   1. The native binary from `claude-agent-sdk-<platform>-<arch>/claude`
 *      (the forward-compatible choice for 0.2.113+).
 *   2. A legacy `cli.js` next to `sdk.mjs` (still valid for 0.2.112 and older).
 *   3. `null`, meaning: let the SDK resolve the executable itself. Recent SDK
 *      versions implement the same fallback chain internally and throw a
 *      clear `ReferenceError` if nothing is found, which is much easier to
 *      diagnose than a silent EPIPE.
 */
import { createRequire } from "module";
import fs from "fs";
import path from "path";
/**
 * Resolve once per process — filesystem lookups here are cheap but the result
 * never changes for a given installation.
 */
let cached;
export function resolveClaudeCliExecutable() {
    if (cached)
        return cached;
    cached = doResolve();
    return cached;
}
/** Convenience wrapper — return just the path, or `undefined` when we should
 *  leave the option unset and let the SDK do its own lookup. */
export function getClaudeExecutablePath() {
    const r = resolveClaudeCliExecutable();
    return r.kind === "sdk-default" ? undefined : r.path;
}
function doResolve() {
    const _require = createRequire(import.meta.url);
    // 1. Modern native binary (claude-agent-sdk >= 0.2.113).
    const nativePkg = `@anthropic-ai/claude-agent-sdk-${process.platform}-${process.arch}`;
    try {
        const pkgJsonPath = _require.resolve(`${nativePkg}/package.json`);
        const pkgDir = path.dirname(pkgJsonPath);
        // The package ships a single executable named `claude`.
        const candidate = path.join(pkgDir, "claude");
        if (fs.existsSync(candidate)) {
            return { kind: "native", path: candidate };
        }
    }
    catch {
        // Optional dep not installed — fall through to legacy cli.js.
    }
    // 2. Legacy JS entry (claude-agent-sdk <= 0.2.112).
    try {
        const sdkDir = path.dirname(_require.resolve("@anthropic-ai/claude-agent-sdk"));
        const candidate = path.join(sdkDir, "cli.js");
        if (fs.existsSync(candidate)) {
            return { kind: "legacy-cli-js", path: candidate };
        }
    }
    catch {
        // No SDK at all — let the caller's catch handle this.
    }
    // 3. Give up and let the SDK resolve on its own. Newer versions still have
    //    a reasonable internal lookup and will throw a clear error if nothing
    //    works, rather than silently spawning a missing module.
    return { kind: "sdk-default" };
}
/** For tests. */
export function _resetClaudeCliResolverCache() {
    cached = undefined;
}
//# sourceMappingURL=cli-resolver.js.map