import { serializeRouteForRust } from './utils/rust-config.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, needsTsHandling),
        };
        // Ensure we have a name for handler lookup
        if (!cleanRoute.name && !cleanRoute.id) {
            cleanRoute.name = routeKey;
        }
        return serializeRouteForRust(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, needsTsHandling) {
        let cleanAction = { ...action };
        if (needsTsHandling) {
            // Convert to socket-handler type for Rust (Rust will relay back to TS)
            const { socketHandler: _socketHandler, datagramHandler: _datagramHandler, ...serializableAction } = cleanAction;
            cleanAction = {
                ...serializableAction,
                type: 'socket-handler',
            };
        }
        // 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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicm91dGUtcHJlcHJvY2Vzc29yLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vdHMvcHJveGllcy9zbWFydC1wcm94eS9yb3V0ZS1wcmVwcm9jZXNzb3IudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsT0FBTyxFQUFFLHFCQUFxQixFQUFFLE1BQU0sd0JBQXdCLENBQUM7QUFFL0Q7Ozs7O0dBS0c7QUFDSCxNQUFNLE9BQU8saUJBQWlCO0lBQzVCOzs7T0FHRztJQUNLLGNBQWMsR0FBRyxJQUFJLEdBQUcsRUFBd0IsQ0FBQztJQUV6RDs7Ozs7Ozs7O09BU0c7SUFDSSxpQkFBaUIsQ0FBQyxNQUFzQjtRQUM3QyxJQUFJLENBQUMsY0FBYyxDQUFDLEtBQUssRUFBRSxDQUFDO1FBQzVCLE9BQU8sTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDLEtBQUssRUFBRSxLQUFLLEVBQUUsRUFBRSxDQUFDLElBQUksQ0FBQyxlQUFlLENBQUMsS0FBSyxFQUFFLEtBQUssQ0FBQyxDQUFDLENBQUM7SUFDMUUsQ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