import { MutableRefObject } from 'react' ; import { MetaTagModel } from '../types' ; type InstanceConfig = MutableRefObject < MetaTagModel > ; type StoreListener = ( metas : MetaTagModel ) => void ; const metaStore = new Set < InstanceConfig > ( ) ; const subscribers = new Set < StoreListener > ( ) ; const mergeInstanceConfigs = ( configs ) : MetaTagModel => Array . from ( configs ) . map ( ( { current } ) => current ) . reduce ( ( acc , instanceConfig ) => ( { title : instanceConfig . title ?? acc . title , lang : instanceConfig . lang ?? acc . lang , tags : { ... acc . tags , ... instanceConfig . tags , } , } ) , { tags : { } } as MetaTagModel ) ; const emitChanges = ( config ) => { subscribers . forEach ( ( listener ) => listener ( config ) ) ; } ; export const addMetasToStore = ( instanceConfig ) => { ! metaStore . has ( instanceConfig ) && metaStore . add ( instanceConfig ) ; emitChanges ( mergeInstanceConfigs ( metaStore ) ) ; } ; export const removeMetasFromStore = ( instanceConfig ) => { metaStore . delete ( instanceConfig ) ; emitChanges ( mergeInstanceConfigs ( metaStore ) ) ; } ; export const clearStore = ( ) => { metaStore . clear ( ) ; emitChanges ( mergeInstanceConfigs ( metaStore ) ) ; } ; export const subscribeToStore = ( listener ) => { subscribers . add ( listener ) ; return ( ) => { subscribers . delete ( listener ) ; } ; } ; export const getState = ( ) => mergeInstanceConfigs ( metaStore ) ;