import type { FilterOption } from 'src/components/menu/intents'
import type { AccountType } from 'src/state/accountStore'

// `posts` and `microblog` are both Pixelfed-shaped values for backward
// compatibility — `posts` is the canonical first-tab id on both servers
// (Mastodon's single chronological "all posts" stream renders under it; on
// Pixelfed it doubles as the media-grid tab that filters to media-only).
// `microblog` only exists on Pixelfed and is unreachable on Mastodon.
export type ProfileTab = 'posts' | 'microblog' | 'likes' | 'bookmarks'
export type PublicProfileTab = 'posts' | 'microblog'

const GRID_OPTION: FilterOption = { value: 'posts', label: 'Grid', icon: 'grid' }
const MICROBLOG_OPTION: FilterOption = {
  value: 'microblog',
  label: 'Microblog',
  icon: 'list',
}
// Mastodon's single "Posts" tab — chronological feed including media + text +
// replies + boosts. Reuses the `posts` value so the renderer's existing
// `activeTab === 'posts'` branch still names the right tab; the grid-vs-feed
// decision moves to `isProfileGridTab(activeTab, accountType)` below.
const POSTS_OPTION: FilterOption = { value: 'posts', label: 'Posts', icon: 'list' }
const LIKES_OPTION: FilterOption = { value: 'likes', label: 'Likes', icon: 'heart' }
const BOOKMARKS_OPTION: FilterOption = {
  value: 'bookmarks',
  label: 'Bookmarks',
  icon: 'bookmark',
}

// Mastodon profiles render a single chronological stream — `PostGridCell`
// bails on `!media`, so a grid over a text-heavy Mastodon feed reads as a
// sea of holes. The Microblog tab is also Pixelfed-specific terminology and
// has no Mastodon equivalent. Drop both for Mastodon and surface the single
// "Posts" option as the default.
export function profileFilterOptionsForSelf(accountType: AccountType): FilterOption[] {
  if (accountType === 'mastodon') {
    return [POSTS_OPTION, LIKES_OPTION, BOOKMARKS_OPTION]
  }
  return [GRID_OPTION, MICROBLOG_OPTION, LIKES_OPTION, BOOKMARKS_OPTION]
}

export function profileFilterOptionsForPublic(accountType: AccountType): FilterOption[] {
  if (accountType === 'mastodon') {
    return [POSTS_OPTION]
  }
  return [GRID_OPTION, MICROBLOG_OPTION]
}

export function isSelfProfileTab(v: unknown, accountType: AccountType): v is ProfileTab {
  if (accountType === 'mastodon') {
    return v === 'posts' || v === 'likes' || v === 'bookmarks'
  }
  return v === 'posts' || v === 'microblog' || v === 'likes' || v === 'bookmarks'
}

export function isPublicProfileTab(
  v: unknown,
  accountType: AccountType
): v is PublicProfileTab {
  if (accountType === 'mastodon') {
    return v === 'posts'
  }
  return v === 'posts' || v === 'microblog'
}

// Single source of truth for the grid-vs-feed render decision. Pixelfed's
// `posts` tab is the media grid; everything else (Pixelfed `microblog`, every
// Mastodon tab, every public-profile feed) renders as a vertical list.
export function isProfileGridTab(activeTab: string, accountType: AccountType): boolean {
  return accountType === 'pixelfed' && activeTab === 'posts'
}

// Picks the cache slice for `postQueries.profileStatuses(userId, variant)`.
// Pixelfed splits its profile feed into two independent caches — `grid`
// (server-side `only_media=true`) and `microblog` (no media filter) — so the
// two tabs render their own data without one starving the other. Mastodon
// has a single chronological stream and always uses `all`.
export type ProfileStatusesVariant = 'grid' | 'microblog' | 'all'

export function profileStatusesVariant(
  activeTab: string,
  accountType: AccountType
): ProfileStatusesVariant {
  if (accountType === 'mastodon') return 'all'
  if (activeTab === 'microblog') return 'microblog'
  return 'grid'
}
