import { requireNativeComponent, Platform, UIManager } from 'react-native'; export let OwnIdNativeManagerName: string = Platform.OS === 'ios' ? 'OwnIdButton' : 'OwnIdButtonManager'; export function isFabric(): boolean { try { return !!(global as any)?.nativeFabricUIManager; } catch { return false; } } let _ownIdInitReady = false; const _ownIdInitListeners: Array<() => void> = []; export function _setInitPending(promise: Promise) { promise.then(() => { _ownIdInitReady = true; while (_ownIdInitListeners.length) { try { _ownIdInitListeners.shift()?.(); } catch { } } }).catch(() => { }); } export function _onInitReady(cb: () => void): () => void { if (_ownIdInitReady) { cb(); return () => { }; } _ownIdInitListeners.push(cb); return () => { const idx = _ownIdInitListeners.indexOf(cb); if (idx >= 0) _ownIdInitListeners.splice(idx, 1); }; } export function _isInitReady(): boolean { return _ownIdInitReady; } let CachedNativeOwnIdButton: any | null = null; export function getNativeOwnIdButton(): any { if (CachedNativeOwnIdButton) { return CachedNativeOwnIdButton; } if (isFabric()) { try { const Comp = require('./specs/NativeOwnIdButtonNativeComponent').default; CachedNativeOwnIdButton = Comp; return Comp; } catch { } } const candidates = Platform.OS === 'ios' ? ['OwnIdButton', 'OwnIdButtonManagerManager'] : ['OwnIdButtonManager']; const name = candidates.find(n => !!UIManager?.getViewManagerConfig?.(n)) ?? candidates[0]; CachedNativeOwnIdButton = requireNativeComponent(name); return CachedNativeOwnIdButton; } // Internal Fabric handshake. The command is sent only after both global event listeners exist. export function _notifyFabricListenersReady(viewRef: any, ownerToken: string): void { if (!viewRef) return; const spec = require('./specs/NativeOwnIdButtonNativeComponent') as typeof import('./specs/NativeOwnIdButtonNativeComponent'); spec.Commands.listenersReady(viewRef, ownerToken); } // Legacy setter retained for external overrides (used by Gigya Android) export function _setOwnIdNativeViewManager(value: any) { CachedNativeOwnIdButton = value; } /** * Represents the type of OwnID flow - Register or Login to be performed by a button. */ export enum OwnIdButtonType { Register = 'register', Login = 'login', } /** * Represents the OwnID response for Registration or Login flows. * * @property {string} authType - (optional) Type of authentication used in the OwnID flow. * @property {string} loginId - (optional) User login ID used in the OwnID flow. Unavailable in `onLogin` callback for OwnID with Integration component. * @property {OwnIdPayload} payload - (optional) Result of the OwnID flow. Only available for OwnID without the Integration component. * @property {string} authToken - (optional) A token that can be used to refresh a session. */ export interface OwnIdResponse { authType?: string; loginId?: string; payload?: OwnIdPayload; authToken?: string; } /** * Represents the type of OwnID Payload - Registration or Login. */ export enum OwnIdPayloadType { Registration = 'Registration', Login = 'Login', } /** * Represents the result of an OwnID flow. * * @property {OwnIdPayloadType} type - The type of data in the `data` and `metadata` properties. * @property {any} data - OwnID Data (OwnID authentication data object) as a parsed JSON Object or a string. * @property {string} metadata - A string with information on how to use the [data]. Integration-specific. */ export interface OwnIdPayload { type: OwnIdPayloadType; data: any; metadata: string; } /** * Interface representing an error with additional metadata specific to OwnID. * * @property {string} className - The class name where the error occurred. * @property {string | null} code - The error code, or null if unavailable. * @property {string} message - A user-friendly localized text message describing the error if `code` is present, otherwise the error message. * @property {IOwnIdError | null} cause - The original exception that is wrapped in, or null if none. * @property {string} stackTrace - The stack trace for the error. */ export interface IOwnIdError { className?: string; code?: string | null; message: string; cause?: IOwnIdError | null; stackTrace?: string; } /** * Represents an error with additional metadata specific to OwnID. */ export class OwnIdError implements IOwnIdError { className?: string; code?: string | null; message: string; cause?: IOwnIdError | null; stackTrace?: string; constructor({ className, code, message, cause, stackTrace }: IOwnIdError) { this.className = className; this.code = code; this.message = message; this.cause = cause; this.stackTrace = stackTrace; } } const ownIdButtonIds: { [index: string]: number | null } = {}; // Internal export function _setViewId(buttonType: OwnIdButtonType, viewId: number | null) { ownIdButtonIds[buttonType] = viewId; } // Internal export function _getViewId(buttonType: OwnIdButtonType) { return ownIdButtonIds[buttonType]; }