import { createServer } from 'node:http';
import { resolveManagedN8nWorkflowOpen } from '../n8n-local/workflow-open.js';
const DEFAULT_LOCAL_BRIDGE_HOST = '127.0.0.1';
const DEFAULT_LOCAL_BRIDGE_PORT = 3791;
let serverPromise;
let server;
export async function ensureLocalWorkflowOpenBridgeRunning() {
    if (serverPromise) {
        await serverPromise;
        return;
    }
    serverPromise = new Promise((resolve, reject) => {
        const nextServer = createServer((request, response) => {
            void handleRequest(request, response);
        });
        nextServer.once('error', (error) => {
            serverPromise = undefined;
            server = undefined;
            reject(error);
        });
        nextServer.listen(DEFAULT_LOCAL_BRIDGE_PORT, DEFAULT_LOCAL_BRIDGE_HOST, () => {
            server = nextServer;
            resolve();
        });
    });
    await serverPromise;
}
export function buildLocalWorkflowOpenBridgeUrl(targetUrl) {
    return `http://${DEFAULT_LOCAL_BRIDGE_HOST}:${DEFAULT_LOCAL_BRIDGE_PORT}/open/n8n-workflow?target=${encodeURIComponent(targetUrl)}`;
}
async function handleRequest(request, response) {
    const method = request.method ?? 'GET';
    const url = new URL(request.url ?? '/', `http://${DEFAULT_LOCAL_BRIDGE_HOST}:${DEFAULT_LOCAL_BRIDGE_PORT}`);
    if (method !== 'GET' || url.pathname !== '/open/n8n-workflow') {
        response.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });
        response.end('Not found');
        return;
    }
    const target = String(url.searchParams.get('target') ?? '').trim();
    const resolution = resolveManagedN8nWorkflowOpen(target);
    if (!resolution.ok) {
        response.writeHead(resolution.statusCode, { 'content-type': 'text/plain; charset=utf-8' });
        response.end(resolution.error);
        return;
    }
    if (resolution.payload.mode === 'direct') {
        response.writeHead(302, { Location: resolution.payload.targetUrl });
        response.end();
        return;
    }
    response.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
    response.end(resolution.payload.fallbackPage);
}
//# sourceMappingURL=local-open-bridge.js.map