import { r as isInsufficientBalanceError, t as InsufficientBalanceError } from "./errors-8Tiso7LK.js";
import { PgColumn, PgTable } from "drizzle-orm/pg-core";
import { SQL } from "drizzle-orm";
import { PostgresTransaction, Querier, Schema } from "@platform-modules/db";
//#region src/schema.d.ts
declare const ledgerEntries: import("drizzle-orm/pg-core").PgTableWithColumns<{
  name: "ledger_entries";
  schema: undefined;
  columns: {
    id: import("drizzle-orm/pg-core").PgColumn<{
      name: "id";
      tableName: "ledger_entries";
      dataType: "string";
      columnType: "PgUUID";
      data: string;
      driverParam: string;
      notNull: true;
      hasDefault: true;
      isPrimaryKey: true;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    delta: import("drizzle-orm/pg-core").PgColumn<{
      name: "delta";
      tableName: "ledger_entries";
      dataType: "bigint";
      columnType: "PgBigInt64";
      data: bigint;
      driverParam: string;
      notNull: true;
      hasDefault: false;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    currency: import("drizzle-orm/pg-core").PgColumn<{
      name: "currency";
      tableName: "ledger_entries";
      dataType: "string";
      columnType: "PgText";
      data: string;
      driverParam: string;
      notNull: false;
      hasDefault: false;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: [string, ...string[]];
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    reason: import("drizzle-orm/pg-core").PgColumn<{
      name: "reason";
      tableName: "ledger_entries";
      dataType: "string";
      columnType: "PgText";
      data: string;
      driverParam: string;
      notNull: true;
      hasDefault: false;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: [string, ...string[]];
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    ref: import("drizzle-orm/pg-core").PgColumn<{
      name: "ref";
      tableName: "ledger_entries";
      dataType: "json";
      columnType: "PgJsonb";
      data: Record<string, unknown> | null;
      driverParam: unknown;
      notNull: false;
      hasDefault: false;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {
      $type: Record<string, unknown> | null;
    }>;
    idempotencyKey: import("drizzle-orm/pg-core").PgColumn<{
      name: "idempotency_key";
      tableName: "ledger_entries";
      dataType: "string";
      columnType: "PgText";
      data: string;
      driverParam: string;
      notNull: true;
      hasDefault: false;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: [string, ...string[]];
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    createdAt: import("drizzle-orm/pg-core").PgColumn<{
      name: "created_at";
      tableName: "ledger_entries";
      dataType: "date";
      columnType: "PgTimestamp";
      data: Date;
      driverParam: string;
      notNull: true;
      hasDefault: true;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
  };
  dialect: "pg";
}>;
/** Optional default balance projection — hosts may use their own column instead. */
declare const walletBalances: import("drizzle-orm/pg-core").PgTableWithColumns<{
  name: "wallet_balances";
  schema: undefined;
  columns: {
    ownerId: import("drizzle-orm/pg-core").PgColumn<{
      name: "owner_id";
      tableName: "wallet_balances";
      dataType: "string";
      columnType: "PgText";
      data: string;
      driverParam: string;
      notNull: true;
      hasDefault: false;
      isPrimaryKey: true;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: [string, ...string[]];
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    balance: import("drizzle-orm/pg-core").PgColumn<{
      name: "balance";
      tableName: "wallet_balances";
      dataType: "bigint";
      columnType: "PgBigInt64";
      data: bigint;
      driverParam: string;
      notNull: true;
      hasDefault: true;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
    updatedAt: import("drizzle-orm/pg-core").PgColumn<{
      name: "updated_at";
      tableName: "wallet_balances";
      dataType: "date";
      columnType: "PgTimestamp";
      data: Date;
      driverParam: string;
      notNull: true;
      hasDefault: true;
      isPrimaryKey: false;
      isAutoincrement: false;
      hasRuntimeDefault: false;
      enumValues: undefined;
      baseColumn: never;
      identity: undefined;
      generated: undefined;
    }, {}, {}>;
  };
  dialect: "pg";
}>;
declare const ledgerSchema: {
  ledgerEntries: import("drizzle-orm/pg-core").PgTableWithColumns<{
    name: "ledger_entries";
    schema: undefined;
    columns: {
      id: import("drizzle-orm/pg-core").PgColumn<{
        name: "id";
        tableName: "ledger_entries";
        dataType: "string";
        columnType: "PgUUID";
        data: string;
        driverParam: string;
        notNull: true;
        hasDefault: true;
        isPrimaryKey: true;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      delta: import("drizzle-orm/pg-core").PgColumn<{
        name: "delta";
        tableName: "ledger_entries";
        dataType: "bigint";
        columnType: "PgBigInt64";
        data: bigint;
        driverParam: string;
        notNull: true;
        hasDefault: false;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      currency: import("drizzle-orm/pg-core").PgColumn<{
        name: "currency";
        tableName: "ledger_entries";
        dataType: "string";
        columnType: "PgText";
        data: string;
        driverParam: string;
        notNull: false;
        hasDefault: false;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: [string, ...string[]];
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      reason: import("drizzle-orm/pg-core").PgColumn<{
        name: "reason";
        tableName: "ledger_entries";
        dataType: "string";
        columnType: "PgText";
        data: string;
        driverParam: string;
        notNull: true;
        hasDefault: false;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: [string, ...string[]];
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      ref: import("drizzle-orm/pg-core").PgColumn<{
        name: "ref";
        tableName: "ledger_entries";
        dataType: "json";
        columnType: "PgJsonb";
        data: Record<string, unknown> | null;
        driverParam: unknown;
        notNull: false;
        hasDefault: false;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {
        $type: Record<string, unknown> | null;
      }>;
      idempotencyKey: import("drizzle-orm/pg-core").PgColumn<{
        name: "idempotency_key";
        tableName: "ledger_entries";
        dataType: "string";
        columnType: "PgText";
        data: string;
        driverParam: string;
        notNull: true;
        hasDefault: false;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: [string, ...string[]];
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      createdAt: import("drizzle-orm/pg-core").PgColumn<{
        name: "created_at";
        tableName: "ledger_entries";
        dataType: "date";
        columnType: "PgTimestamp";
        data: Date;
        driverParam: string;
        notNull: true;
        hasDefault: true;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
    };
    dialect: "pg";
  }>;
  walletBalances: import("drizzle-orm/pg-core").PgTableWithColumns<{
    name: "wallet_balances";
    schema: undefined;
    columns: {
      ownerId: import("drizzle-orm/pg-core").PgColumn<{
        name: "owner_id";
        tableName: "wallet_balances";
        dataType: "string";
        columnType: "PgText";
        data: string;
        driverParam: string;
        notNull: true;
        hasDefault: false;
        isPrimaryKey: true;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: [string, ...string[]];
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      balance: import("drizzle-orm/pg-core").PgColumn<{
        name: "balance";
        tableName: "wallet_balances";
        dataType: "bigint";
        columnType: "PgBigInt64";
        data: bigint;
        driverParam: string;
        notNull: true;
        hasDefault: true;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
      updatedAt: import("drizzle-orm/pg-core").PgColumn<{
        name: "updated_at";
        tableName: "wallet_balances";
        dataType: "date";
        columnType: "PgTimestamp";
        data: Date;
        driverParam: string;
        notNull: true;
        hasDefault: true;
        isPrimaryKey: false;
        isAutoincrement: false;
        hasRuntimeDefault: false;
        enumValues: undefined;
        baseColumn: never;
        identity: undefined;
        generated: undefined;
      }, {}, {}>;
    };
    dialect: "pg";
  }>;
};
type LedgerSchema = typeof ledgerSchema;
//#endregion
//#region src/types.d.ts
type LedgerRef = Record<string, unknown> | null;
type AppendEntryInput = {
  key: string;
  delta: bigint;
  currency?: string | null;
  reason: string;
  ref?: LedgerRef;
};
type AppendEntryResult = {
  inserted: boolean;
  /** The inserted entry's uuid; null when the key already existed (onConflictDoNothing returned no row). */
  id: string | null;
};
/** Default module-owned wallet projection (optional — host may ignore). */
type WalletBalanceTarget = {
  kind: 'wallet';
  ownerId: string;
};
/** Host-owned balance column folded onto an existing domain row (column-balance pattern). */
type ColumnBalanceTarget = {
  kind: 'column';
  table: PgTable;
  where: SQL;
  balanceColumn: PgColumn;
};
type BalanceTarget = WalletBalanceTarget | ColumnBalanceTarget;
type DebitInput = {
  key: string;
  target: BalanceTarget;
  amount: bigint;
  reason: string;
  ref?: LedgerRef;
};
type DebitResult = {
  inserted: boolean;
  balance: bigint;
};
type DebitWithReadLock = {
  table: PgTable;
  where: SQL;
};
type DebitWithReadInput = {
  key: string;
  reason: string;
  ref?: LedgerRef;
  lock: DebitWithReadLock;
};
type DebitWithReadPlan<S extends Schema = Record<string, never>> = {
  delta: bigint;
  apply: (tx: Querier<S>) => Promise<void>;
};
type DebitWithReadFn<S extends Schema, TRow extends Record<string, unknown>> = (rows: TRow[]) => DebitWithReadPlan<S> | Promise<DebitWithReadPlan<S>>;
type DebitWithReadResult = {
  inserted: boolean;
  /** The inserted entry's uuid; null when the key already existed. */
  id: string | null;
};
type LedgerEntry = {
  id: string;
  delta: bigint;
  currency: string | null;
  reason: string;
  ref: LedgerRef;
  idempotencyKey: string;
  createdAt: Date;
};
//#endregion
//#region src/append.d.ts
declare function appendEntry<S extends LedgerSchema>(tx: Querier<S>, input: AppendEntryInput): Promise<AppendEntryResult>;
//#endregion
//#region src/balance.d.ts
declare function getBalance<S extends LedgerSchema>(querier: Querier<S>, target: BalanceTarget): Promise<bigint>;
//#endregion
//#region src/debit.d.ts
/**
 * Optimistic guarded debit: idempotent ledger insert FIRST, then a guarded
 * `UPDATE … WHERE balance >= amount`. On a duplicate key (retry) the balance
 * mutation is skipped; on insufficient balance it throws and the just-inserted
 * ledger row must roll back.
 *
 * MUST run inside a transaction (`db.transaction((tx) => debit(tx, …))`). The
 * insert-first/throw-on-insufficient atomicity depends on the throw rolling the
 * ledger row back — passing a non-transactional `Querier` autocommits the insert
 * and leaves an orphan entry with the balance unmutated.
 */
declare function debit<S extends LedgerSchema>(tx: PostgresTransaction<S>, input: DebitInput): Promise<DebitResult>;
//#endregion
//#region src/debit-with-read.d.ts
/**
 * Pessimistic debit: `SELECT … FOR UPDATE` the target row(s), let the caller
 * `fn` decide sufficiency/split from the locked rows, then idempotent ledger
 * insert FIRST and apply the caller's mutation. On a duplicate key (retry) the
 * mutation is skipped; a caller `fn` that throws (insufficient) leaves no
 * partial state.
 *
 * MUST run inside a transaction (`db.transaction((tx) => debitWithRead(tx, …))`).
 * The `FOR UPDATE` lock and the insert-first/rollback atomicity only hold inside
 * a transaction — a non-transactional `Querier` neither holds the lock nor rolls
 * back the inserted ledger row if `fn` or `apply` throws.
 */
declare function debitWithRead<S extends Schema & LedgerSchema, TRow extends Record<string, unknown>>(tx: PostgresTransaction<S>, input: DebitWithReadInput, fn: DebitWithReadFn<S, TRow>): Promise<DebitWithReadResult>;
//#endregion
export { type AppendEntryInput, type AppendEntryResult, type BalanceTarget, type ColumnBalanceTarget, type DebitInput, type DebitResult, type DebitWithReadFn, type DebitWithReadInput, type DebitWithReadLock, type DebitWithReadPlan, type DebitWithReadResult, InsufficientBalanceError, type LedgerEntry, type LedgerRef, type LedgerSchema, type WalletBalanceTarget, appendEntry, debit, debitWithRead, getBalance, isInsufficientBalanceError, ledgerEntries, ledgerSchema, walletBalances };