import { logger } from '../../core/utils/logger.js';
/**
 * Preprocesses routes before sending them to Rust.
 *
 * Strips non-serializable fields (functions, callbacks) and classifies
 * routes that must be handled by TypeScript (socket-handler, dynamic host/port).
 */
export class RoutePreprocessor {
    /**
     * Map of route name/id → original route config (with JS functions preserved).
     * Used by the socket handler server to look up the original handler.
     */
    originalRoutes = new Map();
    /**
     * Preprocess routes for the Rust binary.
     *
     * - Routes with `socketHandler` callbacks are marked as socket-handler type
     *   (Rust will relay these back to TS)
     * - Routes with dynamic `host`/`port` functions are converted to socket-handler
     *   type (Rust relays, TS resolves the function)
     * - Non-serializable fields are stripped
     * - Original routes are preserved in the local map for handler lookup
     */
    preprocessForRust(routes) {
        this.originalRoutes.clear();
        return routes.map((route, index) => this.preprocessRoute(route, index));
    }
    /**
     * Get the original route config (with JS functions) by route name or id.
     */
    getOriginalRoute(routeKey) {
        return this.originalRoutes.get(routeKey);
    }
    /**
     * Get all original routes that have socket handlers or dynamic functions.
     */
    getHandlerRoutes() {
        return new Map(this.originalRoutes);
    }
    preprocessRoute(route, index) {
        const routeKey = route.name || route.id || `route_${index}`;
        // Check if this route needs TS-side handling
        const needsTsHandling = this.routeNeedsTsHandling(route);
        if (needsTsHandling) {
            // Store the original route for handler lookup
            this.originalRoutes.set(routeKey, route);
        }
        // Create a clean copy for Rust
        const cleanRoute = {
            ...route,
            action: this.cleanAction(route.action, routeKey, needsTsHandling),
        };
        // Ensure we have a name for handler lookup
        if (!cleanRoute.name && !cleanRoute.id) {
            cleanRoute.name = routeKey;
        }
        return cleanRoute;
    }
    routeNeedsTsHandling(route) {
        // Socket handler routes always need TS
        if (route.action.type === 'socket-handler' && route.action.socketHandler) {
            return true;
        }
        // Datagram handler routes always need TS
        if (route.action.type === 'socket-handler' && route.action.datagramHandler) {
            return true;
        }
        // Routes with dynamic host/port functions need TS
        if (route.action.targets) {
            for (const target of route.action.targets) {
                if (typeof target.host === 'function' || typeof target.port === 'function') {
                    return true;
                }
            }
        }
        return false;
    }
    cleanAction(action, routeKey, needsTsHandling) {
        const cleanAction = { ...action };
        if (needsTsHandling) {
            // Convert to socket-handler type for Rust (Rust will relay back to TS)
            cleanAction.type = 'socket-handler';
            // Remove the JS handlers (not serializable)
            delete cleanAction.socketHandler;
            delete cleanAction.datagramHandler;
        }
        // Clean targets - replace functions with static values
        if (cleanAction.targets) {
            cleanAction.targets = cleanAction.targets.map(t => this.cleanTarget(t));
        }
        return cleanAction;
    }
    cleanTarget(target) {
        const clean = { ...target };
        // Replace function host with placeholder
        if (typeof clean.host === 'function') {
            clean.host = 'localhost';
        }
        // Replace function port with placeholder
        if (typeof clean.port === 'function') {
            clean.port = 0;
        }
        return clean;
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicm91dGUtcHJlcHJvY2Vzc29yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vdHMvcHJveGllcy9zbWFydC1wcm94eS9yb3V0ZS1wcmVwcm9jZXNzb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLDRCQUE0QixDQUFDO0FBRXBEOzs7OztHQUtHO0FBQ0gsTUFBTSxPQUFPLGlCQUFpQjtJQUM1Qjs7O09BR0c7SUFDSyxjQUFjLEdBQUcsSUFBSSxHQUFHLEVBQXdCLENBQUM7SUFFekQ7Ozs7Ozs7OztPQVNHO0lBQ0ksaUJBQWlCLENBQUMsTUFBc0I7UUFDN0MsSUFBSSxDQUFDLGNBQWMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztRQUM1QixPQUFPLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsS0FBSyxFQUFFLEVBQUUsQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLEtBQUssRUFBRSxLQUFLLENBQUMsQ0FBQyxDQUFDO0lBQzFFLENBQUM7SUFFRDs7T0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