import { GoogleOneTapClient } from './GoogleOneTapClient.mjs';
import { StytchAPIError } from '../../../core/src/public/SDKErrors.mjs';
import { loadESModule } from '../../../core/src/utils/loadESModule.mjs';
import { GOOGLE_ONE_TAP_SCRIPT_URL } from '../../../core/src/constants.mjs';

const loadGoogleOneTapClient = ()=>loadESModule(GOOGLE_ONE_TAP_SCRIPT_URL, ()=>window.google.accounts.id);
/**
 * Handles loading One Tap config from Stytch API and initializing {@link GoogleOneTapClient}
 */ class OneTapProvider {
    _publicToken;
    clientsideServices;
    googleConfig;
    constructor(_publicToken, clientsideServices){
        this._publicToken = _publicToken;
        this.clientsideServices = clientsideServices;
    }
    async createOneTapClient() {
        let googleClientId;
        try {
            ({ googleClientId } = await this.fetchGoogleStart());
        } catch (e) {
            const err = StytchAPIError.from(e);
            return {
                success: false,
                reason: err.error_type
            };
        }
        if (googleClientId === '') {
            return {
                success: false,
                reason: 'oauth_config_not_found'
            };
        }
        const client = new GoogleOneTapClient(await loadGoogleOneTapClient(), googleClientId);
        return {
            success: true,
            client
        };
    }
    createOnSuccessHandler = ({ loginRedirectUrl, signupRedirectUrl, onSuccess })=>async (response)=>{
            const { credential } = response;
            const { redirect_url } = await this.submitGoogleOneTapToken({
                credential,
                loginRedirectUrl,
                signupRedirectUrl
            });
            onSuccess(redirect_url);
        };
    fetchGoogleStart() {
        if (this.googleConfig) {
            return this.googleConfig;
        }
        this.googleConfig = this.clientsideServices.oneTapStart({
            publicToken: this._publicToken
        });
        return this.googleConfig;
    }
    async submitGoogleOneTapToken({ credential, loginRedirectUrl, signupRedirectUrl }) {
        const { oauthCallbackId } = await this.fetchGoogleStart();
        return this.clientsideServices.oneTapSubmit({
            publicToken: this._publicToken,
            idToken: credential,
            loginRedirectURL: loginRedirectUrl,
            oauthCallbackID: oauthCallbackId,
            signupRedirectURL: signupRedirectUrl
        });
    }
    redirectOnSuccess = (redirect_url)=>{
        window.location.href = redirect_url;
    };
    /**
   * Google One Tap will show a banner on the bottom of the screen on certain mobile devices
   * This logic is controlled via some sniffing of the useragent string on startup
   * These specific strings were extracted from the One Tap minified source code
   * See the linked PR for details and screenshots
   */ static willGoogleOneTapShowEmbedded(ua = navigator.userAgent) {
        const uaContains = (userAgent, searchString)=>userAgent.indexOf(searchString) !== -1;
        const isTabletUA = uaContains(ua, 'iPad') || uaContains(ua, 'Android') && !uaContains(ua, 'Mobile') || uaContains(ua, 'Silk');
        const isMobileUA = uaContains(ua, 'iPod') || uaContains(ua, 'iPhone') || uaContains(ua, 'Android') || uaContains(ua, 'IEMobile');
        return !isTabletUA && isMobileUA;
    }
}

export { OneTapProvider, loadGoogleOneTapClient };
//# sourceMappingURL=OneTapProvider.mjs.map
