/**
 * HTTPS Route Helper Functions
 *
 * This module provides utility functions for creating HTTPS route configurations
 * including TLS termination and passthrough routes.
 */
import { SocketHandlers } from './socket-handlers.js';
/**
 * Create an HTTPS route with TLS termination
 * @param domains Domain(s) to match
 * @param target Target host and port
 * @param options Additional route options
 * @returns Route configuration object
 */
export function createHttpsTerminateRoute(domains, target, options = {}) {
    // Create route match
    const match = {
        ports: options.httpsPort || 443,
        domains
    };
    // Create route action
    const action = {
        type: 'forward',
        targets: [target],
        tls: {
            mode: options.reencrypt ? 'terminate-and-reencrypt' : 'terminate',
            certificate: options.certificate || 'auto'
        }
    };
    // Create the route config
    return {
        match,
        action,
        name: options.name || `HTTPS Route for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
        ...options
    };
}
/**
 * Create an HTTP to HTTPS redirect route
 * @param domains Domain(s) to match
 * @param httpsPort HTTPS port to redirect to (default: 443)
 * @param options Additional route options
 * @returns Route configuration object
 */
export function createHttpToHttpsRedirect(domains, httpsPort = 443, options = {}) {
    // Create route match
    const match = {
        ports: options.match?.ports || 80,
        domains
    };
    // Create route action
    const action = {
        type: 'socket-handler',
        socketHandler: SocketHandlers.httpRedirect(`https://{domain}:${httpsPort}{path}`, 301)
    };
    // Create the route config
    return {
        match,
        action,
        name: options.name || `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
        ...options
    };
}
/**
 * Create an HTTPS passthrough route (SNI-based forwarding without TLS termination)
 * @param domains Domain(s) to match
 * @param target Target host and port
 * @param options Additional route options
 * @returns Route configuration object
 */
export function createHttpsPassthroughRoute(domains, target, options = {}) {
    // Create route match
    const match = {
        ports: options.match?.ports || 443,
        domains
    };
    // Create route action
    const action = {
        type: 'forward',
        targets: [target],
        tls: {
            mode: 'passthrough'
        }
    };
    // Create the route config
    return {
        match,
        action,
        name: options.name || `HTTPS Passthrough for ${Array.isArray(domains) ? domains.join(', ') : domains}`,
        ...options
    };
}
/**
 * Create a complete HTTPS server with HTTP to HTTPS redirects
 * @param domains Domain(s) to match
 * @param target Target host and port
 * @param options Additional configuration options
 * @returns Array of two route configurations (HTTPS and HTTP redirect)
 */
export function createCompleteHttpsServer(domains, target, options = {}) {
    // Create the HTTPS route
    const httpsRoute = createHttpsTerminateRoute(domains, target, options);
    // Create the HTTP redirect route
    const httpRedirectRoute = createHttpToHttpsRedirect(domains, 
    // Extract the HTTPS port from the HTTPS route - ensure it's a number
    typeof options.httpsPort === 'number' ? options.httpsPort :
        Array.isArray(options.httpsPort) ? options.httpsPort[0] : 443, {
        // Set the HTTP port
        match: {
            ports: options.httpPort || 80,
            domains
        },
        name: `HTTP to HTTPS Redirect for ${Array.isArray(domains) ? domains.join(', ') : domains}`
    });
    return [httpsRoute, httpRedirectRoute];
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHR0cHMtaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uL3RzL3Byb3hpZXMvc21hcnQtcHJveHkvdXRpbHMvcm91dGUtaGVscGVycy9odHRwcy1oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7OztHQUtHO0FBR0gsT0FBTyxFQUFFLGNBQWMsRUFBRSxNQUFNLHNCQUFzQixDQUFDO0FBRXREOzs7Ozs7R0FNRztBQUNILE1BQU0sVUFBVSx5QkFBeUIsQ0FDdkMsT0FBMEIsRUFDMUIsTUFBaUQsRUFDakQsVUFPSSxFQUFFO0lBRU4scUJBQXFCO0lBQ3JCLE1BQU0sS0FBSyxHQUFnQjtRQUN6QixLQUFLLEVBQUUsT0FBTyxDQUFDLFNBQVMsSUFBSSxHQUFHO1FBQy9CLE9BQU87S0FDUixDQUFDO0lBRUYsc0JBQXNCO0lBQ3RCLE1BQU0sTUFBTSxHQUFpQjtRQUMzQixJQUFJLEVBQUUsU0FBUztRQUNmLE9BQU8sRUFBRSxDQUFDLE1BQU0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