"use strict";
/**
 * DaemonManager Factory
 * Issue #136: Phase 3 - Task 3.1 - DaemonManager abstraction
 *
 * Provides Factory pattern for creating DaemonManager instances with
 * issue-specific configurations while maintaining backward compatibility
 * with existing DaemonManager(pidFilePath: string) constructor.
 *
 * MF-CONS-002: Backward compatibility with existing constructor maintained
 * SF-002: Factory pattern for DaemonManager creation
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DaemonManagerFactory = exports.DaemonManagerWrapper = void 0;
exports.getDaemonManagerFactory = getDaemonManagerFactory;
exports.resetDaemonManagerFactory = resetDaemonManagerFactory;
const daemon_1 = require("./daemon");
const resource_resolvers_1 = require("./resource-resolvers");
const env_setup_1 = require("./env-setup");
/**
 * Wrapper around existing DaemonManager to implement IDaemonManager
 * MF-CONS-002: Uses existing DaemonManager(pidFilePath: string) internally
 */
class DaemonManagerWrapper {
    innerManager;
    config;
    constructor(innerManager, config) {
        this.innerManager = innerManager;
        this.config = config;
    }
    async start(options) {
        // Set environment variables for the daemon process
        // This allows the daemon to use the correct DB path for worktree
        if (this.config.issueNo !== undefined) {
            process.env.CM_DB_PATH = this.config.dbPath;
            process.env.CM_ISSUE_NO = String(this.config.issueNo);
        }
        return this.innerManager.start(options);
    }
    async stop(force = false) {
        return this.innerManager.stop(force);
    }
    async getStatus() {
        return this.innerManager.getStatus();
    }
    async isRunning() {
        return this.innerManager.isRunning();
    }
    /**
     * Get configuration (for testing/debugging)
     */
    getConfig() {
        return { ...this.config };
    }
}
exports.DaemonManagerWrapper = DaemonManagerWrapper;
/**
 * Factory for creating DaemonManager instances
 * SF-002: Factory pattern implementation
 *
 * @example
 * ```typescript
 * // Create manager for main server
 * const factory = new DaemonManagerFactory();
 * const mainManager = factory.create();
 *
 * // Create manager for worktree #135
 * const worktreeManager = factory.create(135);
 * ```
 */
class DaemonManagerFactory {
    pidResolver;
    dbResolver;
    constructor() {
        this.pidResolver = new resource_resolvers_1.PidPathResolver();
        this.dbResolver = new resource_resolvers_1.DbPathResolver();
    }
    /**
     * Create a DaemonManager instance
     * @param issueNo - Optional issue number for worktree-specific manager
     * @returns IDaemonManager instance
     */
    create(issueNo) {
        const pidPath = this.pidResolver.resolve(issueNo);
        const envPath = (0, env_setup_1.getEnvPath)(issueNo);
        const dbPath = this.dbResolver.resolve(issueNo);
        // MF-CONS-002: Use existing DaemonManager constructor
        const innerManager = new daemon_1.DaemonManager(pidPath);
        // Wrap with additional configuration
        return new DaemonManagerWrapper(innerManager, {
            pidPath,
            envPath,
            dbPath,
            issueNo,
        });
    }
}
exports.DaemonManagerFactory = DaemonManagerFactory;
/**
 * Singleton factory instance for convenience
 */
let _factoryInstance = null;
/**
 * Get singleton factory instance
 */
function getDaemonManagerFactory() {
    if (!_factoryInstance) {
        _factoryInstance = new DaemonManagerFactory();
    }
    return _factoryInstance;
}
/**
 * Reset singleton factory (for testing)
 */
function resetDaemonManagerFactory() {
    _factoryInstance = null;
}
