import { randomHex } from '@/backend/configManager/encryption/crypto' ; import { AccountToScrapeConfig , Config , OutputVendorConfig , OutputVendorName } from '@/backend/configManager/configManager' ; import { defineModule } from 'direct-vuex' ; import { moduleActionContext } from '../index' ; export type GlobalConfig = { numDaysBack : number ; showBrowser : boolean ; } ; type ExporterPayload < T extends OutputVendorName > = { name : T ; exporter : OutputVendorConfig < T > ; } ; const configModule = defineModule ( { namespaced : true as true , mutations : { addImporter : ( state , importer ) => state . scraping . accountsToScrape . push ( importer ) , removeImporter : ( state , importerId ) => { state . scraping . accountsToScrape = state . scraping . accountsToScrape . filter ( ( importer ) => importer . id !== importerId ) ; } , updateExporter : < T extends OutputVendorName > ( state , payload ) => { state . outputVendors [ payload . name ] = payload . exporter ; } , updateGlobalConfig : ( state , updatedConfig ) => { state . scraping . numDaysBack = updatedConfig . numDaysBack ; state . scraping . showBrowser = updatedConfig . showBrowser ; } } , getters : { importers : ( state ) => state . scraping . accountsToScrape , getExporter : < T extends OutputVendorName > ( state ) => ( name ) : OutputVendorConfig < T > => state . outputVendors [ name ] , globalConfig : ( { scraping } ) : GlobalConfig => { const { numDaysBack , showBrowser } = scraping ; return { numDaysBack , showBrowser } ; } , getActiveImporters : ( state ) => { return state . scraping . accountsToScrape . filter ( ( accountToScrape ) => accountToScrape . active ) . map ( ( accountToScrape ) => ( { id : accountToScrape . key , name : accountToScrape . name } ) ) ; } , getActiveExporters : ( state ) => { const activeExporters = [ ] ; Object . entries ( state . outputVendors ) . forEach ( ( [ outputVendorName , outputVendorConfig ] ) => { if ( outputVendorConfig ?. active ) { activeExporters . push ( { id : outputVendorName , name : outputVendorName } ) ; } } ) ; return activeExporters ; } , getState : ( state ) => state } , actions : { addImporter : ( { commit } , importer ) => { importer . id = randomHex ( ) ; commit ( 'addImporter' , importer ) ; } , removeImporter : ( { commit } , importerId ) => { commit ( 'removeImporter' , importerId ) ; } , updateExporter : < T extends OutputVendorName > ( context , payload ) => { configModuleActionContext ( context ) . commit . updateExporter ( payload ) ; } , updateGlobalConfig : ( { commit } , globalConfig ) => { commit ( 'updateGlobalConfig' , globalConfig ) ; } } , } ) ; export default configModule ; const configModuleActionContext = ( context ) => moduleActionContext ( context , configModule ) ;