export function loadRecord<T>(key: string, isValue: (value: unknown) => value is T): Record<string, T> {
  if (typeof localStorage === 'undefined') return {}
  try {
    const raw = localStorage.getItem(key)
    if (!raw) return {}
    const parsed: unknown = JSON.parse(raw)
    if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {}
    return Object.fromEntries(
      Object.entries(parsed as Record<string, unknown>).filter(
        (entry): entry is [string, T] => typeof entry[0] === 'string' && isValue(entry[1]),
      ),
    )
  } catch {
    return {}
  }
}

export function saveRecord(key: string, value: Record<string, unknown>): void {
  localStorage.setItem(key, JSON.stringify(value))
}
