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

/**
 * Transforms a query UserEvent object into a Message object for presentation.
 *
 * @param event The user event object received from the server
 *
 * @returns A Message if the event is a query event, otherwise undefined
 */
export const messageFromQueryEvent = (event: UserEvent): Message | undefined => {
  if (event.category === UserEventCategory.QueryTyped) {
    return {
      id: event.eventId,
      role: MessageRole.User,
      type: MessageType.QueryTyped,
      createdAt: event.createdAt,
      metadata: {
        content: event.attributes.query,
      },
    };
  }

  if (event.category === UserEventCategory.Search) {
    return {
      id: event.eventId,
      role: MessageRole.User,
      type: MessageType.Search,
      createdAt: event.createdAt,
      metadata: {
        searchTerm: event.attributes.searchTerm || '',
        selectedFilters: event.attributes.selectedFilters || [],
      },
    };
  }

  return undefined;
};
