import { OhbugClient , OhbugEventWithMethods , OhbugExtension , OhbugExtensionUI , } from '@ohbug/types' ; import { isFunction } from '@ohbug/utils' ; export function createExtension ( extension ) { return extension ; } export function createExtensionUI ( extensionUI ) { return extensionUI ; } param extension param client param args export function loadExtension ( extension , client , ... args ) { const result = extension . init ?. ( client , ... args ) ; client . _extensions . push ( extension ) ; client . _hooks . created = ( _event , _client ) => { const funcs = [ _client . _config . created , ... _client . _extensions . filter ( ( { created } ) => isFunction ( created ) ) . map ( ( { created } ) => created ) , ] ; if ( funcs . length === 0 ) return ( ( ... arg ) => arg ) ( _event , _client ) ; if ( funcs . length === 1 ) return funcs [ 0 ] ?. ( _event , _client ) ; return funcs . reduce ( ( a , b ) => ( event ) => b ( a ( event , _client ) , _client ) ) ?. ( _event , _client ) ; } ; client . _hooks . notified = ( _event , _client ) => [ _client . _config . notified , ... _client . _extensions . filter ( ( { notified } ) => isFunction ( notified ) ) . map ( ( { notified } ) => notified ) , ] . forEach ( ( func ) => func ?. ( _event , _client ) ) ; return result || client ; }