/**
 * Counts manual skip-intro presses per series. After a threshold we
 * surface a "want to auto-skip these for the rest of this show?" prompt.
 *
 * Storage: `jf_skip_count_<seriesId>` -> number, plus `jf_skip_dismissed_
 * <seriesId>` -> '1' for series the user has explicitly waved off.
 */

const COUNT_PREFIX = 'jf_skip_count_'
const DISMISSED_PREFIX = 'jf_skip_dismissed_'
export const SKIP_THRESHOLD = 3

export function recordManualSkip(seriesId: string): { shouldPrompt: boolean; count: number } {
  if (!seriesId) return { shouldPrompt: false, count: 0 }
  const dismissed = localStorage.getItem(DISMISSED_PREFIX + seriesId) === '1'
  if (dismissed) return { shouldPrompt: false, count: 0 }
  const current = Number(localStorage.getItem(COUNT_PREFIX + seriesId) || '0')
  const next = current + 1
  try { localStorage.setItem(COUNT_PREFIX + seriesId, String(next)) } catch { /* noop */ }
  return { shouldPrompt: next >= SKIP_THRESHOLD, count: next }
}

export function dismissSkipPrompt(seriesId: string) {
  try {
    localStorage.setItem(DISMISSED_PREFIX + seriesId, '1')
    localStorage.removeItem(COUNT_PREFIX + seriesId)
  } catch { /* noop */ }
}

export function clearSkipCount(seriesId: string) {
  try {
    localStorage.removeItem(COUNT_PREFIX + seriesId)
  } catch { /* noop */ }
}
