import { Utils } from '@nativescript/core';
export class UserIdentity {
    static fromNative(identity) {
        if (!identity)
            return null;
        const ret = new UserIdentity();
        ret.native = identity;
        return ret;
    }
    get createdAt() {
        return this.native.createdAt;
    }
    get id() {
        return this.native.id;
    }
    get identityData() {
        if (!this._identityData) {
            this._identityData = Utils.dataDeserialize(this.native.identityData);
        }
        return this._identityData;
    }
    get identityId() {
        return this.native.identityId.UUIDString;
    }
    get lastSignInAt() {
        return this.native.lastSignInAt;
    }
    get provider() {
        return this.native.provider;
    }
    get updatedAt() {
        return this.native.updatedAt;
    }
    get userId() {
        return this.native.userId.UUIDString;
    }
    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 this.native.createdAt;
    }
    get factorType() {
        return this.native.factorType;
    }
    get friendlyName() {
        return this.native.friendlyName;
    }
    get id() {
        return this.native.id;
    }
    get status() {
        switch (this.native.status) {
            case 0 /* NSCSupabaseFactorStatus.Verified */:
                return 'verified';
            case 1 /* NSCSupabaseFactorStatus.Unverified */:
                return 'unverified';
        }
    }
    get updatedAt() {
        return this.native.updatedAt;
    }
    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.actionLink;
    }
    get appMetadata() {
        if (!this._appMetadata) {
            this._appMetadata = Utils.dataDeserialize(this.native.appMetadata);
        }
        return this._appMetadata;
    }
    get aud() {
        return this.native.aud;
    }
    get confirmationSentAt() {
        return this.native.confirmationSentAt;
    }
    get confirmedAt() {
        return this.native.confirmedAt;
    }
    get createdAt() {
        return this.native.createdAt;
    }
    get email() {
        return this.native.email;
    }
    get emailChangeSentAt() {
        return this.native.emailChangeSentAt;
    }
    get emailConfirmedAt() {
        return this.native.emailConfirmedAt;
    }
    get factors() {
        if (!this._factors) {
            this._factors = [];
            const factors = this.native.factors;
            const count = factors.count;
            for (let i = 0; i < count; i++) {
                this._factors.push(Factor.fromNative(factors.objectAtIndex(i)));
            }
        }
        return this._factors;
    }
    get id() {
        return this.native.id.UUIDString;
    }
    get identities() {
        // TODO cache
        const ret = [];
        const identities = this.native.identities;
        const count = identities.count;
        for (let i = 0; i < count; i++) {
            ret.push(UserIdentity.fromNative(identities.objectAtIndex(i)));
        }
        return ret;
    }
    get invitedAt() {
        return this.native.invitedAt;
    }
    get isAnonymous() {
        return this.native.isAnonymous;
    }
    get lastSignInAt() {
        return this.native.lastSignInAt;
    }
    get newEmail() {
        return this.native.newEmail;
    }
    get phone() {
        return this.native.phone;
    }
    get phoneConfirmedAt() {
        return this.native.phoneConfirmedAt;
    }
    get recoverySentAt() {
        return this.native.recoverySentAt;
    }
    get role() {
        return this.native.role;
    }
    get updatedAt() {
        return this.native.updatedAt;
    }
    get userMetadata() {
        if (!this._userMetadata) {
            this._userMetadata = Utils.dataDeserialize(this.native.userMetadata);
        }
        return this._userMetadata;
    }
}
export class Session {
    static fromNative(session) {
        if (!session)
            return null;
        const ret = new Session();
        ret.native = session;
        return ret;
    }
    get accessToken() {
        return this.native.accessToken;
    }
    get expiresAt() {
        return this.native.expiresAt;
    }
    get expiresIn() {
        return this.native.expiresIn;
    }
    get isExpired() {
        return this.native.isExpired;
    }
    get providerRefreshToken() {
        return this.native.providerRefreshToken;
    }
    get providerToken() {
        return this.native.providerToken;
    }
    get refreshToken() {
        return this.native.refreshToken;
    }
    get tokenType() {
        return this.native.tokenType;
    }
    get user() {
        if (!this._user) {
            this._user = User.fromNative(this.native.user);
        }
        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 NSCSupabaseAuthMetaSecurity) {
            this.native_ = captchaToken;
        }
        else {
            this.native_ = NSCSupabaseAuthMetaSecurity.alloc().initWithCaptchaToken(captchaToken);
        }
    }
    static fromNative(meta) {
        if (!meta)
            return null;
        return new AuthMetaSecurity(meta);
    }
    get native() {
        return this.native_;
    }
    get captchaToken() {
        return this.native_.captchaToken;
    }
}
export class OpenIDConnectCredentials {
    constructor(provider, idToken) {
        let nativeProvider;
        switch (provider) {
            case 'google':
                nativeProvider = 0 /* NSCSupabaseOpenIDConnectCredentialsProvider.Google */;
                break;
            case 'apple':
                nativeProvider = 1 /* NSCSupabaseOpenIDConnectCredentialsProvider.Apple */;
                break;
            case 'azure':
                nativeProvider = 2 /* NSCSupabaseOpenIDConnectCredentialsProvider.Azure */;
                break;
            case 'facebook':
                nativeProvider = 3 /* NSCSupabaseOpenIDConnectCredentialsProvider.Facebook */;
                break;
        }
        if (!nativeProvider) {
            throw new Error('Invalid provider');
        }
        this.native_ = NSCSupabaseOpenIDConnectCredentials.alloc().init(nativeProvider, idToken);
    }
    get native() {
        return this.native_;
    }
    get accessToken() {
        return this.native.accessToken;
    }
    set accessToken(token) {
        this.native.accessToken = token ?? null;
    }
    get gotrueMetaSecurity() {
        return AuthMetaSecurity.fromNative(this.native.gotrueMetaSecurity);
    }
    set gotrueMetaSecurity(meta) {
        this.native.gotrueMetaSecurity = meta?.native ?? null;
    }
    get idToken() {
        return this.native.idToken;
    }
    set idToken(token) {
        this.native.idToken = token;
    }
    get nonce() {
        return this.native.nonce;
    }
    set nonce(nonce) {
        this.native.nonce = nonce ?? null;
    }
    get provider() {
        switch (this.native.provider) {
            case 0 /* NSCSupabaseOpenIDConnectCredentialsProvider.Google */:
                return 'google';
            case 1 /* NSCSupabaseOpenIDConnectCredentialsProvider.Apple */:
                return 'apple';
            case 2 /* NSCSupabaseOpenIDConnectCredentialsProvider.Azure */:
                return 'azure';
            case 3 /* NSCSupabaseOpenIDConnectCredentialsProvider.Facebook */:
                return 'facebook';
        }
    }
    set provider(provider) {
        switch (provider) {
            case 'google':
                this.native.provider = 0 /* NSCSupabaseOpenIDConnectCredentialsProvider.Google */;
                break;
            case 'apple':
                this.native.provider = 1 /* NSCSupabaseOpenIDConnectCredentialsProvider.Apple */;
                break;
            case 'azure':
                this.native.provider = 2 /* NSCSupabaseOpenIDConnectCredentialsProvider.Azure */;
                break;
            case 'facebook':
                this.native.provider = 3 /* NSCSupabaseOpenIDConnectCredentialsProvider.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.auth;
    }
    get currentSession() {
        if (!this.native.currentSession)
            return null;
        return Session.fromNative(this.native.currentSession);
    }
    get currentUser() {
        if (!this.native.currentUser)
            return null;
        return User.fromNative(this.native.currentUser);
    }
    onAuthStateChange(callback) {
        const listener = this.native.addOnAuthStateChange((event, session) => {
            let eventName;
            switch (event) {
                case 0 /* NSCSupabaseAuthChangeEvent.InitialSession */:
                    eventName = 'INITIAL_SESSION';
                    break;
                case 1 /* NSCSupabaseAuthChangeEvent.PasswordRecovery */:
                    eventName = 'PASSWORD_RECOVERY';
                    break;
                case 2 /* NSCSupabaseAuthChangeEvent.SignedIn */:
                    eventName = 'SIGNED_IN';
                    break;
                case 3 /* NSCSupabaseAuthChangeEvent.SignedOut */:
                    eventName = 'SIGNED_OUT';
                    break;
                case 4 /* NSCSupabaseAuthChangeEvent.TokenRefreshed */:
                    eventName = 'TOKEN_REFRESHED';
                    break;
                case 5 /* NSCSupabaseAuthChangeEvent.UserUpdated */:
                    eventName = 'USER_UPDATED';
                    break;
                case 6 /* NSCSupabaseAuthChangeEvent.UserDeleted */:
                    eventName = 'USER_DELETED';
                    break;
                case 7 /* NSCSupabaseAuthChangeEvent.MfaChallengeVerified */:
                    eventName = 'MFA_CHALLENGE_VERIFIED';
                    break;
            }
            callback(eventName, Session.fromNative(session));
        });
        return {
            remove: () => {
                listener.remove();
            },
        };
    }
    getSession() {
        return new Promise((resolve, reject) => {
            this.native.getSession((session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(Session.fromNative(session));
                }
            });
        });
    }
    setSession(currentSession) {
        return new Promise((resolve, reject) => {
            this.native.setSession(currentSession.accessToken, currentSession.refreshToken, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(Session.fromNative(session));
                }
            });
        });
    }
    signInWithPassword(credentials) {
        return new Promise((resolve, reject) => {
            if (credentials.phone) {
                this.native.signInPhone(credentials.phone, credentials.password, credentials.options?.captchaToken ?? null, (session, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                });
            }
            else {
                this.native.signIn(credentials.email, credentials.password, credentials.options?.captchaToken ?? null, (session, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve(Session.fromNative(session));
                    }
                });
            }
        });
    }
    signInAnonymously(credentials) {
        return new Promise((resolve, reject) => {
            let dataSerialized = null;
            if (credentials?.data) {
                dataSerialized = Utils.dataSerialize(credentials?.data);
            }
            this.native.signInAnonymously(dataSerialized, credentials?.options?.captchaToken ?? null, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(Session.fromNative(session));
                }
            });
        });
    }
    signInWithIdToken(credentials) {
        return new Promise((resolve, reject) => {
            this.native.signInWithIdToken(credentials.native, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                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 ? Utils.dataSerialize(value.options.data) : null, value.options?.emailRedirectTo ?? null, (user, session, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve({ session: session ? Session.fromNative(session) : null, user: User.fromNative(user) });
                    }
                });
            }
            else if (value.phone) {
                this.native.signUpWithPhone(value.phone, value.password, value.captchaToken ?? null, value.options?.data ? Utils.dataSerialize(value.options.data) : null, (user, session, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    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?.data ? Utils.dataSerialize(credentials.options.data) : null, credentials.options?.captchaToken ?? null, (error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
            else if (credentials.phone) {
                this.native.signInWithOTPWithPhoneShouldCreateUser(credentials.phone, credentials.options?.channel === 'whatsapp' ? 1 /* NSCSupabaseMessagingChannel.Whatsapp */ : 0 /* NSCSupabaseMessagingChannel.Sms */, credentials.options?.shouldCreateUser ?? true, credentials.options?.data ? Utils.dataSerialize(credentials.options.data) : null, credentials.options?.captchaToken ?? null, (error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
        });
    }
    signInWithOAuth(credentials) {
        return new Promise((resolve, reject) => {
            let nativeProvider;
            switch (credentials.provider) {
                case 'apple':
                    nativeProvider = 0 /* NSCSupabaseProvider.Apple */;
                    break;
                case 'azure':
                    nativeProvider = 1 /* NSCSupabaseProvider.Azure */;
                    break;
                case 'bitbucket':
                    nativeProvider = 2 /* NSCSupabaseProvider.Bitbucket */;
                    break;
                case 'discord':
                    nativeProvider = 3 /* NSCSupabaseProvider.Discord */;
                    break;
                case 'facebook':
                    nativeProvider = 5 /* NSCSupabaseProvider.Facebook */;
                    break;
                case 'figma':
                    nativeProvider = 6 /* NSCSupabaseProvider.Figma */;
                    break;
                case 'github':
                    nativeProvider = 7 /* NSCSupabaseProvider.Github */;
                    break;
                case 'gitlab':
                    nativeProvider = 8 /* NSCSupabaseProvider.Gitlab */;
                    break;
                case 'google':
                    nativeProvider = 9 /* NSCSupabaseProvider.Google */;
                    break;
                case 'kakao':
                    nativeProvider = 10 /* NSCSupabaseProvider.Kakao */;
                    break;
                case 'keycloak':
                    nativeProvider = 11 /* NSCSupabaseProvider.Keycloak */;
                    break;
                case 'linkedin':
                    nativeProvider = 12 /* NSCSupabaseProvider.Linkedin */;
                    break;
                case 'linkedin_oidc':
                    nativeProvider = 13 /* NSCSupabaseProvider.LinkedinOIDC */;
                    break;
                case 'notion':
                    nativeProvider = 14 /* NSCSupabaseProvider.Notion */;
                    break;
                case 'slack':
                    nativeProvider = 15 /* NSCSupabaseProvider.Slack */;
                    break;
                case 'spotify':
                    nativeProvider = 17 /* NSCSupabaseProvider.Spotify */;
                    break;
                case 'twitch':
                    nativeProvider = 18 /* NSCSupabaseProvider.Twitch */;
                    break;
                case 'twitter':
                    nativeProvider = 19 /* NSCSupabaseProvider.Twitter */;
                    break;
                case 'workos':
                    nativeProvider = 20 /* NSCSupabaseProvider.Workos */;
                    break;
                case 'zoom':
                    nativeProvider = 21 /* NSCSupabaseProvider.Zoom */;
                    break;
                case 'fly':
                    nativeProvider = 22 /* NSCSupabaseProvider.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 = NSMutableArray.alloc().initWithCapacity(params.length);
                    for (const key in credentials.options.queryParams) {
                        queryParams.addObject(NSCSupabaseQueryParamEntry.alloc().initWithNameValue(key, credentials.options.queryParams[key]));
                    }
                }
            }
            this.native.signInWithOAuth(nativeProvider, credentials.options?.redirectTo ?? null, credentials.options?.scopes ?? null, queryParams, 
            // todo allow ios to configure ASWebAuthenticationSession
            null, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                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, (url, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve(url.absoluteString);
                    }
                });
            }
            else if (params.domain) {
                this.native.signInWithSSOWithDomain(params.domain, params.options?.redirectTo ?? null, params.options?.captchaToken ?? null, (url, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve(url.absoluteString);
                    }
                });
            }
        });
    }
    signOut(options) {
        return new Promise((resolve, reject) => {
            let nativeScope;
            switch (options?.scope) {
                case 'global':
                    nativeScope = 0 /* NSCSupabaseSignOutScope.Global */;
                    break;
                case 'local':
                    nativeScope = 1 /* NSCSupabaseSignOutScope.Local */;
                    break;
                case 'others':
                    nativeScope = 2 /* NSCSupabaseSignOutScope.Others */;
                    break;
                default:
                    break;
            }
            if (nativeScope) {
                this.native.signOutWithScope(nativeScope, (error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
            else {
                this.native.signOut((error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
        });
    }
    resetPasswordForEmail(email, options) {
        return new Promise((resolve, reject) => {
            this.native.resetPasswordForEmail(email, options?.redirectTo ?? null, options?.captchaToken ?? null, (error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve();
                }
            });
        });
    }
    verifyOtp(params) {
        return new Promise((resolve, reject) => {
            if (params) {
                if ('phone' in params) {
                    if (params.token && params.type) {
                        this.native.verifyOTPWithPhone(params.phone, params.token, params.type === 'sms' ? 0 /* NSCSupabaseMobileOTPType.Sms */ : 1 /* NSCSupabaseMobileOTPType.PhoneChange */, params.options?.captchaToken ?? null, (user, session, error) => {
                            if (error) {
                                reject(new Error(error.localizedDescription));
                            }
                            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 = 5 /* NSCSupabaseEmailOTPType.Email */;
                                break;
                            case 'signup':
                                nativeType = 0 /* NSCSupabaseEmailOTPType.Signup */;
                                break;
                            case 'invite':
                                nativeType = 1 /* NSCSupabaseEmailOTPType.Invite */;
                                break;
                            case 'magiclink':
                                nativeType = 2 /* NSCSupabaseEmailOTPType.Magiclink */;
                                break;
                            case 'recovery':
                                nativeType = 3 /* NSCSupabaseEmailOTPType.Recovery */;
                                break;
                            case 'email_change':
                                nativeType = 4 /* NSCSupabaseEmailOTPType.EmailChange */;
                                break;
                        }
                        if (!nativeType) {
                            reject(new Error('Invalid type'));
                            return;
                        }
                        this.native.verifyOTPWithEmail(params.email, params.token, nativeType, params.options?.redirectTo ?? null, params.options?.captchaToken ?? null, (user, session, error) => {
                            if (error) {
                                reject(new Error(error.localizedDescription));
                            }
                            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 = 5 /* NSCSupabaseEmailOTPType.Email */;
                                break;
                            case 'signup':
                                nativeType = 0 /* NSCSupabaseEmailOTPType.Signup */;
                                break;
                            case 'invite':
                                nativeType = 1 /* NSCSupabaseEmailOTPType.Invite */;
                                break;
                            case 'magiclink':
                                nativeType = 2 /* NSCSupabaseEmailOTPType.Magiclink */;
                                break;
                            case 'recovery':
                                nativeType = 3 /* NSCSupabaseEmailOTPType.Recovery */;
                                break;
                            case 'email_change':
                                nativeType = 4 /* NSCSupabaseEmailOTPType.EmailChange */;
                                break;
                        }
                        if (!nativeType) {
                            reject(new Error('Invalid type'));
                            return;
                        }
                        this.native.verifyOTPWithTokenHash(params.token_hash, nativeType, (user, session, error) => {
                            if (error) {
                                reject(new Error(error.localizedDescription));
                            }
                            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, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(Session.fromNative(session));
                }
            });
        });
    }
    update(attributes) {
        return new Promise((resolve, reject) => {
            const attr = NSCSupabaseUserAttributes.new();
            if (attributes.data) {
                attr.data = Utils.dataSerialize(attributes.data);
            }
            if (attributes.email) {
                attr.email = attributes.email;
            }
            if (attributes.nonce) {
                attr.nonce = attributes.nonce;
            }
            if (attributes.password) {
                attr.password = attributes.password;
            }
            if (attributes.phone) {
                attr.phone = attributes.phone;
            }
            this.native.update(attr, (user, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(User.fromNative(user));
                }
            });
        });
    }
    getUserIdentities() {
        return new Promise((resolve, reject) => {
            this.native.userIdentities((identities, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    const ret = [];
                    const count = identities.count;
                    for (let i = 0; i < count; i++) {
                        ret.push(UserIdentity.fromNative(identities.objectAtIndex(i)));
                    }
                    resolve(ret);
                }
            });
        });
    }
    linkIdentity(credentials) {
        return new Promise((resolve, reject) => {
            let nativeProvider;
            switch (credentials.provider) {
                case 'apple':
                    nativeProvider = 0 /* NSCSupabaseProvider.Apple */;
                    break;
                case 'azure':
                    nativeProvider = 1 /* NSCSupabaseProvider.Azure */;
                    break;
                case 'bitbucket':
                    nativeProvider = 2 /* NSCSupabaseProvider.Bitbucket */;
                    break;
                case 'discord':
                    nativeProvider = 3 /* NSCSupabaseProvider.Discord */;
                    break;
                case 'facebook':
                    nativeProvider = 5 /* NSCSupabaseProvider.Facebook */;
                    break;
                case 'figma':
                    nativeProvider = 6 /* NSCSupabaseProvider.Figma */;
                    break;
                case 'github':
                    nativeProvider = 7 /* NSCSupabaseProvider.Github */;
                    break;
                case 'gitlab':
                    nativeProvider = 8 /* NSCSupabaseProvider.Gitlab */;
                    break;
                case 'google':
                    nativeProvider = 9 /* NSCSupabaseProvider.Google */;
                    break;
                case 'kakao':
                    nativeProvider = 10 /* NSCSupabaseProvider.Kakao */;
                    break;
                case 'keycloak':
                    nativeProvider = 11 /* NSCSupabaseProvider.Keycloak */;
                    break;
                case 'linkedin':
                    nativeProvider = 12 /* NSCSupabaseProvider.Linkedin */;
                    break;
                case 'linkedin_oidc':
                    nativeProvider = 13 /* NSCSupabaseProvider.LinkedinOIDC */;
                    break;
                case 'notion':
                    nativeProvider = 14 /* NSCSupabaseProvider.Notion */;
                    break;
                case 'slack':
                    nativeProvider = 15 /* NSCSupabaseProvider.Slack */;
                    break;
                case 'spotify':
                    nativeProvider = 17 /* NSCSupabaseProvider.Spotify */;
                    break;
                case 'twitch':
                    nativeProvider = 18 /* NSCSupabaseProvider.Twitch */;
                    break;
                case 'twitter':
                    nativeProvider = 19 /* NSCSupabaseProvider.Twitter */;
                    break;
                case 'workos':
                    nativeProvider = 20 /* NSCSupabaseProvider.Workos */;
                    break;
                case 'zoom':
                    nativeProvider = 21 /* NSCSupabaseProvider.Zoom */;
                    break;
                case 'fly':
                    nativeProvider = 22 /* NSCSupabaseProvider.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 = NSMutableArray.alloc().initWithCapacity(params.length);
                    for (const key in credentials.options.queryParams) {
                        queryParams.addObject(NSCSupabaseQueryParamEntry.alloc().initWithNameValue(key, credentials.options.queryParams[key]));
                    }
                }
            }
            this.native.linkIdentity(nativeProvider, credentials.options?.scopes ?? null, credentials.options?.redirectTo ?? null, queryParams, (error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                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((error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve();
                }
            });
        });
    }
    resend(credentials) {
        return new Promise((resolve, reject) => {
            if ((credentials.email && credentials.type === 'signup') || credentials.type === 'email_change') {
                this.native.resendWithEmail(credentials.email, credentials.type === 'signup' ? 0 /* NSCSupabaseResendEmailType.Signup */ : 1 /* NSCSupabaseResendEmailType.EmailChange */, credentials?.options?.emailRedirectTo ?? null, credentials?.options?.captchaToken ?? null, (error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
            else if ((credentials.phone && credentials.type === 'sms') || credentials.type === 'phone_change') {
                this.native.resendWithPhone(credentials.phone, credentials.type === 'sms' ? 0 /* NSCSupabaseResendMobileType.Sms */ : 1 /* NSCSupabaseResendMobileType.PhoneChange */, credentials?.options?.captchaToken ?? null, (id, error) => {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    }
                    else {
                        resolve();
                    }
                });
            }
            else {
            }
        });
    }
    exchangeCodeForSession(authCode) {
        return new Promise((resolve, reject) => {
            this.native.exchangeCodeForSession(authCode, (session, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(Session.fromNative(session));
                }
            });
        });
    }
    getUser(jwt) {
        return new Promise((resolve, reject) => {
            this.native.user(jwt ?? null, (user, error) => {
                if (error) {
                    reject(new Error(error.localizedDescription));
                }
                else {
                    resolve(User.fromNative(user));
                }
            });
        });
    }
}
//# sourceMappingURL=index.ios.js.map