import { useState } from 'react';

const STORAGE_KEY = 'screener-mode-v1';
const VALID_MODES = new Set(['form', 'canvas']);

function readMode() {
  try {
    const v = localStorage.getItem(STORAGE_KEY);
    return VALID_MODES.has(v) ? v : 'form';
  } catch {
    return 'form';
  }
}

export function useScreenerMode() {
  const [mode, setModeState] = useState(readMode);

  const setMode = (m) => {
    if (!VALID_MODES.has(m)) return;
    setModeState(m);
    try { localStorage.setItem(STORAGE_KEY, m); } catch { /* ignore quota/security errors */ }
  };

  return { mode, setMode };
}
