/**
 * bundle-source.ts — Phase C C2 (olam-world-bundle-freshness).
 *
 * Single resolver for the agent-stream bind-mount source path. Two modes:
 *
 *   source-mode  (OLAM_DEV=1 + packages/ sibling in repo root):
 *     → <repo>/packages/intelligence/dist/agent-stream
 *
 *   install-mode (OLAM_FORCE_INSTALL_MODE=1, or OLAM_DEV is absent/not '1',
 *                 or the packages/ sibling is absent):
 *     → <cli-pkg-root>/dist/agent-stream
 *     (the path populated by bundle-agent-stream.mjs at publish time)
 *
 * Throws `BundleSourceNotFoundError` with a remediation message when neither
 * path exists on disk.
 *
 * Design decisions:
 *   - Reuses `installRoot()` + `isDevMode()` from install-root.ts (the seam
 *     established by Phase B / fresh-install-self-bootstrap). No new heuristics.
 *   - `OLAM_FORCE_INSTALL_MODE=1` forces install-mode regardless of cwd; used
 *     by the C4 integration test to exercise install-mode paths without a real
 *     global install.
 *   - Does NOT import from @olam/adapters (dep cycle — adapters cannot import
 *     from @pleri/olam-cli). The container.ts refactor in Phase C receives the
 *     resolved path as an argument instead of computing it internally.
 */
import { existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { installRoot, isDevMode } from '../install-root.js';
/**
 * Thrown when neither the source-mode nor the install-mode bundle directory
 * exists on disk. Carries a remediation message operators can act on.
 */
export class BundleSourceNotFoundError extends Error {
    constructor(sourceModePath, installModePath) {
        super(`agent-stream bundle not found in either location:\n` +
            `  source-mode path: ${sourceModePath}\n` +
            `  install-mode path: ${installModePath}\n` +
            `\n` +
            `Remediation:\n` +
            `  In a monorepo clone (OLAM_DEV=1): run \`npm run build --workspace=@olam/intelligence\`\n` +
            `  In a global install: reinstall or update — \`npm install -g @pleri/olam-cli@latest\`\n` +
            `    (the published tarball ships dist/agent-stream/ as of Phase C)\n`);
        this.name = 'BundleSourceNotFoundError';
    }
}
/**
 * Resolve the agent-stream bind-mount source for the current runtime context.
 *
 * @param env - Process environment. Defaults to `process.env`. Injectable for
 *   tests — pass `{ OLAM_FORCE_INSTALL_MODE: '1' }` to exercise install-mode.
 * @param installRootDir - Override the CLI package root. Defaults to
 *   `installRoot()`. Injectable for tests.
 * @returns `BundleSource` with the resolved mode and absolute directory path.
 * @throws `BundleSourceNotFoundError` when neither path exists.
 */
export function resolveBundleSource(env = process.env, installRootDir = installRoot()) {
    // OLAM_FORCE_INSTALL_MODE=1 skips the source-mode check unconditionally.
    // Used by the C4 integration test to force install-mode without a real
    // global install.
    const forceInstall = env['OLAM_FORCE_INSTALL_MODE'] === '1';
    // Source-mode: OLAM_DEV=1 + packages/ sibling + package.json (per isDevMode).
    if (!forceInstall && isDevMode(env, installRootDir)) {
        // installRoot sits at <repo>/packages/cli; walk up two levels to repo root.
        const repoRoot = resolve(installRootDir, '..', '..');
        const sourcePath = join(repoRoot, 'packages', 'intelligence', 'dist', 'agent-stream');
        const installPath = join(installRootDir, 'dist', 'agent-stream');
        if (!existsSync(sourcePath)) {
            throw new BundleSourceNotFoundError(sourcePath, installPath);
        }
        return { mode: 'source', path: sourcePath };
    }
    // Install-mode: use the CLI's own dist/agent-stream/ (shipped via C1).
    const installPath = join(installRootDir, 'dist', 'agent-stream');
    // Source path for error message.
    const repoRoot = resolve(installRootDir, '..', '..');
    const sourcePath = join(repoRoot, 'packages', 'intelligence', 'dist', 'agent-stream');
    if (!existsSync(installPath)) {
        throw new BundleSourceNotFoundError(sourcePath, installPath);
    }
    return { mode: 'install', path: installPath };
}
//# sourceMappingURL=bundle-source.js.map