// ═══════════════════════════════════════════════════════════════
// Hook de dados · PDI · TanStack Query
// ═══════════════════════════════════════════════════════════════
// Escopo global (app sem bundler). Registrado no index.html apos data.jsx.
// Fonte: getPDIs() (LS 'escalab_pdi_v1'), definido em screen-pdi.jsx.
// LS-first (ver plano Treinamentos).

// Aguarda o fetch do Supabase antes de ler. Era sincrono e LS-first: numa
// maquina sem cache local a tela montava, a query cacheava [] e nada
// invalidava depois — a lista nunca aparecia (bug do Recrutamento, 21/07).
async function resolvePDIs() {
  if (typeof fetchPDIsSupabase === 'function') { try { await fetchPDIsSupabase(); } catch {} }
  return (typeof getPDIs === 'function') ? getPDIs() : [];
}

function usePDIs() {
  const { useQuery } = ReactQuery;
  return useQuery({ queryKey: ['pdi'], queryFn: resolvePDIs, staleTime: 300_000 });
}
