import { SQL, Subquery, SQLWrapper, Table } from "drizzle-orm";
import { AnyPgColumn, AnyPgTable, PgColumn } from "drizzle-orm/pg-core";
import { AnySQLiteColumn, SQLiteColumn, SQLiteTable } from "drizzle-orm/sqlite-core";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import { LibSQLDatabase } from "drizzle-orm/libsql";
import postgres from "postgres";
import { Client as LibSqlClient } from "@libsql/client";
import { WithIndexer } from "../../../fd-toolbox-core/types/with-indexer";
import { wellKnownPositions } from "@base-repo/constants/tools-constants";
import { mutationActionNames } from "@base-repo/constants/internal-common-strings";
type InsertType = "insert";
type UpdateType = typeof mutationActionNames.update;
type DeleteType = typeof mutationActionNames.delete;
/** @monorepo-internal */
export type DbSchema = Record<string, AnyTable>;
type PgDb = PostgresJsDatabase<DbSchema>;
type SqliteDb = LibSQLDatabase<DbSchema>;
export type DbInstance = PgDb | SqliteDb;
export type DbClient = postgres.Sql | LibSqlClient;
/** @monorepo-internal */
export interface DbQuery<T = SelectQuery> {
    query: T;
}
/** @monorepo-internal */
export type AnyTable = (AnyPgTable | SQLiteTable) & Table;
export type QuerySource = AnyTable | Subquery;
/** @monorepo-internal */
export type AnyColumn = AnyPgColumn | AnySQLiteColumn;
export type ColumnBase = PgColumn | SQLiteColumn;
export type InsertBuilder = ReturnType<PgDb[InsertType]> | ReturnType<SqliteDb[InsertType]>;
export type UpdateBuilder = ReturnType<PgDb[UpdateType]> | ReturnType<SqliteDb[UpdateType]>;
export type DeleteBuilder = ReturnType<PgDb[DeleteType]> | ReturnType<SqliteDb[DeleteType]>;
/** @monorepo-internal */
export interface SortColumn {
    column: AnyColumn;
    joined: boolean;
    ascending: boolean;
    basePropertyName: string;
}
/** @monorepo-internal */
export interface SelectQuery<TResult = unknown> extends SQLWrapper {
    leftJoin(table: QuerySource, condition: SQL): SelectQuery<TResult>;
    innerJoin(table: QuerySource, condition: SQL): SelectQuery<TResult>;
    where(condition: SQL): SelectQuery<TResult>;
    orderBy(...columns: (SQL | AnyColumn)[]): SelectQuery<TResult>;
    groupBy(...columns: (SQL | AnyColumn)[]): SelectQuery<TResult>;
    having(condition: SQL): SelectQuery<TResult>;
    limit(value: number): SelectQuery<TResult>;
    offset(value: number): SelectQuery<TResult>;
    $dynamic(): SelectQuery<TResult>;
    as(alias: string): Subquery;
    then<TResult1 = TResult[], TResult2 = never>(onfulfilled?: (value: TResult[]) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: unknown) => TResult2 | PromiseLike<TResult2>): Promise<TResult1 | TResult2>;
    catch<TResult2 = never>(onrejected?: (reason: unknown) => TResult2 | PromiseLike<TResult2>): Promise<TResult[] | TResult2>;
    finally(onfinally?: () => void): Promise<TResult[]>;
}
/** @monorepo-internal */
export type DrizzleSelects = WithIndexer<DrizzleSelectionValue>;
export type DrizzleSelectionValue = AnyColumn | AnyTable | SQL | SQL.Aliased | Record<string, unknown>;
/** @monorepo-internal */
export type AnySelectedFields = Record<string, DrizzleSelectionValue>;
export type PgSelectionValue = AnyPgColumn | AnyPgTable | SQL | SQL.Aliased;
export type PgSelectedFields = Record<string, PgSelectionValue>;
export type SQLiteSelectionValue = AnySQLiteColumn | SQLiteTable | SQL | SQL.Aliased;
export type SQLiteSelectedFields = Record<string, SQLiteSelectionValue>;
export interface QueryConfig {
    where?: SQL;
    joins?: {
        table: AnyTable | SQL;
        on: SQL;
        joinType: "inner" | typeof wellKnownPositions.left | typeof wellKnownPositions.right | "full";
    }[];
    limit?: number;
    offset?: number;
    orderBy?: (SQL | AnyColumn)[];
    [key: string]: unknown;
}
/** @monorepo-internal */
export type TableWithId = AnyTable & {
    id: AnyColumn;
};
/** @monorepo-internal */
export type TableWithWorkspaceId = AnyTable & {
    workspaceId: AnyColumn;
};
/** @monorepo-internal */
export declare const queryIndexes: {
    readonly tableName: "drizzle:Name";
    readonly relations: "drizzle:PgInlineForeignKeys";
    readonly config: "config";
};
export interface DbAdapter {
    init(schema: DbSchema): void;
    getDb(): DbInstance;
    getClient(): DbClient;
    beginUnsafeTransaction(): Promise<void>;
    rollbackUnsafeTransaction(): Promise<void>;
    createDynamicSelect(table: QuerySource, selection?: AnySelectedFields): SelectQuery;
    getQueryConfig(query: SelectQuery): QueryConfig;
    getLikeExpression(column: AnyColumn | SQL, value: string | SQL): SQL;
    getExtractDateExpression(column: AnyColumn | SQL): SQL;
    leftJoin(query: SelectQuery, table: QuerySource, condition: SQL): SelectQuery;
    innerJoin(query: SelectQuery, table: QuerySource, condition: SQL): SelectQuery;
    orderBy(query: SelectQuery, ...columns: (SQL | AnyColumn)[]): SelectQuery;
    groupBy(query: SelectQuery, ...columns: (SQL | AnyColumn)[]): SelectQuery;
    having(query: SelectQuery, condition: SQL): SelectQuery;
    limit(query: SelectQuery, value: number): SelectQuery;
    offset(query: SelectQuery, value: number): SelectQuery;
    where(query: SelectQuery, condition?: SQL): SelectQuery;
    insert(table: AnyTable): InsertBuilder;
    update(table: AnyTable): UpdateBuilder;
    delete(table: AnyTable): DeleteBuilder;
    createDatabaseIfNotExists(): Promise<void>;
    dropDatabase(): Promise<void>;
    emptyAllTables(): Promise<void>;
    runMigrations(): Promise<void>;
    generateMigrations(name: string): Promise<void>;
}
export {};
