/**
 * Chrome Bridge
 *
 * Native Messaging API bridge for Chrome extension integration.
 * Provides stubs for browser interaction (console, DOM, network, recording).
 */
import { logger } from '../utils/logger.js';
// ============================================================================
// ChromeBridge (Singleton)
// ============================================================================
let instance = null;
export class ChromeBridge {
    config;
    connected = false;
    recording = false;
    recordedActions = [];
    consoleErrors = [];
    networkRequests = [];
    constructor(config) {
        this.config = {
            port: config?.port ?? 9222,
            extensionId: config?.extensionId,
        };
    }
    static getInstance(config) {
        if (!instance) {
            instance = new ChromeBridge(config);
        }
        return instance;
    }
    static resetInstance() {
        if (instance) {
            instance.connected = false;
            instance.recording = false;
            instance.recordedActions = [];
            instance.consoleErrors = [];
            instance.networkRequests = [];
        }
        instance = null;
    }
    /**
     * Establish connection to Chrome
     */
    async connect(port) {
        if (port !== undefined) {
            this.config.port = port;
        }
        this.connected = true;
        logger.debug(`Chrome bridge connected on port ${this.config.port}`);
        return true;
    }
    /**
     * Close connection
     */
    async disconnect() {
        this.connected = false;
        this.recording = false;
        logger.debug('Chrome bridge disconnected');
    }
    /**
     * Check connection state
     */
    isConnected() {
        return this.connected;
    }
    /**
     * Get captured console errors (stub)
     */
    async getConsoleErrors() {
        if (!this.connected) {
            throw new Error('Not connected to Chrome');
        }
        return [...this.consoleErrors];
    }
    /**
     * Get DOM element info (stub)
     */
    async getDOMState(selector) {
        if (!this.connected) {
            throw new Error('Not connected to Chrome');
        }
        // Stub: return a mock element
        return {
            tagName: 'div',
            id: selector.startsWith('#') ? selector.slice(1) : undefined,
            className: selector.startsWith('.') ? selector.slice(1) : undefined,
            textContent: '',
            attributes: {},
            children: 0,
        };
    }
    /**
     * Get captured network requests (stub)
     */
    async getNetworkRequests(filter) {
        if (!this.connected) {
            throw new Error('Not connected to Chrome');
        }
        if (filter) {
            return this.networkRequests.filter(r => r.url.includes(filter));
        }
        return [...this.networkRequests];
    }
    /**
     * Execute JavaScript in browser (stub)
     */
    async executeScript(script) {
        if (!this.connected) {
            throw new Error('Not connected to Chrome');
        }
        logger.debug(`Executing script: ${script.slice(0, 100)}`);
        return undefined;
    }
    /**
     * Start recording user actions
     */
    async startRecording() {
        if (!this.connected) {
            throw new Error('Not connected to Chrome');
        }
        this.recording = true;
        this.recordedActions = [];
        logger.debug('Started recording');
    }
    /**
     * Stop recording user actions
     */
    async stopRecording() {
        this.recording = false;
        logger.debug('Stopped recording');
    }
    /**
     * Get recorded actions
     */
    getRecording() {
        return [...this.recordedActions];
    }
    /**
     * Check if currently recording
     */
    isRecording() {
        return this.recording;
    }
    /**
     * Get config
     */
    getConfig() {
        return { ...this.config };
    }
}
//# sourceMappingURL=chrome-bridge.js.map