import type { FormAnswers } from './types'

const PREFIX = 'survey_draft_'

// Only store answer data — nothing identifiable about the respondent.
export function saveDraft(surveyId: string, answers: FormAnswers): void {
  try {
    localStorage.setItem(PREFIX + surveyId, JSON.stringify(answers))
  } catch {
    // Storage quota or private-mode — silently ignore
  }
}

export function loadDraft(surveyId: string): FormAnswers {
  try {
    const raw = localStorage.getItem(PREFIX + surveyId)
    return raw ? (JSON.parse(raw) as FormAnswers) : {}
  } catch {
    return {}
  }
}

export function clearDraft(surveyId: string): void {
  try {
    localStorage.removeItem(PREFIX + surveyId)
  } catch {}
}
