/* eslint-disable react-hooks/exhaustive-deps */ import { useReducer, type FC, useEffect } from 'react'; import { rookSyncGateReducer } from './RookSyncGateReducer'; // Importa el reducer import type { RookSyncGateState, RookSyncGateProviderProps, } from './RookSyncGateTypes'; import { RookSyncGateContext } from './RookSyncGateContext'; import RookSdk from '../NativeRookSdk'; import { RookStateManager } from '../modules/components/RookStateManager'; const initialState: RookSyncGateState = { clientUUID: '', environment: 'sandbox', secret: '', bundleId: undefined, packageName: undefined, ready: false, userID: '', permissions: 0, enableLogs: false, }; /* * This code snippet is defining a React functional component called `RookSyncGateProvider`. * this define the provider for the SDK */ const RookSyncGateProvider: FC = ({ environment, clientUUID, secret, bundleId, packageName, children, enableLogs = false, enableBackgroundSync, }) => { const [state, dispatch] = useReducer(rookSyncGateReducer, initialState); useEffect(() => { void initModule(); }, [ environment, clientUUID, secret, bundleId, packageName, enableLogs, enableBackgroundSync, ]); const initModule = async (): Promise => { dispatch({ type: 'SET_ROOK_ENVIRONMENT', environment }); dispatch({ type: 'SET_CLIENT_UUID', clientUUID }); dispatch({ type: 'SET_SECRET', secret }); dispatch({ type: 'SET_BUNDLE_ID', bundle: bundleId }); dispatch({ type: 'SET_PACKAGE_NAME', package: packageName }); dispatch({ type: 'SET_ENABLE_LOGS', enableLogs }); dispatch({ type: 'SET_READY', ready: false }); try { await RookSdk.initRook({ environment, clientUUID, secret, bundleId, packageName, enableLogs, enableBackground: enableBackgroundSync, }); dispatch({ type: 'SET_READY', ready: true }); } catch (error) { console.error('[RookSyncGate] initRook failed', { environment, hasClientUUID: clientUUID.trim().length > 0, hasSecret: secret.trim().length > 0, bundleId, packageName, enableLogs, enableBackgroundSync, error, }); } }; return ( {children} ); }; export default RookSyncGateProvider;