import { Utils } from '@nativescript/core';
import { serialize, serializeArray, serializeObject } from '../utils';
export function parseOperator(operator) {
    switch (operator) {
        case 'eq':
            return 0 /* NSCSupabasePostgresFilterBuilderOperator.Eq */;
        case 'neq':
            return 1 /* NSCSupabasePostgresFilterBuilderOperator.Neq */;
        case 'gt':
            return 2 /* NSCSupabasePostgresFilterBuilderOperator.Gt */;
        case 'gte':
            return 3 /* NSCSupabasePostgresFilterBuilderOperator.Gte */;
        case 'lt':
            return 4 /* NSCSupabasePostgresFilterBuilderOperator.Lt */;
        case 'lte':
            return 5 /* NSCSupabasePostgresFilterBuilderOperator.Lte */;
        case 'like':
            return 6 /* NSCSupabasePostgresFilterBuilderOperator.Like */;
        case 'ilike':
            return 7 /* NSCSupabasePostgresFilterBuilderOperator.Ilike */;
        case 'is':
            return 8 /* NSCSupabasePostgresFilterBuilderOperator.Is */;
        case 'in':
            return 9 /* NSCSupabasePostgresFilterBuilderOperator.In */;
        case 'cs':
            return 10 /* NSCSupabasePostgresFilterBuilderOperator.Cs */;
        case 'cd':
            return 11 /* NSCSupabasePostgresFilterBuilderOperator.Cd */;
        case 'sl':
            return 12 /* NSCSupabasePostgresFilterBuilderOperator.Sl */;
        case 'sr':
            return 13 /* NSCSupabasePostgresFilterBuilderOperator.Sr */;
        case 'nxl':
            return 14 /* NSCSupabasePostgresFilterBuilderOperator.Nxl */;
        case 'nxr':
            return 15 /* NSCSupabasePostgresFilterBuilderOperator.Nxr */;
        case 'adj':
            return 16 /* NSCSupabasePostgresFilterBuilderOperator.Adj */;
        case 'ov':
            return 17 /* NSCSupabasePostgresFilterBuilderOperator.Ov */;
        case 'fts':
            return 18 /* NSCSupabasePostgresFilterBuilderOperator.Fts */;
        case 'plfts':
            return 19 /* NSCSupabasePostgresFilterBuilderOperator.Plfts */;
        case 'phfts':
            return 20 /* NSCSupabasePostgresFilterBuilderOperator.Phfts */;
        case 'wfts':
            return 21 /* NSCSupabasePostgresFilterBuilderOperator.Wfts */;
    }
}
export class PostgresTransformBuilder {
    static fromNative(value) {
        const builder = new PostgresTransformBuilder();
        builder.native_ = value;
        return builder;
    }
    get native() {
        return this.native_;
    }
    csv() {
        this.native_ = this.native.csv();
        return this;
    }
    limit(count) {
        this.native_ = this.native.limit(count);
        return this;
    }
    maybeSingle() {
        this.native_ = this.native.maybeSingle();
        return this;
    }
    order(column, options) {
        this.native_ = this.native.order(column, options?.ascending ?? true, options?.nullsFirst ?? false, options?.referencedTable ?? null);
        return this;
    }
    range(from, to, referencedTable) {
        this.native_ = this.native.range(from, to, referencedTable ?? null);
        return this;
    }
    select(columns) {
        this.native_ = this.native.select(columns);
        return this;
    }
    single() {
        this.native_ = this.native.single();
        return this;
    }
    then(onfulfilled, onrejected) {
        return new Promise((resolve, reject) => {
            this.native.execute(null, (data, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(Utils.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_ = this.native.containedBy(column, serialize(value));
        return this;
    }
    contains(column, value) {
        this.native_ = this.native.contains(column, serialize(value));
        return this;
    }
    eq(column, value) {
        this.native_ = this.native.eq(column, serialize(value));
        return this;
    }
    gt(column, value) {
        this.native_ = this.native.gt(column, serialize(value));
        return this;
    }
    gte(column, value) {
        this.native_ = this.native.gte(column, serialize(value));
        return this;
    }
    ilike(column, pattern) {
        this.native_ = this.native.ilike(column, pattern);
        return this;
    }
    in(column, pattern) {
        this.native_ = this.native.in(column, serializeArray(pattern));
        return this;
    }
    is(column) {
        this.native_ = this.native.is(column);
        return this;
    }
    isValue(column, value) {
        this.native_ = this.native.isValue(column, value);
        return this;
    }
    like(column, pattern) {
        this.native_ = this.native.like(column, pattern);
        return this;
    }
    lt(column, value) {
        this.native_ = this.native.lt(column, serialize(value));
        return this;
    }
    lte(column, value) {
        this.native_ = this.native.lte(column, serialize(value));
        return this;
    }
    match(query) {
        this.native_ = this.native.match(serializeObject(query));
        return this;
    }
    neq(column, value) {
        this.native_ = 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_ = this.native.notWithColumn(column, nativeOperator, serialize(value));
        return this;
    }
    or(filters, options) {
        this.native_ = this.native.orWithFilters(filters, options?.foreignTable ?? options?.referencedTable ?? null);
        return this;
    }
    overlaps(column, value) {
        this.native_ = this.native.overlaps(column, serialize(value));
        return this;
    }
    rangeAdjacent(column, range) {
        this.native_ = this.native.rangeAdjacent(column, serialize(range));
        return this;
    }
    rangeGt(column, range) {
        this.native_ = this.native.rangeGt(column, serialize(range));
        return this;
    }
    rangeGte(column, range) {
        this.native_ = this.native.rangeGte(column, serialize(range));
        return this;
    }
    rangeLt(column, range) {
        this.native_ = this.native.rangeLt(column, serialize(range));
        return this;
    }
    rangeLte(column, range) {
        this.native_ = this.native.rangeLte(column, serialize(range));
        return this;
    }
    textSearch(column, query, options) {
        let nativeType;
        switch (options?.type) {
            case 'plain':
                nativeType = 0 /* NSCSupabasePostgresTextSearchType.Plain */;
                break;
            case 'phrase':
                nativeType = 1 /* NSCSupabasePostgresTextSearchType.Phrase */;
                break;
            case 'websearch':
                nativeType = 2 /* NSCSupabasePostgresTextSearchType.Websearch */;
                break;
        }
        if (nativeType !== undefined) {
            this.native_ = this.native.textSearchType(column, serialize(query), options?.config ?? null, nativeType);
        }
        else {
            this.native_ = this.native.textSearch(column, serialize(query), options?.config ?? null);
        }
        return this;
    }
}
function parseCount(count) {
    switch (count) {
        case 'exact':
            return 1 /* NSCSupabasePostgresCountOption.Exact */;
        case 'planned':
            return 2 /* NSCSupabasePostgresCountOption.Planned */;
        case 'estimated':
            return 3 /* NSCSupabasePostgresCountOption.Estimated */;
        default:
            return 0 /* NSCSupabasePostgresCountOption.None */;
    }
}
export class SupabasePostgresClient {
    get native() {
        return this.native_;
    }
    static fromNative(value) {
        const client = new SupabasePostgresClient();
        client.native_ = value;
        return client;
    }
    from(table) {
        return SupabasePostgresQueryBuilder.fromNative(this.native.from(table));
    }
    schema(schema) {
        return SupabasePostgresClient.fromNative(this.native.schema(schema));
    }
}
export class SupabasePostgresQueryBuilder {
    get native() {
        return this.native_;
    }
    static fromNative(value) {
        const client = new SupabasePostgresQueryBuilder();
        client.native_ = value;
        return client;
    }
    insert(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.insertError(serialize(values), parseCount(options?.count)));
    }
    update(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.updateError(serializeObject(values), parseCount(options?.count)));
    }
    upsert(values, options) {
        return PostgresFilterBuilder.fromNative(this.native.upsertError(serialize(values), options?.onConflict ?? null, parseCount(options?.count), options?.ignoreDuplicates ?? false));
    }
    delete(options) {
        return PostgresFilterBuilder.fromNative(this.native.delete(parseCount(options?.count)));
    }
    select(columns = '*', { head = false, count, } = {}) {
        return PostgresFilterBuilder.fromNative(this.native.selectHead(columns ?? '*', parseCount(count), head ?? false));
    }
}
//# sourceMappingURL=index.ios.js.map