/**
 * Bitable icon registry — single source of truth for file-type icons.
 *
 * History
 * -------
 * Originally, the Bitable file ``icon`` field stored emoji glyphs (table, chart, note
 * etc.). Those look fine in isolation but clash with the project's line-icon
 * aesthetic everywhere else (Ant Design Vue ``Outlined`` family — Tabs,
 * Sidebar, Tool call cards all use stroke-based 1.5px icons). The mixed
 * style made Bitable feel like a foreign widget inside the same window.
 *
 * Decision
 * --------
 * The ``icon`` field is now a stable key (e.g. ``"table"``). The visual is
 * resolved client-side through ``resolveBitableIcon`` so:
 *   - the wire format stays a plain ASCII string (URL-safe, log-friendly,
 *     not subject to font / OS emoji rendering),
 *   - all Bitable icons share the same line-icon style,
 *   - adding a new icon = one line in ``BITABLE_ICON_MAP``.
 *
 * Backward compatibility
 * ----------------------
 * Old data may still contain emoji strings (DB row written before this
 * refactor). We fall back to ``TableOutlined`` for any unknown / non-mapped
 * value — better a uniform default than a mismatched glyph.
 */
import {
  AimOutlined,
  AppstoreOutlined,
  BookOutlined,
  CalendarOutlined,
  ContainerOutlined,
  DashboardOutlined,
  FileTextOutlined,
  TableOutlined,
} from '@ant-design/icons-vue'
import type { Component } from 'vue'

/** Stable, URL-safe identifiers used in the BitableFile.icon field. */
export type BitableIconKey =
  | 'table'
  | 'dashboard'
  | 'note'
  | 'project'
  | 'calendar'
  | 'briefcase'
  | 'aim'
  | 'book'

export const DEFAULT_BITABLE_ICON: BitableIconKey = 'table'

/**
 * Mapping from stable icon key to its Ant Design Vue outlined component.
 * Keep the keys aligned with the keys listed in
 * ``BITABLE_ICON_OPTIONS`` (src/components/bitable) so the picker stays in
 * sync with the renderer.
 */
export const BITABLE_ICON_MAP: Record<BitableIconKey, Component> = {
  table: TableOutlined,
  dashboard: DashboardOutlined,
  note: FileTextOutlined,
  project: AppstoreOutlined,
  calendar: CalendarOutlined,
  briefcase: ContainerOutlined,
  aim: AimOutlined,
  book: BookOutlined,
}

/**
 * Human-readable labels for the file-create picker. Paired with the keys
 * above; order here defines the picker display order.
 */
export const BITABLE_ICON_OPTIONS: ReadonlyArray<{ value: BitableIconKey; label: string }> = [
  { value: 'table', label: '表格' },
  { value: 'dashboard', label: '仪表盘' },
  { value: 'note', label: '笔记' },
  { value: 'project', label: '项目' },
  { value: 'calendar', label: '日程' },
  { value: 'briefcase', label: '工作' },
  { value: 'aim', label: '目标' },
  { value: 'book', label: '知识库' },
]

/**
 * Resolve a stored icon value to its component. Unknown values
 * (e.g. legacy emoji strings) silently fall back to ``DEFAULT_BITABLE_ICON``
 * so the UI stays visually consistent across mixed-era data.
 */
export function resolveBitableIcon(stored: string | null | undefined): Component {
  if (stored && stored in BITABLE_ICON_MAP) {
    return BITABLE_ICON_MAP[stored as BitableIconKey]
  }
  return BITABLE_ICON_MAP[DEFAULT_BITABLE_ICON]
}

/**
 * Type guard: returns true when ``value`` is a known Bitable icon key.
 * Use in form validators to reject arbitrary user input.
 */
export function isBitableIconKey(value: unknown): value is BitableIconKey {
  return typeof value === 'string' && value in BITABLE_ICON_MAP
}
