// src/providers/MasterItemsProvider.tsx
import React, { ReactNode, useMemo } from 'react';
import { MasterItemsContext } from '../contexts/MasterItemsContext';
import { useMasterItemsQuery } from '../hooks/queries/useMasterItemsQuery';

/**
 * Provider for master grocery items using TanStack Query (ADR-0005).
 *
 * This replaces the previous custom useApiOnMount implementation with
 * TanStack Query for better caching, automatic refetching, and state management.
 *
 * Master items are cached longer (10 minutes) since they change infrequently.
 */
export const MasterItemsProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
  const {
    data: masterItems = [],
    isLoading,
    error,
  } = useMasterItemsQuery();

  const value = useMemo(
    () => ({
      masterItems,
      isLoading,
      error: error?.message || null,
    }),
    [masterItems, isLoading, error]
  );

  return <MasterItemsContext.Provider value={value}>{children}</MasterItemsContext.Provider>;
};
