import { UserEventCategory } from '@spiffy-ai/commerce-api-client';
import { Message, MessageRole, MessageType, Suggestion, UserEvent } from 'src/application/models';

/**
 * Transforms a UserEvent object into a Message object for presentation.
 *
 * @param event The UserEvent object received from the server
 * @param suggestions A list of generated suggestions to match the event to
 *
 * @returns A Message if the event is a suggestion click event, undefined otherwise
 */
export const messageFromSuggestionEvent = (
  event: UserEvent,
  suggestions: Suggestion[],
): Message | undefined => {
  if (event.category === UserEventCategory.SuggestionClicked) {
    const { suggestionId } = event.attributes;

    return {
      id: event.eventId,
      role: MessageRole.User,
      type: MessageType.SuggestionClicked,
      createdAt: event.createdAt,
      metadata: {
        suggestionId,
        suggestionContent: suggestions.find(s => s.id === suggestionId)?.content ?? '',
      },
    };
  }

  return undefined;
};
