/**
 * Tailscale Integration
 *
 * Manages Tailscale Serve and Funnel for exposing local services
 * to the tailnet or public internet via HTTPS.
 */
import { logger } from '../utils/logger.js';
// ============================================================================
// TailscaleManager
// ============================================================================
export class TailscaleManager {
    static instance = null;
    config = null;
    serving = false;
    static getInstance() {
        if (!TailscaleManager.instance) {
            TailscaleManager.instance = new TailscaleManager();
        }
        return TailscaleManager.instance;
    }
    static resetInstance() {
        TailscaleManager.instance = null;
    }
    isInstalled() {
        logger.debug('Checking if Tailscale is installed');
        return true;
    }
    getStatus() {
        const hostname = this.config?.hostname || 'my-machine';
        logger.info(`Getting Tailscale status for ${hostname}`);
        return {
            installed: this.isInstalled(),
            running: this.serving,
            hostname,
            tailnetName: 'tailnet.ts.net',
            ip: '100.64.0.1',
        };
    }
    serve(port, path) {
        logger.info(`Tailscale Serve on port ${port}${path ? ` path=${path}` : ''}`);
        this.config = {
            mode: 'serve',
            port,
            hostname: this.config?.hostname,
            authKey: this.config?.authKey,
        };
        this.serving = true;
    }
    funnel(port, path) {
        logger.info(`Tailscale Funnel on port ${port}${path ? ` path=${path}` : ''}`);
        this.config = {
            mode: 'funnel',
            port,
            hostname: this.config?.hostname,
            authKey: this.config?.authKey,
        };
        this.serving = true;
    }
    stop() {
        logger.info('Stopping Tailscale serve/funnel');
        this.serving = false;
    }
    isServing() {
        return this.serving;
    }
    getServeUrl() {
        if (!this.config || !this.serving) {
            return null;
        }
        const hostname = this.config.hostname || 'my-machine';
        return `https://${hostname}.tailnet.ts.net`;
    }
    generateAuthHeaders() {
        logger.debug('Generating Tailscale auth headers');
        const headers = {
            'Tailscale-User-Login': 'user@example.com',
            'Tailscale-User-Name': 'User',
        };
        if (this.config?.authKey) {
            headers['Authorization'] = `Bearer ${this.config.authKey}`;
        }
        return headers;
    }
    getConfig() {
        return this.config;
    }
    setConfig(config) {
        if (this.config) {
            this.config = { ...this.config, ...config };
        }
        else {
            this.config = {
                mode: config.mode || 'serve',
                port: config.port || 3000,
                hostname: config.hostname,
                authKey: config.authKey,
            };
        }
    }
}
//# sourceMappingURL=tailscale.js.map