export function numberHasDecimals(value) {
    return !(value % 1 === 0);
}
export function numberIs64Bit(value) {
    return value < -Math.pow(2, 31) + 1 || value > Math.pow(2, 31) - 1;
}
export function dataDeserialize(nativeData) {
    if (nativeData === null || typeof nativeData !== 'object') {
        return nativeData;
    }
    let store;
    switch (nativeData.getClass().getName()) {
        case 'java.lang.String': {
            return String(nativeData);
        }
        case 'java.lang.Boolean': {
            return String(nativeData) === 'true';
        }
        case 'java.lang.Float':
        case 'java.lang.Integer':
        case 'java.lang.Long':
        case 'java.lang.Double':
        case 'java.lang.Short': {
            return Number(nativeData);
        }
        case 'kotlinx.serialization.json.JsonArray': {
            store = JSON.parse(nativeData.toString());
            break;
        }
        case 'org.json.JSONArray': {
            store = [];
            for (let j = 0; j < nativeData.length(); j++) {
                store[j] = dataDeserialize(nativeData.get(j));
            }
            break;
        }
        case 'kotlinx.serialization.json.JsonObject': {
            store = JSON.parse(nativeData.toString());
            break;
        }
        case 'org.json.JSONObject': {
            store = {};
            const i = nativeData.keys();
            let key;
            while (i.hasNext()) {
                key = i.next();
                store[key] = dataDeserialize(nativeData.get(key));
            }
            break;
        }
        case 'androidx.collection.SimpleArrayMap': {
            const count = nativeData.size();
            for (let l = 0; l < count; l++) {
                const key = nativeData.keyAt(l);
                store[key] = dataDeserialize(nativeData.get(key));
            }
            break;
        }
        case 'androidx.collection.ArrayMap':
        case 'android.os.Bundle':
        case 'java.util.HashMap':
        case 'java.util.Map': {
            store = {};
            const keys = nativeData.keySet().toArray();
            for (let k = 0; k < keys.length; k++) {
                const key = keys[k];
                store[key] = dataDeserialize(nativeData.get(key));
            }
            break;
        }
        default:
            if (typeof nativeData === 'object' && nativeData instanceof java.util.List) {
                const array = [];
                const size = nativeData.size();
                for (let i = 0, n = size; i < n; i++) {
                    array[i] = dataDeserialize(nativeData.get(i));
                }
                store = array;
            }
            else {
                store = null;
            }
            break;
    }
    return store;
}
export function serialize(data, wrapPrimitives) {
    let store;
    switch (typeof data) {
        case 'string':
        case 'boolean': {
            if (wrapPrimitives) {
                if (typeof data === 'string') {
                    return new java.lang.String(data);
                }
                return new java.lang.Boolean(data);
            }
            return data;
        }
        case 'number': {
            const hasDecimals = numberHasDecimals(data);
            if (numberIs64Bit(data)) {
                if (hasDecimals) {
                    return java.lang.Double.valueOf(data);
                }
                else {
                    return java.lang.Long.valueOf(data);
                }
            }
            else {
                if (hasDecimals) {
                    return java.lang.Float.valueOf(data);
                }
                else {
                    return java.lang.Integer.valueOf(data);
                }
            }
        }
        case 'object': {
            if (!data) {
                return null;
            }
            if (data instanceof Date) {
                return new java.util.Date(data.getTime());
            }
            if (Array.isArray(data)) {
                store = new org.nativescript.supabase.JsonHelpers.JsonArray();
                data.forEach((item) => store.add(serialize(item, wrapPrimitives)));
                return store;
            }
            if (data.native) {
                return data.native;
            }
            store = new org.nativescript.supabase.JsonHelpers.JsonObject();
            Object.keys(data).forEach((key) => store.put(key, serialize(data[key], wrapPrimitives)));
            return store;
        }
        default:
            return null;
    }
}
export function serializeObject(value, wrapPrimitives) {
    return serialize(value, wrapPrimitives);
}
export function serializeArray(value, wrapPrimitives) {
    return serialize(value, wrapPrimitives);
}
//# sourceMappingURL=index.android.js.map