/**
 * Browser Automation Stub Tool
 *
 * Provides browser automation capabilities as stubs for future
 * Playwright/Puppeteer integration. Tracks state internally
 * for testing and simulation purposes.
 */
import { logger } from '../utils/logger.js';
// ============================================================================
// BrowserStubTool
// ============================================================================
export class BrowserStubTool {
    static instance = null;
    launched = false;
    config = {};
    currentUrl = '';
    actions = [];
    tabs = [];
    activeTabId = '';
    consoleMessages = [];
    tabCounter = 0;
    constructor() { }
    static getInstance() {
        if (!BrowserStubTool.instance) {
            BrowserStubTool.instance = new BrowserStubTool();
        }
        return BrowserStubTool.instance;
    }
    static resetInstance() {
        if (BrowserStubTool.instance) {
            BrowserStubTool.instance.close();
        }
        BrowserStubTool.instance = null;
    }
    launch(config) {
        if (this.launched) {
            logger.debug('Browser already launched');
            return;
        }
        this.config = config || {};
        this.launched = true;
        this.actions = [];
        this.consoleMessages = [];
        this.tabs = [];
        this.tabCounter = 0;
        // Create initial tab
        const tabId = this.generateTabId();
        this.tabs.push({ id: tabId, url: 'about:blank', active: true });
        this.activeTabId = tabId;
        logger.debug('Browser launched', { config: this.config });
    }
    navigate(url) {
        this.ensureLaunched();
        this.currentUrl = url;
        const activeTab = this.tabs.find(t => t.id === this.activeTabId);
        if (activeTab) {
            activeTab.url = url;
        }
        this.recordAction('navigate', undefined, url);
        logger.debug('Navigated to', { url });
    }
    click(selector) {
        this.ensureLaunched();
        this.recordAction('click', selector);
    }
    type(selector, text) {
        this.ensureLaunched();
        this.recordAction('type', selector, text);
    }
    press(key) {
        this.ensureLaunched();
        this.recordAction('press', undefined, key);
    }
    hover(selector) {
        this.ensureLaunched();
        this.recordAction('hover', selector);
    }
    drag(from, to) {
        this.ensureLaunched();
        this.recordAction('drag', from, to);
    }
    screenshot(path) {
        this.ensureLaunched();
        const screenshotPath = path || `/tmp/screenshot-${Date.now()}.png`;
        this.recordAction('screenshot', undefined, screenshotPath);
        return screenshotPath;
    }
    pdf(path) {
        this.ensureLaunched();
        const pdfPath = path || `/tmp/page-${Date.now()}.pdf`;
        this.recordAction('pdf', undefined, pdfPath);
        return pdfPath;
    }
    getConsole() {
        this.ensureLaunched();
        return [...this.consoleMessages];
    }
    getTabs() {
        this.ensureLaunched();
        return this.tabs.map(t => ({ ...t }));
    }
    newTab(url) {
        this.ensureLaunched();
        const tabId = this.generateTabId();
        const tabUrl = url || 'about:blank';
        // Deactivate current tab
        for (const tab of this.tabs) {
            tab.active = false;
        }
        const newTab = { id: tabId, url: tabUrl, active: true };
        this.tabs.push(newTab);
        this.activeTabId = tabId;
        this.currentUrl = tabUrl;
        this.recordAction('newTab', undefined, tabUrl);
        return { ...newTab };
    }
    closeTab(tabId) {
        this.ensureLaunched();
        const idx = this.tabs.findIndex(t => t.id === tabId);
        if (idx === -1) {
            throw new Error(`Tab ${tabId} not found`);
        }
        this.tabs.splice(idx, 1);
        if (this.activeTabId === tabId && this.tabs.length > 0) {
            this.tabs[0].active = true;
            this.activeTabId = this.tabs[0].id;
            this.currentUrl = this.tabs[0].url;
        }
        this.recordAction('closeTab', undefined, tabId);
    }
    switchTab(tabId) {
        this.ensureLaunched();
        const tab = this.tabs.find(t => t.id === tabId);
        if (!tab) {
            throw new Error(`Tab ${tabId} not found`);
        }
        for (const t of this.tabs) {
            t.active = false;
        }
        tab.active = true;
        this.activeTabId = tabId;
        this.currentUrl = tab.url;
        this.recordAction('switchTab', undefined, tabId);
    }
    evaluate(script) {
        this.ensureLaunched();
        this.recordAction('evaluate', undefined, script);
        return `[eval result: ${script}]`;
    }
    getActions() {
        return [...this.actions];
    }
    close() {
        this.launched = false;
        this.currentUrl = '';
        this.actions = [];
        this.tabs = [];
        this.activeTabId = '';
        this.consoleMessages = [];
        this.tabCounter = 0;
        logger.debug('Browser closed');
    }
    isLaunched() {
        return this.launched;
    }
    ensureLaunched() {
        if (!this.launched) {
            throw new Error('Browser not launched. Call launch() first.');
        }
    }
    recordAction(type, selector, value) {
        this.actions.push({
            type,
            selector,
            value,
            timestamp: Date.now(),
        });
    }
    generateTabId() {
        this.tabCounter++;
        return `tab-${this.tabCounter}`;
    }
}
//# sourceMappingURL=browser-stub.js.map