import { dataDeserialize } from '../utils';
export class UserIdentity {
    static fromNative(identity) {
        if (!identity)
            return null;
        const ret = new UserIdentity();
        ret.native = identity;
        return ret;
    }
    get createdAt() {
        return new Date(this.native.getCreatedAt());
    }
    get id() {
        return this.native.getId();
    }
    get identityData() {
        if (!this._identityData) {
            this._identityData = dataDeserialize(this.native.getIdentityData());
        }
        return this._identityData;
    }
    get identityId() {
        return this.native.getIdentityId();
    }
    get lastSignInAt() {
        return new Date(this.native.getLastSignInAt());
    }
    get provider() {
        return this.native.getProvider();
    }
    get updatedAt() {
        return new Date(this.native.getUpdatedAt());
    }
    get userId() {
        return this.native.getUserId();
    }
    toJSON() {
        return {
            createdAt: this.createdAt,
            id: this.id,
            identityData: this.identityData,
            identityId: this.identityId,
            lastSignInAt: this.lastSignInAt,
            provider: this.provider,
            updatedAt: this.updatedAt,
            userId: this.userId,
        };
    }
}
export class Factor {
    static fromNative(factor) {
        if (!factor)
            return null;
        const ret = new Factor();
        ret.native = factor;
        return ret;
    }
    get createdAt() {
        return new Date(this.native.getCreatedAt().toEpochMilliseconds());
    }
    get factorType() {
        return this.native.getFactorType();
    }
    get friendlyName() {
        return this.native.getFriendlyName();
    }
    get id() {
        return this.native.getId();
    }
    get status() {
        // switch (this.native.status) {
        // 	case NSCSupabaseFactorStatus.Verified:
        // 		return 'verified';
        // 	case NSCSupabaseFactorStatus.Unverified:
        // 		return 'unverified';
        // }
        // todo
        return 'verified';
    }
    get updatedAt() {
        return new Date(this.native.getUpdatedAt().toEpochMilliseconds());
    }
    toJSON() {
        return {
            createdAt: this.createdAt,
            factorType: this.factorType,
            friendlyName: this.friendlyName,
            id: this.id,
            status: this.status,
            updatedAt: this.updatedAt,
        };
    }
}
export class User {
    static fromNative(user) {
        if (!user)
            return null;
        const ret = new User();
        ret.native = user;
        return ret;
    }
    get actionLink() {
        return this.native.getActionLink();
    }
    get appMetadata() {
        if (!this._appMetadata) {
            this._appMetadata = dataDeserialize(this.native.getAppMetadata());
        }
        return this._appMetadata;
    }
    get aud() {
        return this.native.getAud();
    }
    get confirmationSentAt() {
        return new Date(this.native.getConfirmationSentAt().toEpochMilliseconds());
    }
    get confirmedAt() {
        return new Date(this.native.getConfirmedAt().toEpochMilliseconds());
    }
    get createdAt() {
        return new Date(this.native.getCreatedAt().toEpochMilliseconds());
    }
    get email() {
        return this.native.getEmail();
    }
    get emailChangeSentAt() {
        const date = this.native.getEmailChangeSentAt();
        if (!date)
            return null;
        return new Date(date.toEpochMilliseconds());
    }
    get emailConfirmedAt() {
        return new Date(this.native.getEmailConfirmedAt().toEpochMilliseconds());
    }
    get factors() {
        if (!this._factors) {
            this._factors = [];
            const factors = this.native.getFactors();
            const count = factors.size();
            for (let i = 0; i < count; i++) {
                this._factors.push(Factor.fromNative(factors.get(i)));
            }
        }
        return this._factors;
    }
    get id() {
        return this.native.getId();
    }
    get identities() {
        // TODO cache
        const ret = [];
        const identities = this.native.getIdentities();
        const count = identities.size();
        for (let i = 0; i < count; i++) {
            ret.push(UserIdentity.fromNative(identities.get(i)));
        }
        return ret;
    }
    get invitedAt() {
        const date = this.native.getInvitedAt();
        if (!date)
            return null;
        return new Date(date.toEpochMilliseconds());
    }
    get isAnonymous() {
        // todo
        return false;
        //return this.native.isAnonymous;
    }
    get lastSignInAt() {
        return new Date(this.native.getLastSignInAt().toEpochMilliseconds());
    }
    get newEmail() {
        return this.native.getNewEmail();
    }
    get phone() {
        return this.native.getPhone();
    }
    get phoneConfirmedAt() {
        const date = this.native.getPhoneConfirmedAt();
        if (!date)
            return null;
        return new Date(date.toEpochMilliseconds());
    }
    get recoverySentAt() {
        const date = this.native.getRecoverySentAt();
        if (!date)
            return null;
        return new Date(date.toEpochMilliseconds());
    }
    get role() {
        return this.native.getRole();
    }
    get updatedAt() {
        return new Date(this.native.getUpdatedAt().toEpochMilliseconds());
    }
    get userMetadata() {
        if (!this._userMetadata) {
            this._userMetadata = dataDeserialize(this.native.getUserMetadata());
        }
        return this._userMetadata;
    }
}
export class Session {
    static fromNative(session) {
        if (!session)
            return null;
        const ret = new Session();
        ret.native_ = session;
        return ret;
    }
    get native() {
        return this.native_;
    }
    get accessToken() {
        return this.native.getAccessToken();
    }
    get expiresAt() {
        return this.native.getExpiresAt().toEpochMilliseconds();
    }
    get expiresIn() {
        return this.native.getExpiresIn();
    }
    get isExpired() {
        return new Date().getTime() >= this.expiresAt;
    }
    get providerRefreshToken() {
        return this.native.getProviderRefreshToken();
    }
    get providerToken() {
        return this.native.getProviderToken();
    }
    get refreshToken() {
        return this.native.getRefreshToken();
    }
    get tokenType() {
        return this.native.getTokenType();
    }
    get user() {
        if (!this._user) {
            this._user = User.fromNative(this.native.getUser());
        }
        return this._user;
    }
    toJSON() {
        return {
            accessToken: this.accessToken,
            expiresAt: this.expiresAt,
            expiresIn: this.expiresIn,
            isExpired: this.isExpired,
            providerRefreshToken: this.providerRefreshToken,
            providerToken: this.providerToken,
            refreshToken: this.refreshToken,
            tokenType: this.tokenType,
            user: this.user,
        };
    }
}
export class AuthMetaSecurity {
    constructor(captchaToken) {
        if (captchaToken instanceof org.nativescript.supabase.SupabaseAuth.AuthMetaSecurity) {
            this.native_ = captchaToken;
        }
        else {
            this.native_ = new org.nativescript.supabase.SupabaseAuth.AuthMetaSecurity(captchaToken);
        }
    }
    static fromNative(meta) {
        if (!meta)
            return null;
        return new AuthMetaSecurity(meta);
    }
    get native() {
        return this.native_;
    }
    get captchaToken() {
        return this.native.getCaptchaToken();
    }
}
export class OpenIDConnectCredentials {
    constructor(provider, idToken) {
        let nativeProvider;
        switch (provider) {
            case 'google':
                nativeProvider = org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.google;
                break;
            case 'apple':
                nativeProvider = org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.apple;
                break;
            case 'azure':
                nativeProvider = org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.azure;
                break;
            case 'facebook':
                nativeProvider = org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.facebook;
                break;
        }
        if (!nativeProvider) {
            throw new Error('Invalid provider');
        }
        this.native_ = new org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentials(nativeProvider, idToken);
    }
    get native() {
        return this.native_;
    }
    get accessToken() {
        return this.native.getAccessToken();
    }
    set accessToken(token) {
        this.native.setAccessToken(token ?? null);
    }
    get gotrueMetaSecurity() {
        return AuthMetaSecurity.fromNative(this.native.getGotrueMetaSecurity());
    }
    set gotrueMetaSecurity(meta) {
        this.native.setGotrueMetaSecurity(meta?.native ?? null);
    }
    get idToken() {
        return this.native.getIdToken();
    }
    set idToken(token) {
        this.native.setIdToken(token);
    }
    get nonce() {
        return this.native.getNonce();
    }
    set nonce(nonce) {
        this.native.setNonce(nonce ?? null);
    }
    get provider() {
        switch (this.native.getProvider()) {
            case org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.google:
                return 'google';
            case org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.apple:
                return 'apple';
            case org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.azure:
                return 'azure';
            case org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.facebook:
                return 'facebook';
        }
    }
    set provider(provider) {
        switch (provider) {
            case 'google':
                this.native.setProvider(org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.google);
                break;
            case 'apple':
                this.native.setProvider(org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.apple);
                break;
            case 'azure':
                this.native.setProvider(org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.azure);
                break;
            case 'facebook':
                this.native.setProvider(org.nativescript.supabase.SupabaseAuth.OpenIDConnectCredentialsProvider.facebook);
                break;
        }
    }
}
export class Admin {
    static fromNative(admin) {
        if (!admin)
            return null;
        const ret = new Admin();
        ret.native_ = admin;
        return ret;
    }
    get native() {
        return this.native_;
    }
}
export class Auth {
    constructor(client) {
        this.client = client;
    }
    get native() {
        return this.client.native.getAuth();
    }
    get currentSession() {
        const currentSession = this.native.getCurrentSession();
        if (!currentSession)
            return null;
        return Session.fromNative(currentSession);
    }
    get currentUser() {
        const user = this.native.getCurrentUser();
        if (!user)
            return null;
        return User.fromNative(user);
    }
    onAuthStateChange(callback) {
        const ref = new WeakRef(this);
        const listener = this.native.addOnAuthStateChange(new kotlin.jvm.functions.Function1({
            invoke(event) {
                let eventName;
                switch (event) {
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.initialSession:
                        eventName = 'INITIAL_SESSION';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.passwordRecovery:
                        eventName = 'PASSWORD_RECOVERY';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.signedIn:
                        eventName = 'SIGNED_IN';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.signedOut:
                        eventName = 'SIGNED_OUT';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.tokenRefreshed:
                        eventName = 'TOKEN_REFRESHED';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.userUpdated:
                        eventName = 'USER_UPDATED';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.userDeleted:
                        eventName = 'USER_DELETED';
                        break;
                    case org.nativescript.supabase.SupabaseAuth.AuthChangeEvent.mfaChallengeVerified:
                        eventName = 'MFA_CHALLENGE_VERIFIED';
                        break;
                }
                const owner = ref.get();
                let session = null;
                if (owner) {
                    session = Session.fromNative(owner.native.getCurrentSession());
                }
                callback(eventName, session);
            },
        }));
        return {
            remove: () => {
                listener.remove();
            },
        };
    }
    getSession() {
        return new Promise((resolve, reject) => {
            this.native.getSession(new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    setSession(currentSession) {
        return new Promise((resolve, reject) => {
            this.native.setSession(currentSession.accessToken, currentSession.refreshToken, new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    signInWithPassword(credentials) {
        return new Promise((resolve, reject) => {
            if (credentials.phone) {
                this.native.signInWithPhone(credentials.phone, credentials.password, credentials.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                    invoke(user, session, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve({
                                user: User.fromNative(user),
                                session: Session.fromNative(session),
                            });
                        }
                    },
                }));
            }
            else {
                this.native.signIn(credentials.email, credentials.password, credentials.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                    invoke(user, session, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve({
                                user: User.fromNative(user),
                                session: Session.fromNative(session),
                            });
                        }
                    },
                }));
            }
        });
    }
    signInAnonymously(credentials) {
        return new Promise((resolve, reject) => {
            this.native.signInAnonymously(credentials?.data ? JSON.stringify(credentials?.data) : null, credentials?.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    signInWithIdToken(credentials) {
        return new Promise((resolve, reject) => {
            this.native.signInWithIdToken(credentials.native, new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    signUp(value) {
        return new Promise((resolve, reject) => {
            if (value.email) {
                this.native.signUp(value.email, value.password, value.captchaToken ?? null, value.options?.data ? JSON.stringify(value.options.data) : null, value.options?.emailRedirectTo ?? null, new kotlin.jvm.functions.Function3({
                    invoke(user, session, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve({ session: session ? Session.fromNative(session) : null, user: User.fromNative(user) });
                        }
                    },
                }));
            }
            else if (value.phone) {
                this.native.signUpPhone(value.phone, value.password, value.captchaToken ?? null, value.options?.data ? JSON.stringify(value.options.data) : null, null, new kotlin.jvm.functions.Function3({
                    invoke(user, session, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve({ session: session ? Session.fromNative(session) : null, user: User.fromNative(user) });
                        }
                    },
                }));
            }
            else {
                reject(new Error('Email or Phone required'));
            }
        });
    }
    signInWithOtp(credentials) {
        return new Promise((resolve, reject) => {
            if (!credentials.email && !credentials.phone) {
                reject(new Error('Email or Phone required'));
                return;
            }
            if (credentials.email) {
                this.native.signInWithOTP(credentials.email, credentials.options?.emailRedirectTo ?? null, !!credentials?.options?.shouldCreateUser ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE, credentials.options?.data ? JSON.stringify(credentials.options.data) : null, credentials.options?.captchaToken ?? null, new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
            else if (credentials.phone) {
                this.native.signInWithOTPPhone(credentials.phone, !!credentials?.options?.shouldCreateUser ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE, credentials.options?.data ? JSON.stringify(credentials.options.data) : null, credentials.options?.captchaToken ?? null, new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
        });
    }
    signInWithOAuth(credentials) {
        return new Promise((resolve, reject) => {
            let nativeProvider;
            switch (credentials.provider) {
                case 'apple':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.apple;
                    break;
                case 'azure':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.azure;
                    break;
                case 'bitbucket':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.bitbucket;
                    break;
                case 'discord':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.discord;
                    break;
                case 'facebook':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.facebook;
                    break;
                case 'figma':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.figma;
                    break;
                case 'github':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.github;
                    break;
                case 'gitlab':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.gitlab;
                    break;
                case 'google':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.google;
                    break;
                case 'kakao':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.kakao;
                    break;
                case 'keycloak':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.keycloak;
                    break;
                case 'linkedin':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.linkedin;
                    break;
                case 'linkedin_oidc':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.linkedinOIDC;
                    break;
                case 'notion':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.notion;
                    break;
                case 'slack':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.slack;
                    break;
                case 'spotify':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.spotify;
                    break;
                case 'twitch':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.twitch;
                    break;
                case 'twitter':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.twitter;
                    break;
                case 'workos':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.workos;
                    break;
                case 'zoom':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.zoom;
                    break;
                case 'fly':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.fly;
                    break;
            }
            if (!nativeProvider) {
                reject(new Error('Invalid provider'));
                return;
            }
            let queryParams = null;
            if (credentials.options?.queryParams) {
                const params = Object.keys(credentials.options.queryParams);
                if (params.length > 0) {
                    queryParams = new java.util.HashMap(params.length);
                    for (const key in credentials.options.queryParams) {
                        queryParams.put(key, credentials.options.queryParams[key]);
                    }
                }
            }
            this.native.signInWithOAuth(nativeProvider, credentials.options?.redirectTo ?? null, credentials.options?.scopes ?? null, queryParams, new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    signInWithSSO(params) {
        return new Promise((resolve, reject) => {
            if (!params.providerId && !params.domain) {
                reject(new Error('providerId or domain required'));
                return;
            }
            if (params.providerId) {
                this.native.signInWithSSOWithProviderId(params.providerId, params.options?.redirectTo ?? null, params.options?.captchaToken ?? null, new kotlin.jvm.functions.Function2({
                    invoke(url, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve(url);
                        }
                    },
                }));
            }
            else if (params.domain) {
                this.native.signInWithSSO(params.domain, params.options?.redirectTo ?? null, params.options?.captchaToken ?? null, new kotlin.jvm.functions.Function2({
                    invoke(url, error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve(url);
                        }
                    },
                }));
            }
        });
    }
    signOut(options) {
        return new Promise((resolve, reject) => {
            let nativeScope;
            switch (options?.scope) {
                case 'global':
                    nativeScope = org.nativescript.supabase.SupabaseAuth.SignOutScope.global;
                    break;
                case 'local':
                    nativeScope = org.nativescript.supabase.SupabaseAuth.SignOutScope.local;
                    break;
                case 'others':
                    nativeScope = org.nativescript.supabase.SupabaseAuth.SignOutScope.others;
                    break;
                default:
                    break;
            }
            if (nativeScope) {
                this.native.signOut(nativeScope, new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
            else {
                this.native.signOut(new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
        });
    }
    resetPasswordForEmail(email, options) {
        return new Promise((resolve, reject) => {
            this.native.resetPasswordForEmail(email, options?.redirectTo ?? null, options?.captchaToken ?? null, new kotlin.jvm.functions.Function1({
                invoke(error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve();
                    }
                },
            }));
        });
    }
    verifyOtp(params) {
        return new Promise((resolve, reject) => {
            if (params) {
                if ('phone' in params) {
                    if (params.token && params.type) {
                        this.native.verifyOTP(params.phone, params.token, params.type === 'sms' ? org.nativescript.supabase.SupabaseAuth.MobileOTPType.sms : org.nativescript.supabase.SupabaseAuth.MobileOTPType.phoneChange, params.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                            invoke(user, session, error) {
                                if (error) {
                                    reject(new Error(error.getMessage()));
                                }
                                else {
                                    resolve({ user: User.fromNative(user), session: session ? Session.fromNative(session) : null });
                                }
                            },
                        }));
                        return;
                    }
                    reject(new Error('token and type required'));
                }
                else if ('email' in params) {
                    if (params.token && params.type) {
                        let nativeType;
                        switch (params.type) {
                            case 'email':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.email;
                                break;
                            case 'signup':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.signup;
                                break;
                            case 'invite':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.invite;
                                break;
                            case 'magiclink':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.magiclink;
                                break;
                            case 'recovery':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.recovery;
                                break;
                            case 'email_change':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.emailChange;
                                break;
                        }
                        if (!nativeType) {
                            reject(new Error('Invalid type'));
                            return;
                        }
                        this.native.verifyOTPEmail(params.email, params.token, nativeType, params.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                            invoke(user, session, error) {
                                if (error) {
                                    reject(new Error(error.getMessage()));
                                }
                                else {
                                    resolve({ user: User.fromNative(user), session: session ? Session.fromNative(session) : null });
                                }
                            },
                        }));
                        return;
                    }
                    reject(new Error('token and type required'));
                }
                else if ('token_hash' in params) {
                    if (params.token_hash && params.type) {
                        let nativeType;
                        switch (params.type) {
                            case 'email':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.email;
                                break;
                            case 'signup':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.signup;
                                break;
                            case 'invite':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.invite;
                                break;
                            case 'magiclink':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.magiclink;
                                break;
                            case 'recovery':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.recovery;
                                break;
                            case 'email_change':
                                nativeType = org.nativescript.supabase.SupabaseAuth.EmailOTPType.emailChange;
                                break;
                        }
                        if (!nativeType) {
                            reject(new Error('Invalid type'));
                            return;
                        }
                        this.native.verifyOTP(params.token_hash, nativeType, params.options?.captchaToken ?? null, new kotlin.jvm.functions.Function3({
                            invoke(user, session, error) {
                                if (error) {
                                    reject(new Error(error.getMessage()));
                                }
                                else {
                                    resolve({ user: User.fromNative(user), session: session ? Session.fromNative(session) : null });
                                }
                            },
                        }));
                        return;
                    }
                    reject(new Error('token_hash and type required'));
                }
                reject(new Error('Invalid params'));
            }
        });
    }
    refreshSession(refreshToken) {
        return new Promise((resolve, reject) => {
            this.native.refreshSession(refreshToken, new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    update(attributes) {
        return new Promise((resolve, reject) => {
            const attr = new org.nativescript.supabase.SupabaseAuth.UserAttributes();
            if (attributes.data) {
                attr.setData(JSON.stringify(attributes.data));
            }
            if (attributes.email) {
                attr.setEmail(attributes.email);
            }
            if (attributes.nonce) {
                attr.setNonce(attributes.nonce);
            }
            if (attributes.password) {
                attr.setPassword(attributes.password);
            }
            if (attributes.phone) {
                attr.setPhone(attributes.phone);
            }
            this.native.update(attr, new kotlin.jvm.functions.Function2({
                invoke(user, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(User.fromNative(user));
                    }
                },
            }));
        });
    }
    getUserIdentities() {
        return new Promise((resolve, reject) => {
            const ret = [];
            const identities = this.native.userIdentities();
            const count = identities.size();
            for (let i = 0; i < count; i++) {
                ret.push(UserIdentity.fromNative(identities.get(i)));
            }
            resolve(ret);
        });
    }
    linkIdentity(credentials) {
        return new Promise((resolve, reject) => {
            let nativeProvider;
            switch (credentials.provider) {
                case 'apple':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.apple;
                    break;
                case 'azure':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.azure;
                    break;
                case 'bitbucket':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.bitbucket;
                    break;
                case 'discord':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.discord;
                    break;
                case 'facebook':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.facebook;
                    break;
                case 'figma':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.figma;
                    break;
                case 'github':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.github;
                    break;
                case 'gitlab':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.gitlab;
                    break;
                case 'google':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.google;
                    break;
                case 'kakao':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.kakao;
                    break;
                case 'keycloak':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.keycloak;
                    break;
                case 'linkedin':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.linkedin;
                    break;
                case 'linkedin_oidc':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.linkedinOIDC;
                    break;
                case 'notion':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.notion;
                    break;
                case 'slack':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.slack;
                    break;
                case 'spotify':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.spotify;
                    break;
                case 'twitch':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.twitch;
                    break;
                case 'twitter':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.twitter;
                    break;
                case 'workos':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.workos;
                    break;
                case 'zoom':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.zoom;
                    break;
                case 'fly':
                    nativeProvider = org.nativescript.supabase.SupabaseAuth.Provider.fly;
                    break;
            }
            if (!nativeProvider) {
                reject(new Error('Invalid provider'));
                return;
            }
            let queryParams = null;
            if (credentials.options?.queryParams) {
                const params = Object.keys(credentials.options.queryParams);
                if (params.length > 0) {
                    queryParams = new java.util.HashMap(params.length);
                    for (const key in credentials.options.queryParams) {
                        queryParams.put(key, credentials.options.queryParams[key]);
                    }
                }
            }
            this.native.linkIdentity(nativeProvider, credentials.options?.scopes ?? null, credentials.options?.redirectTo ?? null, queryParams, new kotlin.jvm.functions.Function1({
                invoke(error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve();
                    }
                },
            }));
        });
    }
    unlinkIdentity(identity) {
        return new Promise((resolve, reject) => {
            // this.native.unlinkIdentity(identity.native, (error) => {
            // 	if (error) {
            // 		reject(new Error(error.localizedDescription));
            // 	} else {
            // 		resolve();
            // 	}
            // });
        });
    }
    reauthenticate() {
        return new Promise((resolve, reject) => {
            this.native.reauthenticate(new kotlin.jvm.functions.Function1({
                invoke(error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve();
                    }
                },
            }));
        });
    }
    resend(credentials) {
        return new Promise((resolve, reject) => {
            if ((credentials.email && credentials.type === 'signup') || credentials.type === 'email_change') {
                this.native.resend(credentials.email, credentials.type === 'signup' ? org.nativescript.supabase.SupabaseAuth.ResendEmailType.signup : org.nativescript.supabase.SupabaseAuth.ResendEmailType.emailChange, credentials?.options?.emailRedirectTo ?? null, credentials?.options?.captchaToken ?? null, new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
            else if ((credentials.phone && credentials.type === 'sms') || credentials.type === 'phone_change') {
                this.native.resend(credentials.phone, credentials.type === 'sms' ? org.nativescript.supabase.SupabaseAuth.ResendMobileType.sms : org.nativescript.supabase.SupabaseAuth.ResendMobileType.phoneChange, credentials?.options?.captchaToken ?? null, new kotlin.jvm.functions.Function1({
                    invoke(error) {
                        if (error) {
                            reject(new Error(error.getMessage()));
                        }
                        else {
                            resolve();
                        }
                    },
                }));
            }
            else {
            }
        });
    }
    exchangeCodeForSession(authCode) {
        return new Promise((resolve, reject) => {
            this.native.exchangeCodeForSession(authCode, new kotlin.jvm.functions.Function2({
                invoke(session, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                },
            }));
        });
    }
    getUser(jwt) {
        return new Promise((resolve, reject) => {
            this.native.user(jwt ?? null, new kotlin.jvm.functions.Function2({
                invoke(user, error) {
                    if (error) {
                        reject(new Error(error.getMessage()));
                    }
                    else {
                        resolve(User.fromNative(user));
                    }
                },
            }));
        });
    }
}
//# sourceMappingURL=index.android.js.map