import { SQLiteStore, dropStore } from "../sqlite-store.js";
import { open } from "./types.js";
function dropOpSQLiteStore(name) {
  return dropStore(
    name,
    (filename, opts) => new OpSQLiteDatabase(filename, opts)
  );
}
function opSQLiteStoreProvider(opts) {
  return {
    create: (name) => new SQLiteStore(
      name,
      (name2, options) => new OpSQLiteDatabase(name2, options),
      opts
    ),
    drop: dropOpSQLiteStore
  };
}
class OpSQLitePreparedStatement {
  #db;
  #sql;
  constructor(db, sql) {
    this.#db = db;
    this.#sql = sql;
  }
  async firstValue(params) {
    const rows = await this.#db.executeRaw(this.#sql, params);
    return rows[0]?.[0];
  }
  async exec(params) {
    await this.#db.executeRaw(this.#sql, params);
  }
}
class OpSQLiteDatabase {
  #db;
  #filename;
  constructor(filename, opts) {
    this.#filename = filename;
    const openOpts = { name: filename };
    if (opts?.location) {
      openOpts.location = opts.location;
    }
    if (opts?.encryptionKey) {
      openOpts.encryptionKey = opts.encryptionKey;
    }
    this.#db = open(openOpts);
  }
  close() {
    this.#db.close();
  }
  destroy() {
    try {
      const tempDb = open({ name: this.#filename });
      tempDb.delete();
      tempDb.close();
    } catch (_error) {
    }
  }
  prepare(sql) {
    return new OpSQLitePreparedStatement(this.#db, sql);
  }
  execSync(sql) {
    this.#db.executeRawSync(sql, []);
  }
}
export {
  opSQLiteStoreProvider
};
//# sourceMappingURL=store.js.map
