'use strict';

var kysely = require('kysely');
var events = require('events');

// ../../node_modules/.pnpm/tsup@8.2.4_jiti@1.21.6_postcss@8.4.41_typescript@5.5.4_yaml@2.5.0/node_modules/tsup/assets/cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
var BunWorkerDriver = class {
  config;
  worker;
  connection;
  connectionMutex = new ConnectionMutex();
  mitt;
  constructor(config) {
    this.config = config;
  }
  async init() {
    this.worker = this.config?.worker ?? new Worker(
      new URL("./worker", importMetaUrl),
      { type: "module" }
    );
    this.mitt = new events.EventEmitter();
    this.worker.onmessage = ({ data: [type, data, err] }) => {
      this.mitt?.emit(type, data, err);
    };
    this.worker.postMessage([
      0,
      // init
      this.config?.url,
      this.config?.cacheStatment
    ]);
    await new Promise((resolve, reject) => {
      this.mitt?.once(0, (_, err) => err ? reject(err) : resolve());
    });
    this.connection = new BunWorkerConnection(this.worker, this.mitt);
    await this.config?.onCreateConnection?.(this.connection);
  }
  async acquireConnection() {
    await this.connectionMutex.lock();
    return this.connection;
  }
  async beginTransaction(connection) {
    await connection.executeQuery(kysely.CompiledQuery.raw("begin"));
  }
  async commitTransaction(connection) {
    await connection.executeQuery(kysely.CompiledQuery.raw("commit"));
  }
  async rollbackTransaction(connection) {
    await connection.executeQuery(kysely.CompiledQuery.raw("rollback"));
  }
  async releaseConnection() {
    this.connectionMutex.unlock();
  }
  async destroy() {
    if (!this.worker) {
      return;
    }
    this.worker.postMessage([2]);
    return new Promise((resolve, reject) => {
      this.mitt?.once(2, (_, err) => {
        if (err) {
          reject(err);
        } else {
          this.worker?.terminate();
          this.mitt?.removeAllListeners();
          this.mitt = void 0;
          resolve();
        }
      });
    });
  }
};
var ConnectionMutex = class {
  promise;
  resolve;
  async lock() {
    while (this.promise) {
      await this.promise;
    }
    this.promise = new Promise((resolve) => {
      this.resolve = resolve;
    });
  }
  unlock() {
    const resolve = this.resolve;
    this.promise = void 0;
    this.resolve = void 0;
    resolve?.();
  }
};
var BunWorkerConnection = class {
  constructor(worker, mitt) {
    this.worker = worker;
    this.mitt = mitt;
  }
  streamQuery() {
    throw new Error("Bun:sqlite-worker driver doesn't support streaming");
  }
  async executeQuery(compiledQuery) {
    const { parameters, sql, query } = compiledQuery;
    const isSelect = kysely.SelectQueryNode.is(query);
    this.worker.postMessage([1, isSelect, sql, parameters]);
    return new Promise((resolve, reject) => {
      if (!this.mitt) {
        reject(new Error("kysely instance has been destroyed"));
      }
      this.mitt.once(1, (data, err) => !err && data ? resolve(data) : reject(err));
    });
  }
};

// src/index.ts
var BunWorkerDialect = class {
  config;
  /**
   * dialect for `bun:sqlite`, run sql in worker
   */
  constructor(config) {
    this.config = config;
  }
  createDriver() {
    return new BunWorkerDriver(this.config);
  }
  createQueryCompiler() {
    return new kysely.SqliteQueryCompiler();
  }
  createAdapter() {
    return new kysely.SqliteAdapter();
  }
  createIntrospector(db) {
    return new kysely.SqliteIntrospector(db);
  }
};

exports.BunWorkerDialect = BunWorkerDialect;
