"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAgentVariantsOrchestrator = void 0;
const index_core_1 = require("../index-core");
const agent_variants_1 = require("../mcp/agent-variants");
const devServerCommand_1 = require("../utils/devServerCommand");
const ProjectDetectionService_1 = require("./ProjectDetectionService");
const WorktreeManager_1 = require("./WorktreeManager");
const log = (0, index_core_1.createLogger)('AgentVariantsOrchestratorFactory');
/**
 * Creates the shared variants orchestration stack for embedded and MCP-driven agent sessions.
 */
const createAgentVariantsOrchestrator = (options) => {
    const cwd = options.cwd ?? (() => process.cwd());
    const getProjectPath = () => options.bridge.getProjectPath() ?? options.projectPathFallback ?? cwd();
    return new agent_variants_1.AgentVariantsOrchestrator({
        store: new agent_variants_1.SessionStore(),
        worktreeManager: options.worktreeManager ?? new WorktreeManager_1.WorktreeManager(getProjectPath),
        pendingChangesAdapter: new agent_variants_1.PendingChangesAdapter(options.bridge),
        telemetry: options.telemetry,
        resolveProjectEnvironment: options.resolveProjectEnvironment ??
            (async (sessionId) => {
                const projectPath = getProjectPath();
                const detection = new ProjectDetectionService_1.ProjectDetectionService();
                const framework = detection.detectFramework(projectPath);
                const packageManager = await detection.detectPackageManager(projectPath);
                const devCommand = devServerCommand_1.FRAMEWORK_DEV_CONFIG[framework]?.devCommand ?? 'dev';
                log.info(`Resolved env for variants session ${sessionId}: ${framework}/${packageManager}`);
                return {
                    projectPath,
                    framework,
                    packageManager,
                    devCommand,
                    buildDevCommand: (port) => (0, devServerCommand_1.buildDevServerCommand)(framework, packageManager, devCommand, port),
                };
            }),
        switchPreviewPort: (port) => options.bridge.switchPreviewPort(port),
        setCommittedDevServerHealth: (port) => options.bridge.setCommittedDevServerHealth(port),
    });
};
exports.createAgentVariantsOrchestrator = createAgentVariantsOrchestrator;
//# sourceMappingURL=createAgentVariantsOrchestrator.js.map