import {
  SqlError
} from "./index-p3fb6n1p.js";
import {
  SafeIntegers,
  SqlClient,
  defaultTransforms,
  layer,
  make as make3,
  makeCompilerSqlite
} from "./index-wezxaapt.js";
import {
  Scope,
  Service,
  add,
  addFinalizer1 as addFinalizer,
  addFinalizer2,
  as1 as as,
  die2 as die,
  effectServices,
  fail3 as fail,
  flatMap1 as flatMap,
  gen1 as gen,
  get,
  getCurrent,
  getUnsafe,
  identity,
  make2 as make,
  make8 as make2,
  map6 as map,
  provide1 as provide,
  succeed4 as succeed,
  sync1 as sync,
  tap,
  try_,
  uninterruptibleMask1 as uninterruptibleMask,
  unwrap2 as unwrap,
  withFiber1 as withFiber
} from "./index-w9j14fjm.js";
import"./index-sdksp5px.js";

// ../../node_modules/.bun/@effect+sql-sqlite-bun@https+++pkg.pr.new+Effect-TS+effect-smol+@effect+sql-sqlite-bun@8881a9b+eecd35a1726d7096/node_modules/@effect/sql-sqlite-bun/dist/SqliteClient.js
import { Database } from "bun:sqlite";
var ATTR_DB_SYSTEM_NAME = "db.system.name";
var TypeId = "~@effect/sql-sqlite-bun/SqliteClient";
var SqliteClient = /* @__PURE__ */ Service("@effect/sql-sqlite-bun/Client");
var make4 = (options) => gen(function* () {
  const compiler = makeCompilerSqlite(options.transformQueryNames);
  const transformRows = options.transformResultNames ? defaultTransforms(options.transformResultNames).array : undefined;
  const makeConnection = gen(function* () {
    const db = new Database(options.filename, {
      readonly: options.readonly,
      readwrite: options.readwrite ?? true,
      create: options.create ?? true
    });
    yield* addFinalizer2(() => sync(() => db.close()));
    if (options.disableWAL !== true) {
      db.run("PRAGMA journal_mode = WAL;");
    }
    const run = (sql, params = []) => withFiber((fiber) => {
      const statement = db.query(sql);
      const useSafeIntegers = get(fiber.services, SafeIntegers);
      statement.safeIntegers(useSafeIntegers);
      try {
        return succeed(statement.all(...params) ?? []);
      } catch (cause) {
        return fail(new SqlError({
          cause,
          message: "Failed to execute statement"
        }));
      }
    });
    const runValues = (sql, params = []) => withFiber((fiber) => {
      const statement = db.query(sql);
      const useSafeIntegers = get(fiber.services, SafeIntegers);
      statement.safeIntegers(useSafeIntegers);
      try {
        return succeed(statement.values(...params) ?? []);
      } catch (cause) {
        return fail(new SqlError({
          cause,
          message: "Failed to execute statement"
        }));
      }
    });
    return identity({
      execute(sql, params, transformRows2) {
        return transformRows2 ? map(run(sql, params), transformRows2) : run(sql, params);
      },
      executeRaw(sql, params) {
        return run(sql, params);
      },
      executeValues(sql, params) {
        return runValues(sql, params);
      },
      executeUnprepared(sql, params, transformRows2) {
        return this.execute(sql, params, transformRows2);
      },
      executeStream(_sql, _params) {
        return die("executeStream not implemented");
      },
      export: try_({
        try: () => db.serialize(),
        catch: (cause) => new SqlError({
          cause,
          message: "Failed to export database"
        })
      }),
      loadExtension: (path) => try_({
        try: () => db.loadExtension(path),
        catch: (cause) => new SqlError({
          cause,
          message: "Failed to load extension"
        })
      })
    });
  });
  const semaphore = yield* make2(1);
  const connection = yield* makeConnection;
  const acquirer = semaphore.withPermits(1)(succeed(connection));
  const transactionAcquirer = uninterruptibleMask((restore) => {
    const fiber = getCurrent();
    const scope = getUnsafe(fiber.services, Scope);
    return as(tap(restore(semaphore.take(1)), () => addFinalizer(scope, semaphore.release(1))), connection);
  });
  return Object.assign(yield* make3({
    acquirer,
    compiler,
    transactionAcquirer,
    spanAttributes: [...options.spanAttributes ? Object.entries(options.spanAttributes) : [], [ATTR_DB_SYSTEM_NAME, "sqlite"]],
    transformRows
  }), {
    [TypeId]: TypeId,
    config: options,
    export: flatMap(acquirer, (_) => _.export),
    loadExtension: (path) => flatMap(acquirer, (_) => _.loadExtension(path))
  });
});
var layerConfig = (config) => effectServices(unwrap(config).asEffect().pipe(flatMap(make4), map((client) => make(SqliteClient, client).pipe(add(SqlClient, client))))).pipe(provide(layer));
var layer2 = (config) => effectServices(map(make4(config), (client) => make(SqliteClient, client).pipe(add(SqlClient, client)))).pipe(provide(layer));
export {
  make4 as make,
  layerConfig,
  layer2 as layer,
  TypeId,
  SqliteClient
};
