{"version":3,"file":"GoogleOneTapClient.mjs","sources":["../../../../../../../web/src/oneTap/GoogleOneTapClient.ts"],"sourcesContent":["import { GOOGLE_ONE_TAP_HOST } from '@stytch/core';\nimport { OneTapStyleConfig } from '@stytch/core/public';\nimport type { accounts, CredentialResponse, IdConfiguration, PromptMomentNotification } from 'google-one-tap';\n\nimport { getConfiguredEmbeddedMode, getShouldRenderEmbeddedOneTap } from './positionModes';\n\ntype OneTapNotShownReason =\n  | ReturnType<PromptMomentNotification['getNotDisplayedReason']>\n  | ReturnType<PromptMomentNotification['getSkippedReason']>;\n\nexport type OneTapRenderResult = { success: true } | { success: false; reason: OneTapNotShownReason };\n\n/**\n * Wrapper around Google One Tap JS client that combines initialize() and prompt()\n * into one async render() call\n *\n * @see https://developers.google.com/identity/gsi/web/guides/display-google-one-tap#javascript\n */\nexport class GoogleOneTapClient {\n  constructor(\n    private _googleClient: accounts['id'],\n    private _clientId: string,\n  ) {}\n\n  cancel(): void {\n    this._googleClient.cancel();\n  }\n\n  async render({\n    callback,\n    onOneTapCancelled,\n    style,\n    cancelOnTapOutside = true,\n  }: {\n    callback: (response: CredentialResponse) => void;\n    onOneTapCancelled?: (showError?: boolean) => void;\n    style: OneTapStyleConfig;\n    cancelOnTapOutside?: boolean;\n  }): Promise<OneTapRenderResult> {\n    const embeddedMode = getConfiguredEmbeddedMode(style.position);\n    const shouldRenderEmbeddedOneTap = getShouldRenderEmbeddedOneTap(style.position);\n\n    const config: IdConfiguration = {\n      client_id: this._clientId,\n      callback: callback,\n      auto_select: false,\n      context: 'use',\n      itp_support: true,\n      use_fedcm_for_prompt: embeddedMode !== 'force',\n      cancel_on_tap_outside: cancelOnTapOutside,\n    };\n    if (shouldRenderEmbeddedOneTap) {\n      config.prompt_parent_id = 'google-parent-prompt';\n      config.cancel_on_tap_outside = false;\n    }\n    this._googleClient.initialize(config);\n\n    return new Promise((resolve) => {\n      this._googleClient.prompt((notification: PromptMomentNotification) => {\n        if (notification.isSkippedMoment()) {\n          // After FedCM is mandatory, `getSkippedReason` will no longer be\n          // supported, so access it defensively.\n          const reason = notification.getSkippedReason?.() ?? 'unknown_reason';\n\n          if (reason === 'user_cancel') {\n            onOneTapCancelled?.();\n          }\n          return resolve({\n            success: false,\n            reason,\n          });\n        }\n\n        // At some point after FedCM is mandatory, `isNotDisplayed` and\n        // `getNotDisplayedReason` (and display moments in general) will not be\n        // supported. If we can tell they're not available and we're trying to\n        // show an embedded one tap UI, we should treat it as a failure, because\n        // we won't expect an embedded UI to work (by virtue of FedCM being\n        // mandatory) and won't expect any other notifications.\n        if ((!notification.isNotDisplayed && shouldRenderEmbeddedOneTap) || notification.isNotDisplayed?.()) {\n          return resolve({\n            success: false,\n            reason: notification.getNotDisplayedReason?.() ?? 'unknown_reason',\n          });\n        }\n\n        if (!notification.isDismissedMoment()) {\n          this.styleFrame(shouldRenderEmbeddedOneTap);\n          return resolve({ success: true });\n        }\n      });\n    });\n  }\n\n  styleFrame(shouldRenderEmbeddedOneTap: boolean) {\n    if (!shouldRenderEmbeddedOneTap) {\n      return;\n    }\n    Array.from(document.getElementsByTagName('iframe')).forEach((frame) => {\n      if (frame.src.includes(GOOGLE_ONE_TAP_HOST)) {\n        frame.style.width = '100%';\n      }\n    });\n  }\n}\n"],"names":["GoogleOneTapClient","_clientId","_googleClient","cancel","render","callback","onOneTapCancelled","style","cancelOnTapOutside","embeddedMode","getConfiguredEmbeddedMode","position","shouldRenderEmbeddedOneTap","getShouldRenderEmbeddedOneTap","config","client_id","auto_select","context","itp_support","use_fedcm_for_prompt","cancel_on_tap_outside","prompt_parent_id","initialize","Promise","resolve","prompt","notification","isSkippedMoment","reason","getSkippedReason","success","isNotDisplayed","getNotDisplayedReason","isDismissedMoment","styleFrame","Array","from","document","getElementsByTagName","forEach","frame","src","includes","GOOGLE_ONE_TAP_HOST","width"],"mappings":";;;AAYA;;;;;AAKC,IACM,MAAMA,kBAAAA,CAAAA;;;AACX,IAAA,WAAA,CACE,aAAqC,EAC7BC,S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