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

// entity-sync mappers over feed items, shared by every store holding a
// FeedItem list: sync matches on the nested entity id, not the prefixed
// item id

export const withSyncedCrumb =
  (crumb: Crumb) =>
  (item: FeedItem): FeedItem =>
    item.type === 'crumb' && item.crumb.id === crumb.id
      ? {...item, crumb}
      : item

export const withPatchedTrail =
  (id: string, patch: (trail: Trail) => Trail) =>
  (item: FeedItem): FeedItem =>
    item.type === 'trail' && item.trail.id === id
      ? {...item, trail: patch(item.trail)}
      : item

export const matchesFeedCrumb =
  (id: string) =>
  (item: FeedItem): boolean =>
    item.type === 'crumb' && item.crumb.id === id

export const matchesFeedTrail =
  (id: string) =>
  (item: FeedItem): boolean =>
    item.type === 'trail' && item.trail.id === id
