import type { AnalyticsControllerState } from './AnalyticsController.js'; import type { AnalyticsEventFragment, AnalyticsEventFragments, } from './EventFragment.types.js'; const EMPTY_EVENT_FRAGMENTS = Object.freeze({}) as AnalyticsEventFragments; /** * Selects the analytics ID from the controller state. * * @param state - The controller state * @returns The analytics ID */ const selectAnalyticsId = (state: AnalyticsControllerState): string => state.analyticsId; /** * Selects the opted-in user status from the controller state. * Use this selector to read the user's opt-in preference (e.g., for UI display). * * @param state - The controller state * @returns Whether the user has opted in to analytics */ const selectOptedIn = (state: AnalyticsControllerState): boolean => state.optedIn; /** * Selects the marketing opt-in status from the controller state. * * @param state - The controller state * @returns Whether the user has opted in to marketing analytics */ const selectOptedInToMarketing = (state: AnalyticsControllerState): boolean => state.optedInToMarketing === true; /** * Selects whether analytics tracking is enabled. * Use this selector to determine if tracking should occur (e.g., in controller methods). * * @param state - The controller state * @returns Whether analytics tracking is enabled */ const selectEnabled = (state: AnalyticsControllerState): boolean => state.optedIn; /** * Selects whether the user has made a consent decision (opted in or opted out). * Use this selector to distinguish the "undecided" state (e.g. during * onboarding) from an explicit opt-out. * * @param state - The controller state * @returns Whether the user has made a consent decision */ const selectConsentDecisionMade = (state: AnalyticsControllerState): boolean => state.consentDecisionMade ?? false; /** * Selects whether the user has made a marketing consent decision. * * @param state - The controller state * @returns Whether the user has made a marketing consent decision */ const selectMarketingConsentDecisionMade = ( state: AnalyticsControllerState, ): boolean => state.marketingConsentDecisionMade ?? false; /** * Selects whether the marketing consent held locally is a choice not yet * saved to the user's profile in Authenticated User Storage. * * @param state - The controller state * @returns Whether the marketing consent is waiting to be saved */ const selectMarketingConsentSyncPending = ( state: AnalyticsControllerState, ): boolean => state.marketingConsentSyncPending === true; /** * Selects the in-progress event fragments from the controller state. * * @param state - The controller state * @returns The event fragments keyed by fragment ID, empty when the event * fragments feature has never written any */ const selectEventFragments = ( state: AnalyticsControllerState, ): AnalyticsEventFragments => state.eventFragments ?? EMPTY_EVENT_FRAGMENTS; /** * Selects a single event fragment from the controller state. * * @param state - The controller state * @param id - The fragment ID * @returns The fragment, or `undefined` when no fragment has that ID */ const selectEventFragmentById = ( state: AnalyticsControllerState, id: string, ): AnalyticsEventFragment | undefined => state.eventFragments?.[id]; /** * Selectors for the AnalyticsController state. * These can be used with Redux or directly with controller state. */ export const analyticsControllerSelectors = { selectAnalyticsId, selectOptedIn, selectOptedInToMarketing, selectEnabled, selectConsentDecisionMade, selectMarketingConsentDecisionMade, selectMarketingConsentSyncPending, selectEventFragments, selectEventFragmentById, };