// Общие помощники UI задач: подписи статусов, приоритеты, Markdown.

import DOMPurify from 'dompurify'
import { marked } from 'marked'

export const STATUS_LABELS: Record<string, string> = {
  to_do: 'К выполнению',
  in_progress: 'В работе',
  done: 'Завершено',
  cancelled: 'Отменено',
  deferred: 'Отложено',
}

export const STATUS_VARIANTS: Record<string, string> = {
  to_do: 'info',
  in_progress: 'accent',
  done: 'success',
  cancelled: 'error',
  deferred: 'warning',
}

export const RELEVANCE_LABELS: Record<string, string> = {
  active: 'Активен',
  paused: 'Приостановлен',
  archived: 'Закрыт',
}

export function statusLabel(status: string): string {
  return STATUS_LABELS[status] ?? status
}

export function statusVariant(status: string): string {
  return STATUS_VARIANTS[status] ?? 'neutral'
}

export function relevanceLabel(status: string): string {
  return RELEVANCE_LABELS[status] ?? status
}

export function renderMarkdown(text: string): string {
  return DOMPurify.sanitize(marked.parse(text, { async: false }))
}

// «90» → «1 ч 30 м»; для чипов оценок времени
export function formatMinutes(minutes: number | null): string {
  if (minutes === null) return ''
  const h = Math.floor(minutes / 60)
  const m = minutes % 60
  if (h && m) return `${h} ч ${m} м`
  if (h) return `${h} ч`
  return `${m} м`
}