{"version":3,"sources":["../../src/google-one-tap/index.ts"],"sourcesContent":["import type { CredentialResponse, GoogleAccounts, PromptMomentNotification } from './types';\n\ndeclare global {\n  interface Window {\n    google: {\n      accounts: GoogleAccounts;\n    };\n  }\n}\n\nexport type Props = {\n  client_id: string;\n  auto_select?: boolean;\n  cancel_on_tap_outside?: boolean;\n  use_fedcm_for_prompt?: boolean;\n};\n\nexport type PromptMoment = {\n  skipped: boolean;\n  dismissed: boolean;\n  momentType: ReturnType<PromptMomentNotification['getMomentType']>;\n  dismissedReason: ReturnType<PromptMomentNotification['getDismissedReason']>;\n};\n\nexport type PromptResult =\n  | { authorized: true; credential: CredentialResponse }\n  | { authorized: false; moment: PromptMoment };\n\nlet script: HTMLScriptElement | null = null;\n\nfunction loadScript(): Promise<void> {\n  return new Promise((resolve, reject) => {\n    if (script) {\n      if (window.google?.accounts?.id) {\n        resolve();\n      } else {\n        script.addEventListener('load', () => resolve());\n        script.addEventListener('error', () =>\n          reject(new Error('Failed to load Google One Tap script'))\n        );\n      }\n      return;\n    }\n\n    script = document.createElement('script');\n    script.id = 'google-one-tap';\n    script.async = true;\n    script.defer = true;\n    script.src = 'https://accounts.google.com/gsi/client';\n    script.onload = () => resolve();\n    script.onerror = () => reject(new Error('Failed to load Google One Tap script'));\n\n    document.head.appendChild(script);\n  });\n}\n\n/** debug: chrome://settings/content/federatedIdentityApi */\nexport async function prompt({\n  client_id,\n  auto_select = false,\n  use_fedcm_for_prompt = true,\n  cancel_on_tap_outside = false,\n}: Props): Promise<PromptResult> {\n  await loadScript();\n\n  return new Promise<PromptResult>((resolve) => {\n    let settled = false;\n\n    window.google.accounts.id.initialize({\n      ux_mode: 'popup',\n      context: 'signin',\n      auto_select,\n      client_id,\n      use_fedcm_for_prompt,\n      cancel_on_tap_outside,\n      callback: (credential) => {\n        if (settled) return;\n        settled = true;\n        resolve({ authorized: true, credential });\n      },\n      native_callback: (credential) => {\n        if (settled) return;\n        settled = true;\n        resolve({ authorized: true, credential });\n      },\n    });\n\n    window.google.accounts.id.prompt((notification) => {\n      if (settled) return;\n\n      if (\n        notification.isSkippedMoment() ||\n        (notification.isDismissedMoment() &&\n          notification.getDismissedReason() !== 'credential_returned')\n      ) {\n        settled = true;\n        resolve({\n          authorized: false,\n          moment: {\n            momentType: notification.getMomentType(),\n            skipped: notification.isSkippedMoment(),\n            dismissed: notification.isDismissedMoment(),\n            dismissedReason: notification.getDismissedReason(),\n          },\n        });\n      }\n    });\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA4BA,IAAI,SAAmC;AAEvC,SAAS,aAA4B;AACnC,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,QAAI,QAAQ;AACV,UAAI,OAAO,QAAQ,UAAU,IAAI;AAC/B,gBAAQ;AAAA,MACV,OAAO;AACL,eAAO,iBAAiB,QAAQ,MAAM,QAAQ,CAAC;AAC/C,eAAO;AAAA,UAAiB;AAAA,UAAS,MAC/B,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAAA,QAC1D;AAAA,MACF;AACA;AAAA,IACF;AAEA,aAAS,SAAS,cAAc,QAAQ;AACxC,WAAO,KAAK;AACZ,WAAO,QAAQ;AACf,WAAO,QAAQ;AACf,WAAO,MAAM;AACb,WAAO,SAAS,MAAM,QAAQ;AAC9B,WAAO,UAAU,MAAM,OAAO,IAAI,MAAM,sCAAsC,CAAC;AAE/E,aAAS,KAAK,YAAY,MAAM;AAAA,EAClC,CAAC;AACH;AAGA,eAAsB,OAAO;AAAA,EAC3B;AAAA,EACA,cAAc;AAAA,EACd,uBAAuB;AAAA,EACvB,wBAAwB;AAC1B,GAAiC;AAC/B,QAAM,WAAW;AAEjB,SAAO,IAAI,QAAsB,CAAC,YAAY;AAC5C,QAAI,UAAU;AAEd,WAAO,OAAO,SAAS,GAAG,WAAW;AAAA,MACnC,SAAS;AAAA,MACT,SAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,M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