/**
 * Plain-text preview for list view (light markdown stripping).
 */
export function stripMarkdownPreview(raw: string, maxLen = 180): string {
  if (!raw?.trim()) return ''
  let t = raw
    .replace(/^#{1,6}\s+/gm, '')
    .replace(/```[\s\S]*?```/g, ' ')
    .replace(/\*\*([^*]+)\*\*/g, '$1')
    .replace(/\*([^*]+)\*/g, '$1')
    .replace(/`([^`]+)`/g, '$1')
    .replace(/\[(.+?)]\([^)]+\)/g, '$1')
    .replace(/^\s*[-*+]\s+/gm, '')
    .replace(/^\s*\d+\.\s+/gm, '')
    .replace(/\n+/g, ' ')
    .replace(/\s+/g, ' ')
    .trim()
  if (t.length > maxLen) {
    return `${t.slice(0, maxLen).trim()}…`
  }
  return t
}

export function getNoteDisplayTitle(note: { title: string | null; content: string; type: string }, untitled: string): string {
  const title = note.title?.trim()
  if (title) return title
  if (note.type === 'checklist') {
    const line = note.content?.split('\n').find((l) => l.trim())
    if (line) return stripMarkdownPreview(line, 80) || untitled
  }
  const preview = stripMarkdownPreview(note.content || '', 100)
  return preview || untitled
}
