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 {
    constructor() {
        /**
         * Map of route name/id → original route config (with JS functions preserved).
         * Used by the socket handler server to look up the original handler.
         */
        this.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;
        }
        // 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 handler (not serializable)
            delete cleanAction.socketHandler;
        }
        // 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicm91dGUtcHJlcHJvY2Vzc29yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vdHMvcHJveGllcy9zbWFydC1wcm94eS9yb3V0ZS1wcmVwcm9jZXNzb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQ0EsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLDRCQUE0QixDQUFDO0FBRXBEOzs7OztHQUtHO0FBQ0gsTUFBTSxPQUFPLGlCQUFpQjtJQUE5QjtRQUNFOzs7V0FHRztRQUNLLG1CQUFjLEdBQUcsSUFBSSxHQUFHLEVBQXdCLENBQUM7SUEyRzNELENBQUM7SUF6R0M7Ozs7Ozs7OztPQVNHO0lBQ0ksaUJBQWlCLENBQUMsTUFBc0I7UUFDN0MsSUFBSSxDQUFDLGNBQWMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztRQUM1QixPQUFPLE1BQU0sQ0FBQyxHQUFHLENBQUMsQ0FBQyxLQUFLLEVBQUUsS0FBSyxFQUFFLEVBQUUsQ0FBQyxJQUFJLENBQUMsZUFBZSxDQUFDLEtBQUssRUFBRSxLQUFLLENBQUMsQ0FBQyxDQUFDO0lBQzFFLENBQUM7SUFFRDs7T0FFRztJQUNJLGdCQUFnQixDQUFDLFFBQWdCO1FBQ3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