/**
 * CLI context — mirrors the MCP server's OlamContext initialization.
 *
 * Loads config, creates WorldManager, CostTracker, and compute provider.
 * Returns undefined with an error if Olam is not configured.
 */
export async function loadContext() {
    try {
        const { loadConfig } = await import('@olam/core/src/config/loader.js');
        const { WorldManager } = await import('@olam/core/src/world/manager.js');
        const { WorldRegistry } = await import('@olam/core/src/world/registry.js');
        const { CostTracker } = await import('@olam/core/src/cost/tracker.js');
        const { DockerProvider } = await import('@olam/adapters');
        const { DashboardManager } = await import('@olam/core/src/dashboard/index.js');
        const { PleriClient } = await import('@olam/core/src/pleri/index.js');
        const config = loadConfig(process.env.OLAM_CONFIG_DIR);
        const registry = new WorldRegistry();
        // Resolve docker daemon endpoint via the CLI's active context — handles
        // Docker Desktop's `~/.docker/run/docker.sock` path on macOS where the
        // default `/var/run/docker.sock` doesn't exist. See packages/cli/src/docker-host.ts.
        const { resolveDockerHostOptions } = await import('./docker-host.js');
        const computeProvider = new DockerProvider(resolveDockerHostOptions());
        const dashboardManager = new DashboardManager(config.dashboard);
        let pleriClient;
        if (config.pleri) {
            pleriClient = new PleriClient({
                baseUrl: config.pleri.base_url,
                planeId: config.pleri.plane_id,
                apiKey: config.pleri.api_key,
            });
        }
        const worldManager = new WorldManager(config, computeProvider, registry, dashboardManager, pleriClient);
        const costTracker = new CostTracker({
            maxPerWorldUsd: config.cost.max_per_world_usd,
            maxDailyUsd: config.cost.max_daily_usd,
            warningThreshold: config.cost.warning_threshold,
        });
        return {
            ctx: {
                worldManager,
                costTracker,
                computeProvider,
                config,
                dashboardManager,
                pleriClient,
            },
        };
    }
    catch (err) {
        return {
            error: err instanceof Error ? err : new Error(String(err)),
        };
    }
}
//# sourceMappingURL=context.js.map