import { serialize, serializeObject, dataDeserialize } from '../utils';
export function parseOperator(operator) {
    switch (operator) {
        case 'eq':
        case 'neq':
        case 'gt':
        case 'gte':
        case 'lt':
        case 'lte':
        case 'like':
        case 'ilike':
        case 'is':
        case 'in':
        case 'cs':
        case 'cd':
        case 'sl':
        case 'sr':
        case 'nxl':
        case 'nxr':
        case 'adj':
        case 'ov':
        case 'fts':
        case 'plfts':
        case 'phfts':
        case 'wfts':
            return operator;
        default:
            return undefined;
    }
}
function parseCount(count) {
    switch (count) {
        case 'exact':
            return org.nativescript.supabase.SupabaseClient.CountOption.exact;
        case 'planned':
            return org.nativescript.supabase.SupabaseClient.CountOption.planned;
        case 'estimated':
            return org.nativescript.supabase.SupabaseClient.CountOption.estimated;
        default:
            return null;
    }
}
export class PostgresTransformBuilder {
    static fromNative(value) {
        const builder = new PostgresTransformBuilder();
        builder.native_ = value;
        return builder;
    }
    get native() {
        return this.native_;
    }
    csv() {
        this.native.csv();
        return this;
    }
    limit(count) {
        this.native.limit(count);
        return this;
    }
    maybeSingle() {
        this.native.csv();
        return this;
    }
    order(column, options) {
        this.native.order(column, serialize(options?.ascending ?? true, true), serialize(options?.nullsFirst ?? false, true), options?.referencedTable ?? null);
        return this;
    }
    range(from, to, referencedTable) {
        this.native.range(from, to, referencedTable ?? null);
        return this;
    }
    select(columns) {
        this.native.select(columns ?? null);
        return this;
    }
    single() {
        this.native.single();
        return this;
    }
    then(onfulfilled, onrejected) {
        return new Promise((resolve, reject) => {
            this.native.execute(new kotlin.jvm.functions.Function1({
                invoke(data) {
                    resolve(dataDeserialize(data));
                },
            }));
        }).then(onfulfilled, onrejected);
    }
}
export class PostgresFilterBuilder extends PostgresTransformBuilder {
    static fromNative(value) {
        const builder = new PostgresFilterBuilder();
        builder.native_ = value;
        return builder;
    }
    get native() {
        return this.native_;
    }
    containedBy(column, value) {
        this.native.containedBy(column, serialize(value));
        return this;
    }
    contains(column, value) {
        this.native.contains(column, serialize(value));
        return this;
    }
    eq(column, value) {
        this.native.eq(column, serialize(value));
        return this;
    }
    gt(column, value) {
        this.native.gt(column, serialize(value));
        return this;
    }
    gte(column, value) {
        this.native.gte(column, serialize(value));
        return this;
    }
    ilike(column, pattern) {
        this.native.ilike(column, pattern);
        return this;
    }
    in(column, value) {
        this.native.in(column, serialize(value));
        return this;
    }
    is(column, value = null) {
        if (typeof value === 'boolean') {
            this.native.eq(column, serialize(value));
        }
        else {
            this.native.eq(column, null);
        }
        return this;
    }
    like(column, pattern) {
        this.native.like(column, pattern);
        return this;
    }
    lt(column, value) {
        this.native.lt(column, serialize(value));
        return this;
    }
    lte(column, value) {
        this.native.lte(column, serialize(value));
        return this;
    }
    match(query) {
        this.native.match(serialize(query));
        return this;
    }
    neq(column, value) {
        this.native.neq(column, serialize(value));
        return this;
    }
    not(column, operator, value) {
        const nativeOperator = parseOperator(operator);
        if (nativeOperator === undefined) {
            throw new Error(`Invalid operator: ${operator}`);
        }
        this.native.not(column, operator, serialize(value));
        return this;
    }
    or(filters, options) {
        // todo implement
        return this;
    }
    overlaps(column, value) {
        this.native.overlaps(column, serialize(value));
        return this;
    }
    rangeAdjacent(column, range) {
        this.native.rangeAdjacent(column, serialize(range));
        return this;
    }
    rangeGt(column, range) {
        this.native.rangeGt(column, serialize(range));
        return this;
    }
    rangeGte(column, range) {
        this.native.rangeGte(column, serialize(range));
        return this;
    }
    rangeLt(column, range) {
        this.native.rangeLt(column, serialize(range));
        return this;
    }
    rangeLte(column, range) {
        this.native.rangeLte(column, serialize(range));
        return this;
    }
    textSearch(column, query, options) {
        this.native.textSearch(column, serialize(query), options?.config ?? null, options?.type ?? null);
        return this;
    }
}
export class SupabasePostgresClient {
    get native() {
        return this.native_;
    }
    static fromNative(value) {
        const client = new SupabasePostgresClient();
        client.native_ = value;
        return client;
    }
    from(table) {
        const ret = new SupabasePostgresQueryBuilder();
        ret.native_ = this.native.from(table);
        return ret;
    }
    schema(schema) {
        return SupabasePostgresClient.fromNative(this.native.schema(schema));
    }
}
export class SupabasePostgresQueryBuilder {
    get native() {
        return this.native_;
    }
    static fromNative(value) {
        const builder = new SupabasePostgresQueryBuilder();
        builder.native_ = value;
        return builder;
    }
    insert(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.insert(serialize(values, true)?.getValue?.() ?? null, serialize(options?.defaultToNull ?? null, true), parseCount(options?.count)));
    }
    update(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.update(serializeObject(values, true)?.getValue?.() ?? null, parseCount(options?.count)));
    }
    upsert(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.upsert(serializeObject(values)?.getValue?.() ?? null, options?.onConflict ?? null, parseCount(options?.count), serialize(options?.ignoreDuplicates ?? null, true)));
    }
    delete(options) {
        return PostgresFilterBuilder.fromNative(this.native.delete(parseCount(options?.count)));
    }
    select(columns = '*', { head = false, count, } = {}) {
        return PostgresFilterBuilder.fromNative(this.native.select(columns, parseCount(count), serialize(head ?? false, true)));
    }
}
//# sourceMappingURL=index.android.js.map