{"version":3,"file":"useWidgetFetch-CPdPVEO7.cjs","sources":["../src/dashboard/hooks/useWidgetFetch.ts"],"sourcesContent":["import { useQuery } from '@tanstack/react-query';\nimport type { DashboardPeriod, SchemaField } from '../types';\nimport type { EndpointMeta } from '../api/dashboardProxy';\nimport type { CachedWidgetEnvelope } from '../api/widgetEnvelope';\nimport { fetchWidgetEnvelope, widgetDataQueryKey } from '../api/widgetEnvelope';\nimport { useDashboardContext } from '../useDashboardContext';\n\ninterface UseWidgetFetchResult<T> {\n  data: T | null;\n  schema: { fields: SchemaField[] } | null;\n  meta: EndpointMeta | null;\n  loading: boolean;\n  error: string | null;\n}\n\n/**\n * Hook centralizado para fetching de datos de widgets.\n * Retorna data (post field_mapping) + schema del endpoint (si disponible).\n *\n * <b>La clave y el contenido de la entrada de caché los define `api/widgetEnvelope`</b>, no este\n * hook: `useSecondarySource` comparte esa misma entrada a propósito y las dos tienen que guardar\n * exactamente lo mismo. Cuando el `field_mapping` vivía aquí dentro, el que perdía la carrera no\n * ejecutaba su `queryFn` y leía datos sin renombrar.\n */\nexport function useWidgetFetch<T>(\n  widgetId: string,\n  period: DashboardPeriod,\n  refreshPolicy?: number,\n  fieldMapping?: Record<string, string>\n): UseWidgetFetchResult<T> {\n  const { fetcher } = useDashboardContext();\n\n  const { data: result, isLoading, error } = useQuery<CachedWidgetEnvelope<T>>({\n    queryKey: widgetDataQueryKey(widgetId, period),\n    queryFn: () => fetchWidgetEnvelope<T>({ widgetId, period }, fieldMapping, fetcher),\n    // Mínimo 10 segundos para evitar polling agresivo por configuración incorrecta\n    refetchInterval: refreshPolicy && refreshPolicy > 0\n      ? Math.max(refreshPolicy, 10) * 1000\n      : false,\n  });\n\n  return {\n    data: result?.data ?? null,\n    schema: result?.schema ?? null,\n    meta: result?.meta ?? null,\n    loading: isLoading,\n    error: error ? (error as Error).message : null,\n  };\n}\n"],"names":["useWidgetFetch","widgetId","period","refreshPolicy","fieldMapping","fetcher","useDashboardContext","result","isLoading","error","useQuery","widgetDataQueryKey","fetchWidgetEnvelope"],"mappings":"wFAwBO,SAASA,EACdC,EACAC,EACAC,EACAC,EACyB,CACzB,KAAM,CAAE,QAAAC,CAAA,EAAYC,sBAAA,EAEd,CAAE,KAAMC,EAAQ,UAAAC,EAAW,MAAAC,CAAA,EAAUC,EAAAA,SAAkC,CAC3E,SAAUC,EAAAA,mBAAmBV,EAAUC,CAAM,EAC7C,QAAS,IAAMU,EAAAA,oBAAuB,CAAE,SAAAX,EAAU,OAAAC,CAAA,EAAUE,EAAcC,CAAO,EAEjF,gBAAiBF,GAAiBA,EAAgB,EAC9C,KAAK,IAAIA,EAAe,EAAE,EAAI,IAC9B,EAAA,CACL,EAED,MAAO,CACL,KAAMI,GAAQ,MAAQ,KACtB,OAAQA,GAAQ,QAAU,KAC1B,KAAMA,GAAQ,MAAQ,KACtB,QAASC,EACT,MAAOC,EAASA,EAAgB,QAAU,IAAA,CAE9C"}