"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stopPlaywrightServer = exports.buildHandle = exports.ensureBrowser = exports.allocatePageId = exports.pages = void 0;
exports.createPlaywrightServer = createPlaywrightServer;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const logger_1 = require("../../../lib/logger");
const tools_atomic_1 = require("./tools-atomic");
const tools_run_script_1 = require("./tools-run-script");
// Re-export public API consumed by playwright-cli.ts and system/index.ts
var browser_lifecycle_1 = require("./browser-lifecycle");
Object.defineProperty(exports, "pages", { enumerable: true, get: function () { return browser_lifecycle_1.pages; } });
Object.defineProperty(exports, "allocatePageId", { enumerable: true, get: function () { return browser_lifecycle_1.allocatePageId; } });
Object.defineProperty(exports, "ensureBrowser", { enumerable: true, get: function () { return browser_lifecycle_1.ensureBrowser; } });
Object.defineProperty(exports, "buildHandle", { enumerable: true, get: function () { return browser_lifecycle_1.buildHandle; } });
Object.defineProperty(exports, "stopPlaywrightServer", { enumerable: true, get: function () { return browser_lifecycle_1.stopPlaywrightServer; } });
/**
 * Create a Playwright Browser MCP server.
 *
 * Provides 9 tools for browser automation:
 *   navigate, screenshot, click, fill, wait_for, evaluate, run_script, list_pages, close_page
 *
 * All page-scoped tools participate in the _handle convention:
 * - navigate returns a _handle in its result
 * - All other tools accept _handle to locate the page across activities
 * - Fast path: local pages Map (same container)
 * - Slow path: CDP reconnection (different container)
 *
 * `run_script` is the preferred tool for YAML workflows — it executes a
 * multi-step browser script in a single activity, avoiding cross-activity
 * session issues entirely.
 *
 * Returns a fresh McpServer instance each time. The browser is shared
 * and lazy-launched on first tool call.
 */
async function createPlaywrightServer(options) {
    const name = options?.name || 'long-tail-playwright';
    const instance = new mcp_js_1.McpServer({ name, version: '1.0.0' });
    (0, tools_atomic_1.registerAtomicTools)(instance);
    (0, tools_run_script_1.registerRunScript)(instance);
    logger_1.loggerRegistry.info(`[lt-mcp:playwright] ${name} ready (9 tools registered)`);
    return instance;
}
