import type {FeedItem} from '@crumbs/core/schemas'

import {CrumbRow} from '@/components/crumb-list'
import {TrailRow} from '@/components/trail-list'
import {List} from '@/components/ui/list'

// one activity feed row: a crumb saved or a trail created, reusable by any
// list container (List, VirtualList)
export const FeedRow = ({
  item,
  currentUserId,
  hideAuthor,
}: {
  item: FeedItem
  currentUserId?: string
  hideAuthor?: boolean
}) =>
  item.type === 'crumb' ? (
    <CrumbRow
      crumb={{...item.crumb, authorHandle: item.author.handle}}
      showAuthor={!hideAuthor && item.author.id !== currentUserId}
    />
  ) : (
    <TrailRow
      trail={item.trail}
      currentUserId={currentUserId}
      hideAuthor={hideAuthor}
    />
  )

export const FeedList = ({
  items,
  loading,
  loadingMore,
  emptyMessage = 'No activity yet',
  currentUserId,
  hideAuthor,
}: {
  items: FeedItem[]
  loading?: boolean
  loadingMore?: boolean
  emptyMessage?: string
  currentUserId?: string
  // single-subject feeds (a profile's activity) already imply the author,
  // so hide the per-row handle instead of repeating it on every item
  hideAuthor?: boolean
}) => (
  <List
    items={items}
    loading={loading}
    loadingMore={loadingMore}
    emptyMessage={emptyMessage}
    renderItem={(item) => (
      <FeedRow
        item={item}
        currentUserId={currentUserId}
        hideAuthor={hideAuthor}
      />
    )}
  />
)
