import { literalUnion, valita_exports } from "../../../../../../shared/src/valita.js";
import { must } from "../../../../../../shared/src/must.js";
import { jsonObjectSchema } from "./json.js";
import { jsonValueSchema } from "../../../../../../shared/src/bigint-json.js";
import { columnSpec, indexSpec, tableSpec } from "../../../../db/specs.js";
//#region ../zero-cache/src/services/change-source/protocol/current/data.ts
/**
* Data plane messages encapsulate changes that are sent by ChangeSources,
* forwarded / fanned out to subscribers by the ChangeStreamerService, and
* stored in the Change DB for catchup of old subscribers.
*/
var beginSchema = valita_exports.object({
	tag: valita_exports.literal("begin"),
	json: literalUnion("p", "s").optional(),
	skipAck: valita_exports.boolean().optional()
});
var commitSchema = valita_exports.object({ tag: valita_exports.literal("commit") });
var rollbackSchema = valita_exports.object({ tag: valita_exports.literal("rollback") });
var rowKeySchema = valita_exports.object({
	columns: valita_exports.array(valita_exports.string()),
	type: literalUnion("default", "nothing", "full", "index").optional()
});
var relationSchema = valita_exports.object({
	schema: valita_exports.string(),
	name: valita_exports.string(),
	rowKey: rowKeySchema.optional(),
	keyColumns: valita_exports.array(valita_exports.string()).optional(),
	replicaIdentity: literalUnion("default", "nothing", "full", "index").optional()
}).map((rel) => {
	const { rowKey, ...rest } = rel;
	if (rowKey) return {
		...rest,
		rowKey
	};
	return {
		...rest,
		rowKey: {
			columns: must(rel.keyColumns),
			type: rel.replicaIdentity
		}
	};
});
var newRelationSchema = valita_exports.object({
	schema: valita_exports.string(),
	name: valita_exports.string(),
	rowKey: rowKeySchema
});
var tableMetadataSchema = valita_exports.object({ rowKey: valita_exports.record(jsonValueSchema) }).rest(jsonValueSchema);
var rowSchema = valita_exports.record(jsonValueSchema);
var insertSchema = valita_exports.object({
	tag: valita_exports.literal("insert"),
	relation: relationSchema,
	new: rowSchema
});
var updateSchema = valita_exports.object({
	tag: valita_exports.literal("update"),
	relation: relationSchema,
	key: rowSchema.nullable(),
	new: rowSchema
});
var deleteSchema = valita_exports.object({
	tag: valita_exports.literal("delete"),
	relation: relationSchema,
	key: rowSchema
});
var truncateSchema = valita_exports.object({
	tag: valita_exports.literal("truncate"),
	relations: valita_exports.array(relationSchema)
});
var identifierSchema = valita_exports.object({
	schema: valita_exports.string(),
	name: valita_exports.string()
});
var backfillIDSchema = jsonObjectSchema;
var createTableSchema = valita_exports.object({
	tag: valita_exports.literal("create-table"),
	spec: tableSpec,
	metadata: tableMetadataSchema.optional(),
	backfill: valita_exports.record(backfillIDSchema).optional()
});
var renameTableSchema = valita_exports.object({
	tag: valita_exports.literal("rename-table"),
	old: identifierSchema,
	new: identifierSchema
});
var updateTableMetadataSchema = valita_exports.object({
	tag: valita_exports.literal("update-table-metadata"),
	table: identifierSchema,
	old: tableMetadataSchema,
	new: tableMetadataSchema
});
var columnSchema = valita_exports.object({
	name: valita_exports.string(),
	spec: columnSpec
});
var addColumnSchema = valita_exports.object({
	tag: valita_exports.literal("add-column"),
	table: identifierSchema,
	column: columnSchema,
	tableMetadata: tableMetadataSchema.optional(),
	backfill: backfillIDSchema.optional()
});
var updateColumnSchema = valita_exports.object({
	tag: valita_exports.literal("update-column"),
	table: identifierSchema,
	old: columnSchema,
	new: columnSchema
});
var dropColumnSchema = valita_exports.object({
	tag: valita_exports.literal("drop-column"),
	table: identifierSchema,
	column: valita_exports.string()
});
var dropTableSchema = valita_exports.object({
	tag: valita_exports.literal("drop-table"),
	id: identifierSchema
});
var createIndexSchema = valita_exports.object({
	tag: valita_exports.literal("create-index"),
	spec: indexSpec
});
var dropIndexSchema = valita_exports.object({
	tag: valita_exports.literal("drop-index"),
	id: identifierSchema
});
var downloadStatusSchema = valita_exports.object({
	rows: valita_exports.number(),
	totalRows: valita_exports.number(),
	totalBytes: valita_exports.number().optional()
});
var backfillSchema = valita_exports.object({
	tag: valita_exports.literal("backfill"),
	relation: newRelationSchema,
	columns: valita_exports.array(valita_exports.string()),
	watermark: valita_exports.string(),
	rowValues: valita_exports.array(valita_exports.array(jsonValueSchema)),
	status: downloadStatusSchema.optional()
});
var backfillCompletedSchema = valita_exports.object({
	tag: valita_exports.literal("backfill-completed"),
	relation: newRelationSchema,
	columns: valita_exports.array(valita_exports.string()),
	watermark: valita_exports.string(),
	status: downloadStatusSchema.optional()
});
var dataChangeSchema = valita_exports.union(insertSchema, updateSchema, deleteSchema, truncateSchema, backfillSchema);
var dataChangeTags = [
	"insert",
	"update",
	"delete",
	"truncate",
	"backfill"
];
literalUnion(...dataChangeTags);
var schemaChanges = [
	createTableSchema,
	renameTableSchema,
	updateTableMetadataSchema,
	addColumnSchema,
	updateColumnSchema,
	dropColumnSchema,
	dropTableSchema,
	createIndexSchema,
	dropIndexSchema,
	backfillCompletedSchema
];
var schemaChangeTags = [
	"create-table",
	"rename-table",
	"update-table-metadata",
	"add-column",
	"update-column",
	"drop-column",
	"drop-table",
	"create-index",
	"drop-index",
	"backfill-completed"
];
var schemaChangeSchema = valita_exports.union(...schemaChanges);
literalUnion(...schemaChangeTags);
var schemaChangeTagSet = new Set(schemaChangeTags);
function isSchemaChange(change) {
	return schemaChangeTagSet.has(change.tag);
}
var dataChangeTagSet = new Set(dataChangeTags);
function isDataChange(change) {
	return dataChangeTagSet.has(change.tag);
}
//#endregion
export { addColumnSchema, backfillCompletedSchema, backfillIDSchema, backfillSchema, beginSchema, commitSchema, createIndexSchema, createTableSchema, dataChangeSchema, deleteSchema, downloadStatusSchema, dropColumnSchema, dropIndexSchema, dropTableSchema, identifierSchema, insertSchema, isDataChange, isSchemaChange, newRelationSchema, relationSchema, renameTableSchema, rollbackSchema, rowSchema, schemaChangeSchema, tableMetadataSchema, truncateSchema, updateColumnSchema, updateSchema, updateTableMetadataSchema };

//# sourceMappingURL=data.js.map