import { must } from "../../shared/src/must.js";
import { mapAllEntries } from "../../shared/src/objects.js";
import { literalUnion } from "../../shared/src/valita.js";
import { object, array, record, string } from "@badrap/valita";
const valueTypeSchema = literalUnion(
  "string",
  "number",
  "boolean",
  "null",
  "json"
);
const columnSchemaSchema = object({
  type: valueTypeSchema
});
const tableSchemaSchema = object({
  columns: record(columnSchemaSchema),
  primaryKey: array(string())
});
const clientSchemaSchema = object({
  tables: record(tableSchemaSchema)
});
const keyCmp = ([a], [b]) => a < b ? -1 : a > b ? 1 : 0;
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()
        }
      ])
    )
  };
}
export {
  clientSchemaSchema,
  columnSchemaSchema,
  normalizeClientSchema,
  tableSchemaSchema,
  valueTypeSchema
};
//# sourceMappingURL=client-schema.js.map
