import * as App from '@flamelink/sdk-app-types' ; import { logWarning , logError , EventEmitter } from '@flamelink/sdk-utils' ; import { getModule , ensureValidContext , isAdminApp , isNodeEnvironment , } from './helpers' ; import { PUBLIC_MODULES , DEFAULT_ENVIRONMENT , DEFAULT_LOCALE , DEFAULT_DB_TYPE , } from './constants' ; const createFlamelinkFactory = ( ) => { const registeredModules = [ ] ; const initRegisteredModules = ( context ) : void => { registeredModules . forEach ( ( { moduleName , setupModule } ) => { const moduleApi = setupModule ( context ) ; if ( ! moduleApi ) return ; if ( context . modules [ moduleName ] ) { return logWarning ( `Duplicate import for the "${ moduleName }" module` ) ; } Object . defineProperty ( context . modules , moduleName , { value : moduleApi , writable : false , } ) ; } ) ; } ; function flamelink ( config ) { const context = { firebaseApp : config . firebaseApp , env : config . env || DEFAULT_ENVIRONMENT , locale : config . locale || DEFAULT_LOCALE , dbType : config . dbType || DEFAULT_DB_TYPE , modules : { } , services : { } , cache : { } , emitter : new EventEmitter ( ) , precache : typeof config . precache === 'undefined' ? true : config . precache , proxySupported : typeof Proxy !== 'undefined' , usesAdminApp : isAdminApp ( config . firebaseApp ) , isNodeEnvironment : isNodeEnvironment ( ) , } ; ensureValidContext ( context ) ; initRegisteredModules ( context ) ; const api = PUBLIC_MODULES . reduce ( ( acc , moduleName ) => { return Object . defineProperty ( acc , moduleName , { enumerable : true , configurable : false , get ( ) { return getModule ( moduleName , context ) ; } , } ) ; } , { } ) ; return api ; } flamelink . _registerModule = ( moduleName , setupModule ) => { registeredModules . push ( { moduleName , setupModule , } ) ; } ; flamelink . _ensureService = ( serviceName , context ) => { if ( context . services [ serviceName ] ) { return context . services [ serviceName ] ; } try { context . services [ serviceName ] = context . firebaseApp [ serviceName ] ( ) ; } catch ( error ) { logError ( `The "${ serviceName }" Firebase service could not be instantiated. Please ensure you have imported the package for this service.` ) ; throw error ; } return context . services [ serviceName ] ; } ; return flamelink ; } ; export default createFlamelinkFactory ( ) ;