"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqlTestingHelper = void 0;
const __ΩPartial = ['T', 'Partial', 'l+e#!e"!fRb!Pde"!gN#"w"y'];
function __assignType(fn, args) {
    fn.__type = args;
    return fn;
}
/*@ts-ignore*/
var { __ΩClassType } = require('@deepkit/core');
const mysql_1 = require("@deepkit/mysql");
const orm_1 = require("@deepkit/orm");
const postgres_1 = require("@deepkit/postgres");
const type_1 = require("@deepkit/type");
const node_test_1 = require("node:test");
/*@ts-ignore*/
var { __ΩEntityFields } = require('../database');
class SqlTestingHelper {
    mockEntity(entityCls, data) {
        const entityRefCls = type_1.ReflectionClass.from(entityCls);
        this.mdbAdapter ??= new orm_1.MemoryDatabaseAdapter();
        this.mdbPersistence ??= this.mdbAdapter.createPersistence();
        this.mdbPersistence.insert(entityRefCls, Array.isArray(data) ? data : [data]);
        this.installMocks();
    }
    clearMocks() {
        this.mdbAdapter?.['store'].clear();
    }
    installMocks() {
        // Mock MySQL adapter
        const mysqlQfDescriptor = Object.getOwnPropertyDescriptor(mysql_1.MySQLDatabaseAdapter.prototype, 'queryFactory');
        if (!(mysqlQfDescriptor && 'value' in mysqlQfDescriptor && mysqlQfDescriptor.value?.mock)) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            node_test_1.mock.method(mysql_1.MySQLDatabaseAdapter.prototype, 'queryFactory', __assignType((session) => this.mdbAdapter.queryFactory(session), ['session', '', 'P"2!"/"']));
        }
        // Mock Postgres adapter
        const pgQfDescriptor = Object.getOwnPropertyDescriptor(postgres_1.PostgresDatabaseAdapter.prototype, 'queryFactory');
        if (!(pgQfDescriptor && 'value' in pgQfDescriptor && pgQfDescriptor.value?.mock)) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            node_test_1.mock.method(postgres_1.PostgresDatabaseAdapter.prototype, 'queryFactory', __assignType((session) => this.mdbAdapter.queryFactory(session), ['session', '', 'P"2!"/"']));
        }
        const mdbAdapter = this.mdbAdapter;
        const originalCreateQuery = orm_1.MemoryQueryFactory.prototype.createQuery;
        node_test_1.mock.method(orm_1.MemoryQueryFactory.prototype, 'createQuery', __assignType(function (entity) {
            const entityRefCls = type_1.ReflectionClass.from(entity);
            if (!mdbAdapter?.['store'].has(entityRefCls)) {
                throw new Error(`No mock data found for entity: ${entityRefCls.getClassName()}`);
            }
            return originalCreateQuery.call(this, entity);
        }, [() => orm_1.MemoryQueryFactory, 'this', () => __ΩClassType, 'entity', '', 'PP7!2"n#2$"/%']));
    }
}
exports.SqlTestingHelper = SqlTestingHelper;
SqlTestingHelper.__type = [() => orm_1.MemoryDatabaseAdapter, 'mdbAdapter', () => orm_1.DatabasePersistence, 'mdbPersistence', () => __ΩClassType, 'entityCls', () => __ΩPartial, () => __ΩEntityFields, () => __ΩPartial, () => __ΩEntityFields, 'data', 'mockEntity', 'clearMocks', 'installMocks', 'SqlTestingHelper', 'P7!3"8;P7#3$8;P"o%"2&P"o("o\'"F"o*"o)"J2+"0,P"0-P"0.5w/'];
//# sourceMappingURL=sql.js.map