// Hatchway CLI - Built with Rollup
import Conf from 'conf';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { existsSync } from 'node:fs';

/**
 * Config manager using conf library
 * Handles platform-specific config locations
 */
class ConfigManager {
    constructor() {
        this.conf = new Conf({
            projectName: 'hatchway',
            projectSuffix: '', // No suffix, just 'hatchway'
            defaults: this.getDefaults(),
        });
    }
    getDefaults() {
        return {
            version: '0.1.0',
            workspace: join(homedir(), 'hatchway-workspace'),
            apiUrl: 'http://localhost:3000', // Default API URL
            server: {
                wsUrl: 'ws://localhost:3000/ws/runner', // Direct WebSocket connection to Next.js
                secret: 'dev-secret', // Default local secret
            },
            runner: {
                id: 'local',
                reconnectAttempts: 5,
                heartbeatInterval: 15000,
            },
            tunnel: {
                provider: 'cloudflare',
                autoCreate: true,
            },
        };
    }
    get(key) {
        if (!key) {
            return this.conf.store;
        }
        return this.conf.get(key);
    }
    set(key, value) {
        this.conf.set(key, value);
    }
    delete(key) {
        this.conf.delete(key);
    }
    reset() {
        this.conf.clear();
        this.conf.store = this.getDefaults();
    }
    has(key) {
        return this.conf.has(key);
    }
    get path() {
        return this.conf.path;
    }
    validate() {
        const errors = [];
        const config = this.conf.store;
        if (!config.workspace) {
            errors.push('Workspace path is required');
        }
        // Check for server config (new) or legacy broker config
        const hasServerConfig = config.server?.secret && config.server?.wsUrl;
        const hasLegacyBrokerConfig = config.broker?.secret && config.broker?.url;
        if (!hasServerConfig && !hasLegacyBrokerConfig) {
            errors.push('Server shared secret is required');
            errors.push('Server WebSocket URL is required');
        }
        if (!config.runner?.id) {
            errors.push('Runner ID is required');
        }
        // Check if workspace exists
        if (config.workspace && !existsSync(config.workspace)) {
            errors.push(`Workspace directory does not exist: ${config.workspace}`);
        }
        return {
            valid: errors.length === 0,
            errors,
        };
    }
    /**
     * Check if config has been initialized properly
     * Requires monorepo to be cloned and databaseUrl to be configured
     */
    isInitialized() {
        const monorepoPath = this.get('monorepoPath');
        const databaseUrl = this.get('databaseUrl');
        // Must have monorepo path set and the directory must exist
        if (!monorepoPath || !existsSync(monorepoPath)) {
            return false;
        }
        // Must have database URL configured
        if (!databaseUrl) {
            return false;
        }
        return true;
    }
    /**
     * Get the WebSocket URL (supports both new and legacy config)
     */
    getWsUrl() {
        const server = this.get('server');
        const broker = this.get('broker');
        // Prefer new server config
        if (server?.wsUrl) {
            return server.wsUrl;
        }
        // Fall back to legacy broker URL
        return broker?.url ?? 'ws://localhost:3000/ws/runner';
    }
    /**
     * Get the shared secret (supports both new and legacy config)
     */
    getSecret() {
        const server = this.get('server');
        const broker = this.get('broker');
        // Prefer new server config
        if (server?.secret) {
            return server.secret;
        }
        // Fall back to legacy broker secret
        return broker?.secret ?? '';
    }
}
const configManager = new ConfigManager();

export { configManager as c };
//# sourceMappingURL=config-manager-DST6RbP8.js.map
