// node_modules/tsup/assets/esm_shims.js
import path from "path";
import { fileURLToPath } from "url";
var getFilename = () => fileURLToPath(import.meta.url);
var getDirname = () => path.dirname(getFilename());
var __dirname = /* @__PURE__ */ getDirname();

// src/index.ts
import nodeGypBuild from "node-gyp-build";
import { join } from "path";

// src/stack-path.ts
import { dirname } from "path";
function getCallerDirname() {
  const e = new Error();
  if (e.stack == null) {
    Error.captureStackTrace(e);
  }
  return dirname(extractCallerPath(e.stack));
}
var patterns = process.platform === "win32" ? [
  // File URLs: "at functionName (file:///C:/path/file.js:1:1)"
  /\bat\s.+?\((?<path>file:\/\/\/.+?):\d+:\d+\)$/,
  // File URLs direct: "at file:///C:/path/file.js:1:1"
  /\bat\s(?<path>file:\/\/\/.+?):\d+:\d+$/,
  // Standard: "at functionName (C:\path\file.js:1:1)"
  /\bat\s.+?\((?<path>[A-Z]:\\.+):\d+:\d+\)$/,
  // direct: "at C:\path\file.js:1:1"
  /\bat\s(?<path>[A-Z]:\\.+):\d+:\d+$/,
  // UNC: "at functionName (\\server\share\path\file.js:1:1)"
  /\bat\s.+?\((?<path>\\\\.+):\d+:\d+\)$/,
  // direct: "at \\server\share\path\file.js:1:1"
  /\bat\s(?<path>\\\\.+):\d+:\d+$/
] : [
  // Standard: "at functionName (/path/file.js:1:1)"
  /\bat\s.+?\((?<path>\/.+?):\d+:\d+\)$/,
  // Anonymous or direct: "at /path/file.js:1:1"
  // eslint-disable-next-line security/detect-unsafe-regex -- Pattern is safe: no nested quantifiers
  /\bat\s(?:[^\s()]+\s)?(?<path>\/[^:]+):\d+:\d+$/
];
var MaybeUrlRE = /^[a-z]{2,5}:\/\//i;
function extractCallerPath(stack) {
  const frames = stack.split("\n").filter(Boolean);
  const callerFrame = frames.findIndex(
    (frame) => frame.includes("getCallerDirname")
  );
  if (callerFrame === -1) {
    throw new Error("Invalid stack trace format: missing caller frame");
  }
  for (let i = callerFrame + 1; i < frames.length; i++) {
    const frame = frames[i];
    for (const pattern of patterns) {
      const g = frame?.trim().match(pattern)?.groups;
      if (g != null && g["path"]) {
        const path2 = g["path"];
        if (MaybeUrlRE.test(path2)) {
          try {
            return new URL(path2).pathname;
          } catch {
          }
        }
        return path2;
      }
    }
  }
  throw new Error("Invalid stack trace format: no parsable frames");
}

// src/dirname.ts
function _dirname() {
  try {
    if (typeof __dirname !== "undefined") return __dirname;
  } catch {
  }
  return getCallerDirname();
}

// src/lru-cache.ts
var LRUCache = class {
  cache = /* @__PURE__ */ new Map();
  maxCapacity;
  constructor(capacity) {
    if (capacity < 1) {
      throw new RangeError("LRU cache capacity must be at least 1");
    }
    this.maxCapacity = capacity;
  }
  /**
   * Get a value from the cache.
   * If found, moves the entry to the end (most recently used).
   */
  get(key) {
    const value = this.cache.get(key);
    if (value !== void 0) {
      this.cache.delete(key);
      this.cache.set(key, value);
    }
    return value;
  }
  /**
   * Set a value in the cache.
   * If key exists, updates and moves to end.
   * If at capacity, evicts the oldest entry first.
   */
  set(key, value) {
    if (this.cache.has(key)) {
      this.cache.delete(key);
    } else if (this.cache.size >= this.maxCapacity) {
      const oldestKey = this.cache.keys().next().value;
      if (oldestKey !== void 0) {
        this.cache.delete(oldestKey);
      }
    }
    this.cache.set(key, value);
  }
  /**
   * Delete an entry from the cache.
   */
  delete(key) {
    return this.cache.delete(key);
  }
  /**
   * Check if a key exists in the cache.
   * Does NOT update recency.
   */
  has(key) {
    return this.cache.has(key);
  }
  /**
   * Clear all entries from the cache.
   */
  clear() {
    this.cache.clear();
  }
  /**
   * Get the current number of entries in the cache.
   */
  size() {
    return this.cache.size;
  }
  /**
   * Get the maximum capacity of the cache.
   */
  capacity() {
    return this.maxCapacity;
  }
};

// src/sql-tag-store.ts
var DEFAULT_CAPACITY = 1e3;
var SQLTagStore = class {
  database;
  cache;
  maxCapacity;
  constructor(db, capacity = DEFAULT_CAPACITY) {
    if (!db.isOpen) {
      throw new Error("Database is not open");
    }
    this.database = db;
    this.maxCapacity = capacity;
    this.cache = new LRUCache(capacity);
  }
  /**
   * Returns the associated database instance.
   */
  get db() {
    return this.database;
  }
  /**
   * Returns the maximum capacity of the statement cache.
   */
  get capacity() {
    return this.maxCapacity;
  }
  /**
   * Returns the current number of cached statements.
   */
  size() {
    return this.cache.size();
  }
  /**
   * Clears all cached statements.
   */
  clear() {
    this.cache.clear();
  }
  /**
   * Execute an INSERT, UPDATE, DELETE or other statement that doesn't return rows.
   * Returns an object with `changes` and `lastInsertRowid`.
   */
  run(strings, ...values) {
    const stmt = this.getOrPrepare(strings);
    return stmt.run(...values);
  }
  /**
   * Execute a query and return the first row, or undefined if no rows.
   */
  get(strings, ...values) {
    const stmt = this.getOrPrepare(strings);
    return stmt.get(...values);
  }
  /**
   * Execute a query and return all rows as an array.
   */
  all(strings, ...values) {
    const stmt = this.getOrPrepare(strings);
    return stmt.all(...values);
  }
  /**
   * Execute a query and return an iterator over the rows.
   */
  iterate(strings, ...values) {
    const stmt = this.getOrPrepare(strings);
    return stmt.iterate(...values);
  }
  /**
   * Get a cached statement or prepare a new one.
   * If a cached statement has been finalized, it's evicted and a new one is prepared.
   */
  getOrPrepare(strings) {
    if (!this.database.isOpen) {
      throw new Error("Database is not open");
    }
    const sql = this.buildSQL(strings);
    const cached = this.cache.get(sql);
    if (cached) {
      if (!cached.finalized) {
        return cached;
      }
      this.cache.delete(sql);
    }
    const stmt = this.database.prepare(sql);
    this.cache.set(sql, stmt);
    return stmt;
  }
  /**
   * Build the SQL string by joining template parts with `?` placeholders.
   */
  buildSQL(strings) {
    let sql = strings[0] ?? "";
    for (let i = 1; i < strings.length; i++) {
      sql += "?" + (strings[i] ?? "");
    }
    return sql;
  }
};

// src/index.ts
var binding = nodeGypBuild(join(_dirname(), ".."));
var DatabaseSync = binding.DatabaseSync;
DatabaseSync.prototype.createTagStore = function(capacity) {
  return new SQLTagStore(this, capacity);
};
var StatementSync = binding.StatementSync;
var Session = binding.Session;
var constants = binding.constants;
var backup = binding.backup;
var index_default = binding;
export {
  DatabaseSync,
  SQLTagStore,
  Session,
  StatementSync,
  backup,
  constants,
  index_default as default
};
//# sourceMappingURL=index.mjs.map