import { valita_exports } from "../../shared/src/valita.js";
import { must } from "../../shared/src/must.js";
import "../../shared/src/dotenv.js";
import { parseOptions } from "../../shared/src/options.js";
import { getShardID, upstreamSchema } from "../../zero-cache/src/types/shards.js";
import { ZERO_ENV_VAR_PREFIX, appOptions, shardOptions } from "../../zero-cache/src/config/zero-config.js";
import { pgClient } from "../../zero-cache/src/types/pg.js";
import { loadSchemaAndPermissions } from "../../zero-cache/src/scripts/permissions.js";
import { astToZQL } from "../../ast-to-zql/src/ast-to-zql.js";
import { formatOutput } from "../../ast-to-zql/src/format.js";
import { transformAndHashQuery } from "../../zero-cache/src/auth/read-authorizer.js";
import { LogContext, consoleLogSink } from "@rocicorp/logger";
//#region ../analyze-query/src/bin-transform.ts
var config = parseOptions({
	cvr: { db: valita_exports.string() },
	schema: {
		type: valita_exports.string().default("./schema.ts"),
		desc: ["Path to the schema file."]
	},
	app: appOptions,
	shard: shardOptions,
	hash: {
		type: valita_exports.string().optional(),
		desc: ["Hash of the query to fetch the AST for."]
	}
}, { envNamePrefix: ZERO_ENV_VAR_PREFIX });
var lc = new LogContext("debug", {}, consoleLogSink);
var { permissions } = await loadSchemaAndPermissions(config.schema);
var cvrDB = pgClient(lc, config.cvr.db);
var rows = await cvrDB`select "clientAST", "internal" from ${cvrDB(upstreamSchema(getShardID(config)) + "/cvr")}."queries" where "queryHash" = ${must(config.hash)} limit 1;`;
var queryAst = transformAndHashQuery(lc, "", rows[0].clientAST, permissions, {
	type: "jwt",
	raw: "",
	decoded: {}
}, rows[0].internal).transformedAst;
console.log("\n=== AST ===\n");
console.log(JSON.stringify(queryAst, null, 2));
console.log("\n=== ZQL ===\n");
console.log(await formatOutput(queryAst.table + astToZQL(queryAst)));
await cvrDB.end();
//#endregion
export {};

//# sourceMappingURL=bin-transform.js.map