"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.maxIdentifierLength = maxIdentifierLength;
exports.normalizeGeneratedIdentifier = normalizeGeneratedIdentifier;
exports.defaultBlueprintIdentifierName = defaultBlueprintIdentifierName;
exports.defaultSchemaIndexName = defaultSchemaIndexName;
exports.defaultEntityIndexName = defaultEntityIndexName;
exports.defaultEntityForeignKeyName = defaultEntityForeignKeyName;
const MAX_IDENTIFIER_LENGTH = {
    mysql: 64,
    postgres: 63
};
function maxIdentifierLength(dialect) {
    return MAX_IDENTIFIER_LENGTH[dialect];
}
function normalizeGeneratedIdentifier(name, dialect) {
    const maxLength = maxIdentifierLength(dialect);
    if (name.length <= maxLength)
        return name;
    const hash = hashIdentifier(name);
    const remaining = maxLength - hash.length - 2;
    const headLength = Math.ceil(remaining / 2);
    const tailLength = Math.floor(remaining / 2);
    return `${name.slice(0, headLength)}_${hash}_${name.slice(name.length - tailLength)}`;
}
function defaultBlueprintIdentifierName(tableName, columns, suffix, dialect) {
    return normalizeGeneratedIdentifier(`${tableName}_${columns.join('_')}_${suffix}`, dialect);
}
function defaultSchemaIndexName(tableName, columns, unique, dialect) {
    return defaultBlueprintIdentifierName(tableName, columns, unique ? 'unique' : 'index', dialect);
}
function defaultEntityIndexName(tableName, columns, dialect) {
    return normalizeGeneratedIdentifier(`idx_${tableName}_${columns.join('_')}`, dialect);
}
function defaultEntityForeignKeyName(tableName, columns, dialect) {
    return normalizeGeneratedIdentifier(`fk_${tableName}_${columns.join('_')}`, dialect);
}
function hashIdentifier(input) {
    let hash = 0x811c9dc5;
    for (let i = 0; i < input.length; i++) {
        hash ^= input.charCodeAt(i);
        hash = Math.imul(hash, 0x01000193) >>> 0;
    }
    return hash.toString(16).padStart(8, '0');
}
//# sourceMappingURL=identifiers.js.map