import { mapPostgresToLite, warnIfDataTypeSupported } from "../../../../db/pg-to-lite.js";
import { Default, Nothing, Index } from "../../../../db/postgres-replica-identity-enum.js";
import { ZERO_VERSION_COLUMN_NAME } from "../../../replicator/schema/constants.js";
const ALLOWED_TABLE_CHARS = /^[A-Za-z_]+[A-Za-z0-9_-]*$/;
const ALLOWED_COLUMN_CHARS = /^[A-Za-z_]+[.A-Za-z0-9_-]*$/;
function validate(lc, table) {
  if (ZERO_VERSION_COLUMN_NAME in table.columns) {
    throw new UnsupportedTableSchemaError(
      `Table "${table.name}" uses reserved column name "${ZERO_VERSION_COLUMN_NAME}"`
    );
  }
  if (!table.primaryKey?.length && table.replicaIdentity === Default) {
    lc.warn?.(
      `


Table "${table.name}" needs a primary key in order to be synced to clients. Add one with 'ALTER TABLE "${table.name}" ADD PRIMARY KEY (...)'.


`
    );
  }
  if (table.replicaIdentity === Nothing) {
    throw new UnsupportedTableSchemaError(
      `Table "${table.name}" with REPLICA IDENTITY NOTHING cannot be replicated`
    );
  }
  if (table.replicaIdentity === Index && table.replicaIdentityColumns.length === 0) {
    throw new UnsupportedTableSchemaError(
      `Table "${table.name}" is missing its REPLICA IDENTITY INDEX`
    );
  }
  if (!ALLOWED_TABLE_CHARS.test(table.name)) {
    throw new UnsupportedTableSchemaError(
      `Table "${table.name}" has invalid characters.`
    );
  }
  for (const [col, spec] of Object.entries(mapPostgresToLite(table).columns)) {
    if (!ALLOWED_COLUMN_CHARS.test(col)) {
      throw new UnsupportedTableSchemaError(
        `Column "${col}" in table "${table.name}" has invalid characters.`
      );
    }
    warnIfDataTypeSupported(lc, spec.dataType, table.name, col);
  }
}
class UnsupportedTableSchemaError extends Error {
  name = "UnsupportedTableSchemaError";
  constructor(msg) {
    super(msg);
  }
}
export {
  UnsupportedTableSchemaError,
  validate
};
//# sourceMappingURL=validation.js.map
