"use strict";
/**
 * Resource Path Resolvers
 * Issue #136: Phase 1 - Task 1.2
 *
 * Provides Strategy pattern implementation for resolving resource paths:
 * - Database paths (cm.db, cm-{issueNo}.db)
 * - PID file paths (.commandmate.pid, pids/{issueNo}.pid)
 * - Log file paths (commandmate.log, commandmate-{issueNo}.log)
 *
 * SF-SEC-001: TOCTOU protection via try-catch pattern in validate()
 *
 * @module resource-resolvers
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogPathResolver = exports.PidPathResolver = exports.DbPathResolver = void 0;
exports.createDbPathResolver = createDbPathResolver;
exports.createPidPathResolver = createPidPathResolver;
exports.createLogPathResolver = createLogPathResolver;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const install_context_1 = require("./install-context");
/**
 * Database path resolver
 *
 * @example
 * ```typescript
 * const resolver = new DbPathResolver();
 * resolver.resolve(); // ~/.commandmate/data/cm.db
 * resolver.resolve(135); // ~/.commandmate/data/cm-135.db
 * ```
 */
class DbPathResolver {
    resolve(issueNo) {
        const configDir = (0, install_context_1.getConfigDir)();
        if (issueNo !== undefined) {
            return path_1.default.join(configDir, 'data', `cm-${issueNo}.db`);
        }
        return path_1.default.join(configDir, 'data', 'cm.db');
    }
    validate(targetPath) {
        const configDir = (0, install_context_1.getConfigDir)();
        // SF-SEC-001: try-catch for TOCTOU protection
        try {
            const resolved = (0, fs_1.realpathSync)(targetPath);
            return resolved.startsWith(configDir);
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                // New file - validate parent directory
                const parentDir = path_1.default.dirname(targetPath);
                try {
                    const resolvedParent = (0, fs_1.realpathSync)(parentDir);
                    return resolvedParent.startsWith(configDir);
                }
                catch {
                    return false;
                }
            }
            return false;
        }
    }
}
exports.DbPathResolver = DbPathResolver;
/**
 * PID file path resolver
 *
 * Maintains backward compatibility:
 * - No issueNo: ~/.commandmate/.commandmate.pid (main server)
 * - With issueNo: ~/.commandmate/pids/{issueNo}.pid (worktree server)
 *
 * @example
 * ```typescript
 * const resolver = new PidPathResolver();
 * resolver.resolve(); // ~/.commandmate/.commandmate.pid
 * resolver.resolve(135); // ~/.commandmate/pids/135.pid
 * ```
 */
class PidPathResolver {
    resolve(issueNo) {
        const configDir = (0, install_context_1.getConfigDir)();
        if (issueNo !== undefined) {
            return path_1.default.join(configDir, 'pids', `${issueNo}.pid`);
        }
        // Backward compatibility: main PID file in config root
        return path_1.default.join(configDir, '.commandmate.pid');
    }
    validate(targetPath) {
        const configDir = (0, install_context_1.getConfigDir)();
        // SF-SEC-001: try-catch for TOCTOU protection
        try {
            const resolved = (0, fs_1.realpathSync)(targetPath);
            return resolved.startsWith(configDir);
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                // New file - validate parent directory
                const parentDir = path_1.default.dirname(targetPath);
                try {
                    const resolvedParent = (0, fs_1.realpathSync)(parentDir);
                    return resolvedParent.startsWith(configDir);
                }
                catch {
                    return false;
                }
            }
            return false;
        }
    }
}
exports.PidPathResolver = PidPathResolver;
/**
 * Log file path resolver
 *
 * @example
 * ```typescript
 * const resolver = new LogPathResolver();
 * resolver.resolve(); // ~/.commandmate/logs/commandmate.log
 * resolver.resolve(135); // ~/.commandmate/logs/commandmate-135.log
 * ```
 */
class LogPathResolver {
    resolve(issueNo) {
        const configDir = (0, install_context_1.getConfigDir)();
        const logsDir = path_1.default.join(configDir, 'logs');
        if (issueNo !== undefined) {
            return path_1.default.join(logsDir, `commandmate-${issueNo}.log`);
        }
        return path_1.default.join(logsDir, 'commandmate.log');
    }
    validate(targetPath) {
        const configDir = (0, install_context_1.getConfigDir)();
        const logsDir = path_1.default.join(configDir, 'logs');
        // SF-SEC-001: try-catch for TOCTOU protection
        try {
            const resolved = (0, fs_1.realpathSync)(targetPath);
            return resolved.startsWith(logsDir);
        }
        catch (error) {
            if (error.code === 'ENOENT') {
                // New file - validate logs directory exists within config
                try {
                    const resolvedLogsDir = (0, fs_1.realpathSync)(logsDir);
                    return resolvedLogsDir.startsWith(configDir);
                }
                catch {
                    return false;
                }
            }
            return false;
        }
    }
}
exports.LogPathResolver = LogPathResolver;
/**
 * Factory function for DbPathResolver
 */
function createDbPathResolver() {
    return new DbPathResolver();
}
/**
 * Factory function for PidPathResolver
 */
function createPidPathResolver() {
    return new PidPathResolver();
}
/**
 * Factory function for LogPathResolver
 */
function createLogPathResolver() {
    return new LogPathResolver();
}
