import { MutableRefObject } from 'react'; import { MessageGroupForwardedRefContent } from '../components/MessageGroup'; const buildMessageSetter = ( id: string, messageGroupRef: MutableRefObject, container?: MapToMessageSetter, key: string = id, ): MessageSetterFunction => { const setter: MessageSetterFunction = (message?) => { messageGroupRef.current.setMessage?.call(null, id, message); }; if (container) { container[key as keyof T] = setter; } return setter; }; const buildMapToMessageSetter = < U extends string, I extends InputIds, M extends MapToInputId, >( ids: I, messageGroupRef: MutableRefObject, ): MapToMessageSetter => { let result: MapToMessageSetter = {} as MapToMessageSetter; if (ids instanceof Array) { result = ids.reduce>((previous, id) => { buildMessageSetter(id, messageGroupRef, previous); return previous; }, result); } else { result = Object.entries(ids).reduce>( (previous, [key, id]) => { buildMessageSetter(id, messageGroupRef, previous, key); return previous; }, result, ); } return result; }; export default buildMapToMessageSetter;