import { jsonSchema } from "../../shared/src/json-schema.js";
import { tdigestSchema } from "../../shared/src/tdigest-schema.js";
import * as v from "../../shared/src/valita.js";
import { analyzeQueryResultSchema } from "./analyze-query-result.js";
import { astSchema } from "./ast.js";
const serverMetricsSchema = v.object({
    'query-materialization-server': tdigestSchema,
    'query-update-server': tdigestSchema,
});
const inspectQueryRowSchema = v.object({
    clientID: v.string(),
    queryID: v.string(),
    // This is the server return AST for custom queries
    // TODO: Return server generated AST
    ast: astSchema.nullable(),
    // not null for custom queries
    name: v.string().nullable(),
    // not null for custom queries
    args: v.readonlyArray(jsonSchema).nullable(),
    got: v.boolean(),
    deleted: v.boolean(),
    ttl: v.number(),
    inactivatedAt: v.number().nullable(),
    rowCount: v.number(),
    metrics: serverMetricsSchema.nullable().optional(),
});
const inspectBaseDownSchema = v.object({
    id: v.string(),
});
export const inspectQueriesDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('queries'),
    value: v.array(inspectQueryRowSchema),
});
export const inspectMetricsDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('metrics'),
    value: serverMetricsSchema,
});
export const inspectVersionDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('version'),
    value: v.string(),
});
export const inspectAuthenticatedDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('authenticated'),
    value: v.boolean(),
});
export const inspectAnalyzeQueryDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('analyze-query'),
    value: analyzeQueryResultSchema,
});
export const inspectErrorDownSchema = inspectBaseDownSchema.extend({
    op: v.literal('error'),
    value: v.string(),
});
export const inspectDownBodySchema = v.union(inspectQueriesDownSchema, inspectMetricsDownSchema, inspectVersionDownSchema, inspectAuthenticatedDownSchema, inspectAnalyzeQueryDownSchema, inspectErrorDownSchema);
export const inspectDownMessageSchema = v.tuple([
    v.literal('inspect'),
    inspectDownBodySchema,
]);
//# sourceMappingURL=inspect-down.js.map