/**
 * Search parameter schema types and runtime utilities.
 *
 * Provides a lightweight schema system for typed query parameters.
 * When a route defines a `search` schema, ctx.searchParams becomes
 * a typed object with parsed values instead of raw URLSearchParams.
 */
// ============================================================================
// Runtime Parser
// ============================================================================
/**
 * Parse URLSearchParams into a typed object using the given schema.
 *
 * - `"string"` / `"string?"` - kept as-is
 * - `"number"` / `"number?"` - coerced via `Number()`; NaN treated as missing
 * - `"boolean"` / `"boolean?"` - `"true"` / `"1"` -> true, `"false"` / `"0"` / `""` -> false
 *
 * Missing required params are set to their zero value (empty string / 0 / false).
 */
export function parseSearchParams(searchParams, schema) {
    const result = {};
    for (const [key, descriptor] of Object.entries(schema)) {
        const isOptional = descriptor.endsWith("?");
        const baseType = isOptional ? descriptor.slice(0, -1) : descriptor;
        const raw = searchParams.get(key);
        if (raw === null) {
            if (!isOptional) {
                // Required param missing: use zero value
                if (baseType === "string")
                    result[key] = "";
                else if (baseType === "number")
                    result[key] = 0;
                else if (baseType === "boolean")
                    result[key] = false;
            }
            // Optional params are omitted (undefined)
            continue;
        }
        if (baseType === "string") {
            result[key] = raw;
        }
        else if (baseType === "number") {
            const num = Number(raw);
            if (Number.isNaN(num)) {
                if (!isOptional)
                    result[key] = 0;
            }
            else {
                result[key] = num;
            }
        }
        else if (baseType === "boolean") {
            result[key] = raw === "true" || raw === "1";
        }
    }
    return result;
}
// ============================================================================
// Runtime Serializer
// ============================================================================
/**
 * Serialize a typed search params object to a query string (without leading `?`).
 * Skips `undefined` and `null` values.
 */
export function serializeSearchParams(params) {
    const parts = [];
    for (const [key, value] of Object.entries(params)) {
        if (value === undefined || value === null)
            continue;
        parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
    }
    return parts.join("&");
}
//# sourceMappingURL=search-params.js.map