import { entityKind, is } from "../entity.js";
import { DefaultLogger } from "../logger.js";
import { MsSqlDatabase } from "../mssql-core/db.js";
import { MsSqlDialect } from "../mssql-core/dialect.js";
import {
  createTableRelationsHelpers,
  extractTablesRelationalConfig
} from "../relations.js";
import { isConfig } from "../utils.js";
import { AutoPool } from "./pool.js";
import { NodeMsSqlSession } from "./session.js";
class NodeMsSqlDriver {
  constructor(client, dialect, options = {}) {
    this.client = client;
    this.dialect = dialect;
    this.options = options;
  }
  static [entityKind] = "NodeMsSqlDriver";
  createSession(schema) {
    return new NodeMsSqlSession(this.client, this.dialect, schema, { logger: this.options.logger });
  }
}
import { MsSqlDatabase as MsSqlDatabase2 } from "../mssql-core/db.js";
function construct(client, config = {}) {
  const dialect = new MsSqlDialect({ casing: config.casing });
  let logger;
  if (config.logger === true) {
    logger = new DefaultLogger();
  } else if (config.logger !== false) {
    logger = config.logger;
  }
  if (isCallbackClient(client)) {
    client = client.promise();
  }
  let schema;
  if (config.schema) {
    const tablesConfig = extractTablesRelationalConfig(
      config.schema,
      createTableRelationsHelpers
    );
    schema = {
      fullSchema: config.schema,
      schema: tablesConfig.tables,
      tableNamesMap: tablesConfig.tableNamesMap
    };
  }
  const driver = new NodeMsSqlDriver(client, dialect, { logger });
  const session = driver.createSession(schema);
  const db = new MsSqlDatabase(dialect, session, schema);
  if (is(client, AutoPool)) {
    db.$client = client.$instance();
  } else {
    db.$client = client;
  }
  return db;
}
function drizzle(...params) {
  if (typeof params[0] === "string") {
    const instance = new AutoPool(params[0]);
    return construct(instance, params[1]);
  }
  if (isConfig(params[0])) {
    const { connection, client, ...drizzleConfig } = params[0];
    if (client) return construct(client, drizzleConfig);
    const instance = typeof connection === "string" ? new AutoPool(connection) : new AutoPool(connection);
    return construct(instance, drizzleConfig);
  }
  return construct(params[0], params[1]);
}
function isCallbackClient(client) {
  return typeof client.promise === "function";
}
((drizzle2) => {
  function mock(config) {
    return construct({}, config);
  }
  drizzle2.mock = mock;
})(drizzle || (drizzle = {}));
export {
  MsSqlDatabase2 as MsSqlDatabase,
  NodeMsSqlDriver,
  drizzle
};
//# sourceMappingURL=driver.js.map