import { EntityManager } from '@mikro-orm/core';
import { MikroKyselyPlugin } from './plugin/index.js';
/**
 * @inheritDoc
 */
export class SqlEntityManager extends EntityManager {
  /**
   * Creates a QueryBuilder instance
   */
  createQueryBuilder(entityName, alias, type, loggerContext) {
    const context = this.getContext(false);
    return this.driver.createQueryBuilder(
      entityName,
      context.getTransactionContext(),
      type,
      true,
      loggerContext ?? context.loggerContext,
      alias,
      this,
    );
  }
  /**
   * Shortcut for `createQueryBuilder()`
   */
  qb(entityName, alias, type, loggerContext) {
    return this.createQueryBuilder(entityName, alias, type, loggerContext);
  }
  /**
   * Returns configured Kysely instance.
   */
  getKysely(options = {}) {
    let kysely = this.getConnection(options.type).getClient();
    if (
      options.columnNamingStrategy != null ||
      options.tableNamingStrategy != null ||
      options.processOnCreateHooks != null ||
      options.processOnUpdateHooks != null ||
      options.convertValues != null
    ) {
      kysely = kysely.withPlugin(new MikroKyselyPlugin(this, options));
    }
    return kysely;
  }
  /** Executes a raw SQL query, using the current transaction context if available. */
  async execute(query, params = [], method = 'all', loggerContext) {
    return this.getDriver().execute(
      query,
      params,
      method,
      this.getContext(false).getTransactionContext(),
      loggerContext,
    );
  }
  getRepository(entityName) {
    return super.getRepository(entityName);
  }
  applyDiscriminatorCondition(entityName, where) {
    // this is handled in QueryBuilder now for SQL drivers
    return where;
  }
}
