import { LabelColorName } from './types'

const STORAGE_KEY = 'memento-label-colors'

// Store label colors in localStorage
export function getLabelColor(label: string): LabelColorName {
  if (typeof window === 'undefined') return 'gray'
  
  try {
    const stored = localStorage.getItem(STORAGE_KEY)
    if (!stored) return 'gray'
    
    const colors = JSON.parse(stored) as Record<string, LabelColorName>
    return colors[label] || 'gray'
  } catch {
    return 'gray'
  }
}

export function setLabelColor(label: string, color: LabelColorName) {
  if (typeof window === 'undefined') return
  
  try {
    const stored = localStorage.getItem(STORAGE_KEY)
    const colors = stored ? JSON.parse(stored) as Record<string, LabelColorName> : {}
    colors[label] = color
    localStorage.setItem(STORAGE_KEY, JSON.stringify(colors))
  } catch (error) {
    console.error('Failed to save label color:', error)
  }
}

export function getAllLabelColors(): Record<string, LabelColorName> {
  if (typeof window === 'undefined') return {}
  
  try {
    const stored = localStorage.getItem(STORAGE_KEY)
    return stored ? JSON.parse(stored) : {}
  } catch {
    return {}
  }
}

export function deleteLabelColor(label: string) {
  if (typeof window === 'undefined') return
  
  try {
    const stored = localStorage.getItem(STORAGE_KEY)
    if (!stored) return
    
    const colors = JSON.parse(stored) as Record<string, LabelColorName>
    delete colors[label]
    localStorage.setItem(STORAGE_KEY, JSON.stringify(colors))
  } catch (error) {
    console.error('Failed to delete label color:', error)
  }
}
