import {} from "../../../zql/src/query/query-impl.js";
import * as v from "../../../shared/src/valita.js";
import { transformRequestMessageSchema, } from "../../../zero-protocol/src/custom-queries.js";
import { clientToServer } from "../../../zero-schema/src/name-mapper.js";
import { mapAST } from "../../../zero-protocol/src/ast.js";
/**
 * Invokes the callback `cb` for each query in the request or JSON body.
 * The callback should return a Query or Promise<Query> that is the transformed result.
 *
 * This function will call `cb` in parallel for each query found in the request.
 *
 * If you need to limit concurrency, you can use a library like `p-limit` to wrap the `cb` function.
 */
export async function handleGetQueriesRequest(cb, schema, requestOrJsonBody) {
    const nameMapper = clientToServer(schema.tables);
    let body;
    if (requestOrJsonBody instanceof Request) {
        body = await requestOrJsonBody.json();
    }
    else {
        body = requestOrJsonBody;
    }
    const parsed = v.parse(body, transformRequestMessageSchema);
    const responses = await Promise.all(parsed[1].map(async (req) => {
        const { query } = await cb(req.name, req.args);
        return {
            id: req.id,
            name: req.name,
            ast: mapAST(query.ast, nameMapper),
        };
    }));
    return ['transformed', responses];
}
//# sourceMappingURL=process-queries.js.map