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