/**
 * Network Route Interceptor
 *
 * Playwright page.route() wrapper for intercepting, mocking, blocking,
 * and modifying network requests. OpenClaw-inspired network control.
 */
import { logger } from '../utils/logger.js';
// ============================================================================
// Route Interceptor
// ============================================================================
export class RouteInterceptor {
    rules = new Map();
    handlers = new Map();
    /**
     * Add a route interception rule
     */
    async addRule(page, rule) {
        // Remove existing rule with same ID
        if (this.rules.has(rule.id)) {
            await this.removeRule(page, rule.id);
        }
        const handler = async (route) => {
            const url = route.request().url();
            switch (rule.action) {
                case 'block':
                    logger.debug(`Route blocked: ${url}`);
                    await route.abort('blockedbyclient');
                    break;
                case 'mock':
                    if (rule.mockResponse) {
                        logger.debug(`Route mocked: ${url}`);
                        await route.fulfill({
                            status: rule.mockResponse.status,
                            body: rule.mockResponse.body || '',
                            contentType: rule.mockResponse.contentType || 'text/plain',
                            headers: rule.mockResponse.headers,
                        });
                    }
                    else {
                        await route.continue();
                    }
                    break;
                case 'modify':
                    if (rule.modifyHeaders) {
                        const headers = {
                            ...route.request().headers(),
                            ...rule.modifyHeaders,
                        };
                        logger.debug(`Route headers modified: ${url}`);
                        await route.continue({ headers });
                    }
                    else {
                        await route.continue();
                    }
                    break;
                case 'log':
                    logger.info(`Route logged: ${route.request().method()} ${url}`);
                    await route.continue();
                    break;
                default:
                    await route.continue();
            }
        };
        await page.route(rule.urlPattern, handler);
        this.rules.set(rule.id, rule);
        this.handlers.set(rule.id, handler);
        logger.debug(`Route rule added: ${rule.id} (${rule.action} ${rule.urlPattern})`);
    }
    /**
     * Remove a route interception rule
     */
    async removeRule(page, ruleId) {
        const rule = this.rules.get(ruleId);
        const handler = this.handlers.get(ruleId);
        if (rule && handler) {
            await page.unroute(rule.urlPattern, handler);
            this.rules.delete(ruleId);
            this.handlers.delete(ruleId);
            logger.debug(`Route rule removed: ${ruleId}`);
        }
    }
    /**
     * List active rules
     */
    listRules() {
        return Array.from(this.rules.values());
    }
    /**
     * Clear all rules
     */
    async clearRules(page) {
        for (const [id, rule] of this.rules) {
            const handler = this.handlers.get(id);
            if (handler) {
                await page.unroute(rule.urlPattern, handler);
            }
        }
        this.rules.clear();
        this.handlers.clear();
        logger.debug('All route rules cleared');
    }
}
//# sourceMappingURL=route-interceptor.js.map