import { must } from "../../shared/src/must.js";
import { mapAllEntries } from "../../shared/src/objects.js";
import * as v from "../../shared/src/valita.js";
export const valueTypeSchema = v.literalUnion('string', 'number', 'boolean', 'null', 'json');
export const columnSchemaSchema = v.object({
    type: valueTypeSchema,
});
export const tableSchemaSchema = v.object({
    columns: v.record(columnSchemaSchema),
    // TODO: Make this non-optional when bumping the
    //       MIN_SERVER_SUPPORTED_SYNC_PROTOCOL to 30+.
    primaryKey: v.array(v.string()).optional(),
});
export const clientSchemaSchema = v.object({
    tables: v.record(tableSchemaSchema),
});
const keyCmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0;
/**
 * Returns a normalized schema (with the tables and columns sorted)
 * suitable for hashing.
 */
export function normalizeClientSchema(schema) {
    return {
        tables: mapAllEntries(schema.tables, tables => tables.sort(keyCmp).map(([name, table]) => [
            name,
            {
                columns: mapAllEntries(table.columns, e => e.sort(keyCmp)),
                primaryKey: must(table.primaryKey, `new clients always specify a primaryKey`).sort(),
            },
        ])),
    };
}
//# sourceMappingURL=client-schema.js.map