import { Definitions } from './definition';
import { z } from '@bpinternal/zui';
import { Table } from '@botpress/client';
import { ZuiObjectSchema } from '@botpress/sdk';
import { TableDefinitions } from '../_types/tables';
export declare namespace Typings {
    type ColumnDefinition<TName extends string = string, TSchema extends z.ZodType = z.ZodType> = TSchema extends z.ZodType ? {
        computed: true;
        searchable?: boolean;
        schema: TSchema;
        dependencies: ReadonlyArray<keyof TableDefinitions[TName]['Output']>;
        value: (row: TableDefinitions[TName]['Output']) => Promise<z.output<TSchema>>;
    } | {
        computed?: false;
        searchable?: boolean;
        schema: TSchema;
    } : never;
    type Props<TName extends string = string> = {
        name: TName;
        description?: string;
        factor?: number;
        columns: Record<string, ColumnDefinition<TName> | z.ZodType>;
        keyColumn?: keyof TableDefinitions[TName]['Output'];
        tags?: Record<string, string>;
    };
    const Primitive: "table";
}
type NestedKeyOf<T> = T extends object ? {
    [K in keyof T & string]: K | (T[K] extends (infer U)[] ? K | `${K}.${number}` | (U extends object ? `${K}.${number}.${NestedKeyOf<U>}` : never) : T[K] extends object ? K | `${K}.${NestedKeyOf<T[K]>}` : K);
}[keyof T & string] : never;
export type PrimitiveFilter<T> = {
    $eq?: T;
    $gt?: T;
    $gte?: T;
    $lt?: T;
    $lte?: T;
    $ne?: T;
    $in?: T[];
    $nin?: T[];
    $exists?: boolean;
    $regex?: string;
    $options?: 'i' | 'c';
};
export type LogicalFilter<TColumns, TName extends string = string> = {
    $and?: TableFilter<TColumns, TName>[];
    $or?: TableFilter<TColumns, TName>[];
    $not?: TableFilter<TColumns, TName>;
};
export type TableFilter<TColumns, TName extends string = string> = {
    [K in keyof TColumns]?: TColumns[K] | PrimitiveFilter<TColumns[K]>;
} & {
    [K in NestedKeyOf<TColumns>]?: any | PrimitiveFilter<any>;
} & LogicalFilter<TColumns, TName>;
export type OrderDirection = 'asc' | 'desc';
type NumberAggregations = 'key' | 'count' | 'sum' | 'avg' | 'max' | 'min' | 'unique';
type StringAggregations = 'key' | 'count' | 'max' | 'min' | 'unique';
type DateAggregations = 'key' | 'count' | 'max' | 'min' | 'unique';
type BooleanAggregations = 'key' | 'count' | 'unique';
type ArrayAggregations = 'key' | 'count' | 'unique';
type ObjectAggregations = 'key' | 'count' | 'unique';
type GetAggregations<T> = T extends number ? NumberAggregations : T extends string ? StringAggregations : T extends Date ? DateAggregations : T extends boolean ? BooleanAggregations : T extends any[] ? ArrayAggregations : T extends object ? ObjectAggregations : 'key' | 'count' | 'unique';
type GetTypeAtPath<T, Path> = Path extends `${infer K}.${infer Rest}` ? K extends keyof T ? GetTypeAtPath<T[K], Rest> : any : Path extends keyof T ? T[Path] : any;
export type TableGroup<TColumns> = {
    [K in NestedKeyOf<TColumns>]?: GetAggregations<GetTypeAtPath<TColumns, K>> | GetAggregations<GetTypeAtPath<TColumns, K>>[];
};
type CamelCasePath<S extends string> = S extends `${infer A}.${infer B}` ? `${A}${Capitalize<CamelCasePath<B>>}` : S;
type AggregatedFieldName<Path extends string, Op extends string> = Op extends 'key' ? `${CamelCasePath<Path>}Key` : `${CamelCasePath<Path>}${Capitalize<Op>}`;
type AggregationResultType<T, Op extends string> = Op extends 'count' ? number : Op extends 'sum' ? number : Op extends 'avg' ? number : Op extends 'max' ? T : Op extends 'min' ? T : Op extends 'unique' ? T[] : Op extends 'key' ? T : never;
type GroupResultShape<TColumns, TGroup> = TGroup extends TableGroup<TColumns> ? {
    [K in keyof TGroup as TGroup[K] extends string ? AggregatedFieldName<K & string, TGroup[K]> : TGroup[K] extends readonly string[] ? TGroup[K][number] extends string ? AggregatedFieldName<K & string, TGroup[K][number]> : never : never]: TGroup[K] extends string ? AggregationResultType<GetTypeAtPath<TColumns, K & string>, TGroup[K]> : TGroup[K] extends readonly string[] ? TGroup[K][number] extends string ? AggregationResultType<GetTypeAtPath<TColumns, K & string>, TGroup[K][number]> : never : never;
} : never;
export interface FindRowsOptions<TName extends string> {
    filter?: TableFilter<TableDefinitions[TName]['Input'], TName>;
    orderBy?: keyof TableDefinitions[TName]['Output'];
    orderDirection?: OrderDirection;
    limit?: number;
    offset?: number;
    search?: string;
    group?: TableGroup<TableDefinitions[TName]['Output']>;
}
type Row<Shape> = {
    id: number;
    createdAt: string;
    updatedAt: string;
} & Shape;
type SearchResult<Shape> = {
    id: number;
    createdAt: string;
    updatedAt: string;
    similarity: number;
} & Shape;
export declare class BaseTable<TName extends string = string> implements Definitions.Primitive {
    readonly name: TName;
    readonly description?: string;
    readonly factor: number;
    readonly columns: Record<string, Typings.ColumnDefinition<TName>>;
    readonly schema: ZuiObjectSchema;
    readonly type: Definitions.PrimitiveDefinition['type'];
    readonly keyColumn?: string;
    readonly tags?: Record<string, string>;
    readonly nullableColumns: Set<string>;
    readonly searchableColumns: Set<string>;
    readonly computedColumns: Set<string>;
    private get client();
    constructor(props: Typings.Props<TName>);
    getRow(props: {
        id: number;
    }): Promise<Row<TableDefinitions[TName]['Output']>>;
    findRows<TOptions extends FindRowsOptions<TName>>(options?: TOptions): Promise<{
        rows: TOptions['group'] extends TableGroup<TableDefinitions[TName]['Output']> ? GroupResultShape<TableDefinitions[TName]['Output'], TOptions['group']>[] : SearchResult<TableDefinitions[TName]['Output']>[];
        hasMore: boolean;
        limit: number;
        offset: number;
    }>;
    createRows({ rows, waitComputed, }: {
        rows: TableDefinitions[TName]['Input'][];
        waitComputed?: boolean;
    }): Promise<{
        rows: Row<TableDefinitions[TName]['Output']>[];
        errors?: string[];
        warnings?: string[];
    }>;
    deleteAllRows(): Promise<{
        deletedRows: number;
    }>;
    deleteRowIds(ids: number[]): Promise<{
        deletedRows: number;
    }>;
    deleteRows(filter: FindRowsOptions<TName>['filter']): Promise<{
        deletedRows: number;
    }>;
    updateRows(props: {
        rows: (Partial<TableDefinitions[TName]['Input']> & {
            id: number;
        })[];
        waitComputed?: boolean;
    }): Promise<{
        rows: Row<TableDefinitions[TName]['Output']>[];
        errors?: string[];
        warnings?: string[];
    }>;
    upsertRows(props: {
        rows: (Partial<TableDefinitions[TName]['Input']> & {
            id?: number;
        })[];
        waitComputed?: boolean;
        keyColumn?: keyof TableDefinitions[TName]['Input'];
    }): Promise<{
        updated: Row<TableDefinitions[TName]['Output']>[];
        inserted: Row<TableDefinitions[TName]['Output']>[];
        errors?: string[];
        warnings?: string[];
    }>;
    getTable(): Promise<{
        rows: number;
        stale: number;
        indexing: number;
        table: Table;
    }>;
}
export { BaseTable as Table };
//# sourceMappingURL=table.d.ts.map