"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MigrationCharactersCommand = void 0;
exports.standardizeDbCollation = standardizeDbCollation;
const tslib_1 = require("tslib");
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
const app_1 = require("@deepkit/app");
const state_1 = require("../../app/state");
const common_1 = require("../common");
const dialect_1 = require("../dialect");
const __ΩICollationOptions = ['charset', 'collation', 'ICollationOptions', 'P&4!8&4"8Mw#y'];
const DEFAULT_CHARSET = 'utf8mb4';
const DEFAULT_COLLATION = 'utf8mb4_0900_ai_ci';
async function standardizeDbCollation(db, options) {
    const dialect = (0, dialect_1.getDialect)(db.adapter);
    if (dialect === 'postgres') {
        console.warn('standardizeDbCollation is not applicable to PostgreSQL — skipping');
        return;
    }
    const charset = options?.charset ?? DEFAULT_CHARSET;
    const collation = options?.collation ?? DEFAULT_COLLATION;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const dbNameResult = await db.rawQuery('SELECT DATABASE()');
    const dbName = Object.values(dbNameResult[0])[0];
    await db.rawQuery(`ALTER DATABASE ${dbName} CHARACTER SET = ${charset} COLLATE = ${collation}`);
    const tablesResult = await db.rawQuery('SHOW TABLES');
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const tables = tablesResult.map(__assignType((row) => Object.values(row)[0], ['row', '', 'P"2!"/"']));
    for (const table of tables) {
        await db.rawQuery(`ALTER TABLE ${table} CONVERT TO CHARACTER SET ${charset} COLLATE ${collation}`);
    }
}
standardizeDbCollation.__type = [() => common_1.BaseDatabase, 'db', () => __ΩICollationOptions, 'options', 'standardizeDbCollation', 'PP7!2"n#2$8"/%'];
let MigrationCharactersCommand = class MigrationCharactersCommand {
    constructor(dbProvider) {
        this.dbProvider = dbProvider;
    }
    async execute(charset = DEFAULT_CHARSET, collation = DEFAULT_COLLATION) {
        const dialect = (0, dialect_1.getDialect)(this.dbProvider.db.adapter);
        if (dialect === 'postgres') {
            console.warn('Character set standardization is not applicable to PostgreSQL');
            return;
        }
        await standardizeDbCollation(this.dbProvider.db, { charset, collation });
    }
};
exports.MigrationCharactersCommand = MigrationCharactersCommand;
MigrationCharactersCommand.__type = [() => state_1.DBProvider, 'dbProvider', 'constructor', 'charset', () => DEFAULT_CHARSET, 'collation', () => DEFAULT_COLLATION, 'execute', 'MigrationCharactersCommand', 'PP7!2";"0#P&2$>%&2&>\'"0(5w)'];
exports.MigrationCharactersCommand = MigrationCharactersCommand = tslib_1.__decorate([
    app_1.cli.controller('migration:charset'),
    tslib_1.__metadata("design:paramtypes", [state_1.DBProvider])
], MigrationCharactersCommand);
//# sourceMappingURL=characters.js.map